1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! `App` — owns the editor + host, drives the event loop.
use anyhow::Result;
use crossterm::{
cursor::SetCursorStyle,
event::{self, Event, KeyCode, KeyModifiers},
execute,
};
use hjkl_buffer::Buffer;
use hjkl_editor::runtime::ex::{self, ExEffect};
use hjkl_engine::{BufferEdit, Host};
use hjkl_engine::{CursorShape, Editor, Options, VimMode};
use hjkl_tree_sitter::DotFallbackTheme;
use ratatui::{Terminal, backend::CrosstermBackend};
use std::io::Stdout;
use std::path::PathBuf;
use crate::host::TuiHost;
use crate::render;
use crate::syntax::{self, SYNTAX_WINDOW_MARGIN, SyntaxLayer};
/// Height reserved for the status line at the bottom of the screen.
pub const STATUS_LINE_HEIGHT: u16 = 1;
/// Line-editing buffer for the `:` command prompt.
///
/// Tracks `text` and a byte-offset `cursor` within it so Phase 4 can
/// render the insertion point and support full editing ops.
///
/// Invariant: `cursor` is always a valid UTF-8 boundary in `text`.
#[derive(Default, Clone)]
pub struct CommandInput {
/// The typed command text (without the leading `:`).
pub text: String,
/// Byte offset of the insertion point within `text`.
pub cursor: usize,
}
impl CommandInput {
/// Insert `c` at the current cursor position and advance past it.
pub fn insert_char(&mut self, c: char) {
self.text.insert(self.cursor, c);
self.cursor += c.len_utf8();
}
/// Delete the character immediately before the cursor (Backspace).
pub fn backspace(&mut self) {
if self.cursor == 0 {
return;
}
// Step back to the previous char boundary.
let prev = prev_char_boundary(&self.text, self.cursor);
self.text.drain(prev..self.cursor);
self.cursor = prev;
}
/// Delete the character at the cursor (Delete / Forward-delete).
pub fn delete_forward(&mut self) {
if self.cursor >= self.text.len() {
return;
}
let next = next_char_boundary(&self.text, self.cursor);
self.text.drain(self.cursor..next);
}
/// Move cursor one char to the left.
pub fn move_left(&mut self) {
if self.cursor > 0 {
self.cursor = prev_char_boundary(&self.text, self.cursor);
}
}
/// Move cursor one char to the right.
pub fn move_right(&mut self) {
if self.cursor < self.text.len() {
self.cursor = next_char_boundary(&self.text, self.cursor);
}
}
/// Move cursor to the start of the text (Home / Ctrl-A).
pub fn move_home(&mut self) {
self.cursor = 0;
}
/// Move cursor to the end of the text (End / Ctrl-E).
pub fn move_end(&mut self) {
self.cursor = self.text.len();
}
/// Clear the text and reset the cursor (Ctrl-U).
pub fn clear(&mut self) {
self.text.clear();
self.cursor = 0;
}
/// Delete back to the previous word boundary (Ctrl-W).
///
/// Skips trailing spaces, then deletes back to the next space (or the
/// start of text), matching vim's `Ctrl-W` in command line.
pub fn delete_word_back(&mut self) {
if self.cursor == 0 {
return;
}
// Skip trailing spaces.
let mut pos = self.cursor;
while pos > 0 {
let prev = prev_char_boundary(&self.text, pos);
if !self.text[prev..pos].starts_with(' ') {
break;
}
pos = prev;
}
// Delete back to the previous space.
while pos > 0 {
let prev = prev_char_boundary(&self.text, pos);
if self.text[prev..pos].starts_with(' ') {
break;
}
pos = prev;
}
self.text.drain(pos..self.cursor);
self.cursor = pos;
}
/// Number of display columns the prefix `char` + text before cursor
/// occupies. Used by the renderer to place the terminal cursor.
/// `prefix_width` is 1 for `:`, `/`, `?`.
pub fn display_cursor_col(&self, prefix_width: usize) -> u16 {
// For now assume every byte of the text up to cursor is one
// display column (ASCII assumption; good enough for command input).
(prefix_width + self.text[..self.cursor].chars().count()) as u16
}
}
/// Return the byte offset of the char boundary that is strictly before `pos`
/// in `s`. Panics if `pos == 0`.
fn prev_char_boundary(s: &str, pos: usize) -> usize {
let mut p = pos - 1;
while !s.is_char_boundary(p) {
p -= 1;
}
p
}
/// Return the byte offset of the char boundary that is strictly after `pos`
/// in `s`. Panics if `pos >= s.len()`.
fn next_char_boundary(s: &str, pos: usize) -> usize {
let mut p = pos + 1;
while !s.is_char_boundary(p) {
p += 1;
}
p
}
/// Top-level application state. Everything the event loop and renderer need.
pub struct App {
/// The live editor — buffer + FSM + host, all in one.
pub editor: Editor<Buffer, TuiHost>,
/// File path shown in status line and used for `:w` saves.
pub filename: Option<PathBuf>,
/// Set to `true` when the FSM or Ctrl-C wants to quit.
pub exit_requested: bool,
/// Persistent dirty flag. Set when `editor.take_dirty()` returns `true`;
/// cleared after a successful `:w` save.
pub dirty: bool,
/// Last ex-command result (Info / Error / write confirmation).
/// Shown in the status line; cleared on next keypress.
pub status_message: Option<String>,
/// Active `:` command input. `Some` while the user is typing an ex command.
pub command_input: Option<CommandInput>,
/// Last cursor shape we emitted to the terminal. Compared each
/// frame so we only write the DECSCUSR sequence on transitions.
last_cursor_shape: CursorShape,
/// True when a file was requested but not found on disk — shows
/// "[New File]" annotation in the status line until the first edit
/// or successful `:w`.
pub is_new_file: bool,
/// Tree-sitter syntax highlighting layer. Owns the registry, highlighter,
/// and active theme. Recomputed on every buffer-mutating event.
syntax: SyntaxLayer,
/// `top_row` of the viewport at the time of the last syntax recompute.
/// `None` forces a recompute on the first frame regardless of dirty state.
last_highlight_top: Option<usize>,
}
impl App {
/// Build a fresh [`App`], optionally loading `filename` from disk.
///
/// - File found → content seeded into buffer, dirty = false.
/// - File not found → buffer empty, filename retained, `is_new_file = true`.
/// - Other I/O error → returns `Err` so main can print to stderr before
/// entering alternate-screen mode.
///
/// `readonly` sets `:set readonly` on the editor options.
/// `goto_line` (1-based) moves the cursor after load when `Some`.
/// `search_pattern` triggers an initial search when `Some`.
pub fn new(
filename: Option<PathBuf>,
readonly: bool,
goto_line: Option<usize>,
search_pattern: Option<String>,
) -> Result<Self> {
let mut buffer = Buffer::new();
let mut is_new_file = false;
if let Some(ref path) = filename {
match std::fs::read_to_string(path) {
Ok(content) => {
// Strip one trailing newline (vim default): a file
// ending in `\n` is the EOL of its last line, not
// a separator before an empty trailing line. Save
// re-appends one.
let content = content.strip_suffix('\n').unwrap_or(&content);
BufferEdit::replace_all(&mut buffer, content);
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
// New file — buffer stays empty, filename retained.
is_new_file = true;
}
Err(e) => {
return Err(anyhow::anyhow!("{}: {}", path.display(), e));
}
}
}
let host = TuiHost::new();
let options = Options {
readonly,
..Options::default()
};
let mut editor = Editor::new(buffer, host, options);
// +N line jump — 1-based, clamp to buffer.
if let Some(n) = goto_line {
editor.goto_line(n);
}
// +/pattern initial search — compile the pattern and set it.
if let Some(pat) = search_pattern {
match regex::Regex::new(&pat) {
Ok(re) => {
editor.set_search_pattern(Some(re));
editor.search_advance_forward(false);
}
Err(e) => {
// Bad regex — surface as a startup status message later.
// We can't set status_message before Self is constructed,
// so we store it in a temporary and set it after build.
eprintln!("hjkl: bad search pattern: {e}");
}
}
}
// Build syntax layer (dark theme default) and detect language for
// the opened file, then run an initial highlight pass.
let mut syntax = syntax::default_layer();
if let Some(ref path) = filename {
syntax.set_language_for_path(path);
}
let initial_vp_top = editor.host().viewport().top_row;
let initial_vp_height = editor.host().viewport().height as usize;
if let Some(spans) = syntax.recompute(editor.buffer(), initial_vp_top, initial_vp_height) {
editor.install_ratatui_syntax_spans(spans);
}
Ok(Self {
editor,
filename,
exit_requested: false,
dirty: false,
status_message: None,
command_input: None,
last_cursor_shape: CursorShape::Block,
is_new_file,
syntax,
last_highlight_top: Some(initial_vp_top),
})
}
/// Main event loop. Draws every frame, routes key events through
/// the vim FSM, handles resize, exits on Ctrl-C.
pub fn run(&mut self, terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
loop {
// Update host viewport dimensions from the current terminal size.
{
let size = terminal.size()?;
let vp = self.editor.host_mut().viewport_mut();
vp.width = size.width;
vp.height = size.height.saturating_sub(STATUS_LINE_HEIGHT);
}
// Emit cursor shape before the draw call, once per transition.
let current_shape = self.editor.host().cursor_shape();
if current_shape != self.last_cursor_shape {
match current_shape {
CursorShape::Block => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyBlock);
}
CursorShape::Bar => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyBar);
}
CursorShape::Underline => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyUnderScore);
}
}
self.last_cursor_shape = current_shape;
}
// Draw the current frame.
terminal.draw(|frame| render::frame(frame, self))?;
// Process the next event (blocking).
match event::read()? {
Event::Key(key) => {
// Ctrl-C is the hard-exit shortcut independent of the FSM.
if key.code == KeyCode::Char('c')
&& key.modifiers.contains(KeyModifiers::CONTROL)
{
break;
}
// Clear status message on any keypress (vim-style).
self.status_message = None;
// ── Command input mode (`:` prompt) ──────────────────────
if let Some(ref mut cmd) = self.command_input {
match (key.modifiers, key.code) {
(KeyModifiers::NONE, KeyCode::Esc) => {
self.command_input = None;
}
(KeyModifiers::NONE, KeyCode::Enter) => {
let cmd_text = self.command_input.take().unwrap_or_default().text;
self.dispatch_ex(cmd_text.trim());
}
(KeyModifiers::NONE, KeyCode::Backspace) => {
cmd.backspace();
}
(KeyModifiers::NONE, KeyCode::Delete) => {
cmd.delete_forward();
}
(KeyModifiers::NONE, KeyCode::Left) => {
cmd.move_left();
}
(KeyModifiers::NONE, KeyCode::Right) => {
cmd.move_right();
}
(KeyModifiers::NONE, KeyCode::Home) => {
cmd.move_home();
}
(KeyModifiers::NONE, KeyCode::End) => {
cmd.move_end();
}
// Ctrl-A — move to start (readline convention).
(KeyModifiers::CONTROL, KeyCode::Char('a')) => {
cmd.move_home();
}
// Ctrl-E — move to end.
(KeyModifiers::CONTROL, KeyCode::Char('e')) => {
cmd.move_end();
}
// Ctrl-U — clear entire line.
(KeyModifiers::CONTROL, KeyCode::Char('u')) => {
cmd.clear();
}
// Ctrl-W — delete-word-back.
(KeyModifiers::CONTROL, KeyCode::Char('w')) => {
cmd.delete_word_back();
}
(KeyModifiers::NONE | KeyModifiers::SHIFT, KeyCode::Char(c)) => {
cmd.insert_char(c);
}
_ => {}
}
// Don't fall through to editor FSM while in cmd mode.
if self.exit_requested {
break;
}
continue;
}
// ── Intercept `:` in Normal mode to open command prompt ──
if key.code == KeyCode::Char(':')
&& key.modifiers == KeyModifiers::NONE
&& self.editor.vim_mode() == VimMode::Normal
{
self.command_input = Some(CommandInput::default());
continue;
}
// ── Normal editor key handling ───────────────────────────
self.editor.handle_key(key);
// Recompute when content changed or when the viewport has
// scrolled past half the cache margin (approaching the edge
// of the previously highlighted window).
let did_dirty = self.editor.take_dirty();
if did_dirty {
self.dirty = true;
self.is_new_file = false;
}
let vp_top = self.editor.host().viewport().top_row;
let scrolled = self
.last_highlight_top
.is_none_or(|t| vp_top.abs_diff(t) >= SYNTAX_WINDOW_MARGIN / 2);
if did_dirty || scrolled {
self.recompute_and_install();
self.last_highlight_top = Some(vp_top);
}
}
Event::Resize(w, h) => {
let vp = self.editor.host_mut().viewport_mut();
vp.width = w;
vp.height = h.saturating_sub(STATUS_LINE_HEIGHT);
}
_ => {}
}
if self.exit_requested {
break;
}
}
Ok(())
}
/// Execute an ex command string (without the leading `:`).
fn dispatch_ex(&mut self, cmd: &str) {
// Intercept `:set background={dark,light}` before the engine sees it.
// Theme awareness is a binary-local concern; the engine has no theme API.
if let Some(rest) = cmd.strip_prefix("set background=") {
match rest.trim() {
"dark" => {
self.syntax.set_theme(Box::new(DotFallbackTheme::dark()));
self.recompute_and_install();
self.status_message = Some("background=dark".into());
return;
}
"light" => {
self.syntax.set_theme(Box::new(DotFallbackTheme::light()));
self.recompute_and_install();
self.status_message = Some("background=light".into());
return;
}
other => {
self.status_message = Some(format!("E: unknown background value: {other}"));
return;
}
}
}
match ex::run(&mut self.editor, cmd) {
ExEffect::None => {}
ExEffect::Ok => {}
ExEffect::Save => {
self.do_save(None);
}
ExEffect::SaveAs(path) => {
self.do_save(Some(PathBuf::from(path)));
}
ExEffect::Quit { force, save } => {
if save {
// :wq / :x — save first, then quit.
self.do_save(None);
if self.exit_requested {
// do_save set exit_requested on error? No — only quit
// path sets it. If save succeeded (no error msg) quit.
}
// Quit regardless of save result to match vim behaviour for :wq.
self.exit_requested = true;
} else if force {
// :q!
self.exit_requested = true;
} else {
// :q — block if dirty.
if self.dirty {
self.status_message =
Some("E37: No write since last change (add ! to override)".into());
} else {
self.exit_requested = true;
}
}
}
ExEffect::Substituted { count } => {
// Engine applied the substitution in-place; propagate dirty.
if self.editor.take_dirty() {
self.dirty = true;
self.recompute_and_install();
}
self.status_message = Some(format!("{count} substitution(s)"));
}
ExEffect::Info(msg) => {
self.status_message = Some(msg);
}
ExEffect::Error(msg) => {
self.status_message = Some(format!("E: {msg}"));
}
ExEffect::Unknown(c) => {
self.status_message = Some(format!("E492: Not an editor command: :{c}"));
}
}
}
/// Write buffer content to `path` (or `self.filename` if `path` is
/// `None`). Updates `self.filename` and `self.dirty` on success.
///
/// Blocks writes when the editor is in readonly mode unless `force` is
/// true (`:w!` not yet wired — for now `:set noreadonly` + `:w` is the
/// override path, matching Phase 5 spec).
fn do_save(&mut self, path: Option<PathBuf>) {
// Readonly guard — E45 matches vim's message.
if self.editor.is_readonly() {
self.status_message = Some("E45: 'readonly' option is set (add ! to override)".into());
return;
}
let target = path.or_else(|| self.filename.clone());
match target {
None => {
self.status_message = Some("E32: No file name".into());
}
Some(p) => {
let lines = self.editor.buffer().lines();
// vim default: lines joined with \n, trailing \n after last
// non-empty line.
let content = if lines.is_empty() {
String::new()
} else {
let mut s = lines.join("\n");
s.push('\n');
s
};
match std::fs::write(&p, &content) {
Ok(()) => {
let line_count = lines.len();
let byte_count = content.len();
self.status_message = Some(format!(
"\"{}\" {}L, {}B written",
p.display(),
line_count,
byte_count,
));
self.filename = Some(p);
self.dirty = false;
self.is_new_file = false;
}
Err(e) => {
self.status_message = Some(format!("E: {}: {e}", p.display()));
}
}
}
}
}
/// Recompute syntax spans for the current viewport window and install them.
///
/// No-op when no language is attached (highlighter is `None`).
pub fn recompute_and_install(&mut self) {
let (top, height) = {
let vp = self.editor.host().viewport();
(vp.top_row, vp.height as usize)
};
if let Some(spans) = self.syntax.recompute(self.editor.buffer(), top, height) {
self.editor.install_ratatui_syntax_spans(spans);
}
}
/// Mode label for the status line.
pub fn mode_label(&self) -> &'static str {
match self.editor.vim_mode() {
VimMode::Normal => "NORMAL",
VimMode::Insert => "INSERT",
VimMode::Visual => "VISUAL",
VimMode::VisualLine => "VISUAL LINE",
VimMode::VisualBlock => "VISUAL BLOCK",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ci(text: &str, cursor: usize) -> CommandInput {
CommandInput {
text: text.to_string(),
cursor,
}
}
#[test]
fn insert_char_at_end() {
let mut c = CommandInput::default();
c.insert_char('h');
c.insert_char('i');
assert_eq!(c.text, "hi");
assert_eq!(c.cursor, 2);
}
#[test]
fn insert_char_at_middle() {
let mut c = ci("ac", 1);
c.insert_char('b');
assert_eq!(c.text, "abc");
assert_eq!(c.cursor, 2);
}
#[test]
fn backspace_removes_before_cursor() {
let mut c = ci("abc", 2);
c.backspace();
assert_eq!(c.text, "ac");
assert_eq!(c.cursor, 1);
}
#[test]
fn backspace_at_start_is_noop() {
let mut c = ci("abc", 0);
c.backspace();
assert_eq!(c.text, "abc");
assert_eq!(c.cursor, 0);
}
#[test]
fn delete_forward_removes_at_cursor() {
let mut c = ci("abc", 1);
c.delete_forward();
assert_eq!(c.text, "ac");
assert_eq!(c.cursor, 1);
}
#[test]
fn delete_forward_at_end_is_noop() {
let mut c = ci("abc", 3);
c.delete_forward();
assert_eq!(c.text, "abc");
}
#[test]
fn move_left_right() {
let mut c = ci("ab", 2);
c.move_left();
assert_eq!(c.cursor, 1);
c.move_left();
assert_eq!(c.cursor, 0);
c.move_left(); // already at start
assert_eq!(c.cursor, 0);
c.move_right();
assert_eq!(c.cursor, 1);
}
#[test]
fn home_end() {
let mut c = ci("hello", 3);
c.move_home();
assert_eq!(c.cursor, 0);
c.move_end();
assert_eq!(c.cursor, 5);
}
#[test]
fn clear_resets_text_and_cursor() {
let mut c = ci("hello", 3);
c.clear();
assert_eq!(c.text, "");
assert_eq!(c.cursor, 0);
}
#[test]
fn delete_word_back_removes_word() {
let mut c = ci("hello world", 11);
c.delete_word_back();
assert_eq!(c.text, "hello ");
assert_eq!(c.cursor, 6);
}
#[test]
fn delete_word_back_skips_trailing_spaces() {
let mut c = ci("hello ", 8);
c.delete_word_back();
assert_eq!(c.text, "");
assert_eq!(c.cursor, 0);
}
#[test]
fn delete_word_back_at_start_is_noop() {
let mut c = ci("hello", 0);
c.delete_word_back();
assert_eq!(c.text, "hello");
assert_eq!(c.cursor, 0);
}
#[test]
fn display_cursor_col_counts_correctly() {
let c = ci("hello", 3);
// prefix width 1 (`:`) + 3 chars before cursor = 4
assert_eq!(c.display_cursor_col(1), 4);
}
// ── App::new tests ──────────────────────────────────────────────────────
#[test]
fn app_new_no_file() {
let app = App::new(None, false, None, None).unwrap();
assert!(!app.dirty);
assert!(!app.is_new_file);
assert!(app.filename.is_none());
assert!(!app.editor.is_readonly());
}
#[test]
fn app_new_readonly_flag() {
let app = App::new(None, true, None, None).unwrap();
assert!(app.editor.is_readonly());
}
#[test]
fn app_new_not_found_sets_is_new_file() {
let path = std::path::PathBuf::from("/tmp/hjkl_phase5_nonexistent_abc123.txt");
// Make sure the file doesn't exist.
let _ = std::fs::remove_file(&path);
let app = App::new(Some(path), false, None, None).unwrap();
assert!(app.is_new_file);
assert!(!app.dirty);
}
#[test]
fn app_new_goto_line_clamps() {
// No file, just verify goto_line doesn't panic on line=999 with empty buffer.
let app = App::new(None, false, Some(999), None).unwrap();
let (row, _col) = app.editor.cursor();
// Empty buffer → cursor stays at row 0.
assert_eq!(row, 0);
}
#[test]
fn do_save_readonly_blocked() {
let mut app = App::new(None, true, None, None).unwrap();
app.filename = Some(std::path::PathBuf::from("/tmp/hjkl_phase5_ro_test.txt"));
app.do_save(None);
let msg = app.status_message.unwrap_or_default();
assert!(
msg.contains("E45"),
"expected E45 readonly error, got: {msg}"
);
}
#[test]
fn do_save_no_filename_e32() {
let mut app = App::new(None, false, None, None).unwrap();
app.do_save(None);
let msg = app.status_message.unwrap_or_default();
assert!(msg.contains("E32"), "expected E32, got: {msg}");
}
}