hjkl 0.2.0

Vim-modal terminal editor: standalone TUI built on the hjkl engine.
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
//! Per-frame render functions.
//!
//! [`frame`] is the top-level entry point called from the event loop.
//! It splits the terminal area into a buffer pane + status line row and
//! delegates to [`buffer_pane`] and [`status_line`].

use hjkl_buffer::{BufferView, Gutter};
use hjkl_engine::{Host, Query};
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::Paragraph,
};

use crate::app::{App, STATUS_LINE_HEIGHT};

/// Gutter width formula — matches `Editor::cursor_screen_pos`'s
/// `lnum_width = line_count.to_string().len() + 2`. The renderer must
/// agree with the engine or terminal cursor lands off by one column.
fn gutter_width(line_count: usize) -> u16 {
    line_count.to_string().len() as u16 + 2
}

/// Render one complete frame into `frame`.
pub fn frame(frame: &mut Frame, app: &mut App) {
    let area = frame.area();

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(1), Constraint::Length(STATUS_LINE_HEIGHT)])
        .split(area);

    let buf_area = chunks[0];
    let status_area = chunks[1];

    let gw = gutter_width(app.editor.buffer().line_count() as usize);
    let text_width = buf_area.width.saturating_sub(gw);

    // Publish viewport dims so engine scrolloff math is accurate.
    // `width` is the text-area width (gutter excluded) — `Viewport::ensure_visible`
    // uses it as the horizontal cursor band, and the cursor lives in the text area.
    {
        let vp = app.editor.host_mut().viewport_mut();
        vp.width = text_width;
        vp.height = buf_area.height;
        vp.text_width = text_width;
    }
    // Publish height to the engine's atomic so scrolloff (5-row margin) engages.
    app.editor.set_viewport_height(buf_area.height);

    buffer_pane(frame, app, buf_area, gw);
    status_line(frame, app, status_area);
}

/// Render the buffer pane with line numbers, text, and the cursor.
///
/// The buffer-pane cursor is suppressed when the user is typing in the
/// command line (`:` prompt or `/`/`?` search prompt), because the
/// terminal cursor belongs to the bottom row in those states.
fn buffer_pane(frame: &mut Frame, app: &mut App, area: ratatui::layout::Rect, gutter_width: u16) {
    let gutter = Gutter {
        width: gutter_width,
        style: Style::default().fg(Color::DarkGray),
    };

    let selection = app.editor.buffer_selection();
    let buffer_spans = app.editor.buffer_spans();
    let search_pattern = app.editor.search_state().pattern.as_ref();
    let in_prompt = app.command_input.is_some() || app.editor.search_prompt().is_some();

    // Use a subtle yellow background for search match highlighting (vim's `Search` hl).
    let search_bg = if search_pattern.is_some() {
        Style::default()
            .bg(Color::Rgb(147, 103, 0))
            .fg(Color::White)
    } else {
        Style::default()
    };

    // Bind the style table after the viewport mutation above to avoid a
    // double-borrow on `app.editor` (host_mut() and style_table() both
    // require access to the editor).
    let style_table = app.editor.style_table().to_owned();
    let resolver = move |id: u32| style_table.get(id as usize).copied().unwrap_or_default();

    let view = BufferView {
        buffer: app.editor.buffer(),
        viewport: app.editor.host().viewport(),
        selection,
        resolver: &resolver,
        cursor_line_bg: Style::default(),
        cursor_column_bg: Style::default(),
        selection_bg: Style::default().bg(Color::Blue),
        cursor_style: if in_prompt {
            Style::default().add_modifier(Modifier::REVERSED)
        } else {
            Style::default()
        },
        gutter: Some(gutter),
        search_bg,
        signs: &[],
        conceals: &[],
        spans: buffer_spans,
        search_pattern,
    };

    frame.render_widget(view, area);

    // Suppress the buffer-pane cursor while the user is typing in the
    // command line or search prompt — the cursor belongs to the status row.
    if !in_prompt && let Some((cx, cy)) = app.editor.cursor_screen_pos_in_rect(area) {
        frame.set_cursor_position((cx, cy));
    }
}

/// Render the one-row status line.
///
/// When the user is typing a `:` command or a `/`/`?` search, the status
/// area shows the prompt instead of the normal mode/file/cursor info, and
/// the terminal cursor is moved to the insertion point.
fn status_line(frame: &mut Frame, app: &App, area: ratatui::layout::Rect) {
    let (status, cursor_col) = build_status_line(app, area.width);
    let paragraph = Paragraph::new(status);
    frame.render_widget(paragraph, area);

    // Move the terminal cursor to the insertion point in the prompt row.
    if let Some(col) = cursor_col {
        frame.set_cursor_position((area.x + col, area.y));
    }
}

/// Build the status line text as a ratatui [`Line`].
///
/// Returns `(line, Some(cursor_col))` when a prompt is active so the
/// caller can position the terminal cursor at the insertion point.
///
/// Priority (highest first):
/// 1. Command input (user typing `:cmd`) — shows `:{typed_text}`.
/// 2. Engine search prompt (`/` or `?`) — shows `/{typed_text}`.
/// 3. Status message (ex-command result) — shown until next keypress.
/// 4. Normal mode/filename/cursor line.
fn build_status_line(app: &App, width: u16) -> (Line<'static>, Option<u16>) {
    // ── Command prompt (`:`) ─────────────────────────────────────────────────
    if let Some(ref cmd) = app.command_input {
        let content = format!(":{}", cmd.text);
        // Pad to width so the background fills the row.
        let padded = format!("{content:<width$}", width = width as usize);
        let cursor_col = cmd.display_cursor_col(1); // 1 = width of `:`
        return (
            Line::from(vec![Span::styled(
                padded,
                Style::default().bg(Color::DarkGray).fg(Color::White),
            )]),
            Some(cursor_col),
        );
    }

    // ── Engine search prompt (`/` or `?`) ────────────────────────────────────
    if let Some(sp) = app.editor.search_prompt() {
        let prefix = if sp.forward { '/' } else { '?' };
        let content = format!("{prefix}{}", sp.text);
        let padded = format!("{content:<width$}", width = width as usize);
        // cursor position inside the prompt text (byte-counted in ASCII context)
        let cursor_col = 1u16 + sp.text[..sp.cursor.min(sp.text.len())].chars().count() as u16;
        return (
            Line::from(vec![Span::styled(
                padded,
                Style::default().bg(Color::DarkGray).fg(Color::White),
            )]),
            Some(cursor_col),
        );
    }

    // ── Status message (ex-command result) ──────────────────────────────────
    if let Some(ref msg) = app.status_message {
        let content = format!(" {msg}");
        let padded = format!("{content:<width$}", width = width as usize);
        return (
            Line::from(vec![Span::styled(
                padded,
                Style::default().bg(Color::DarkGray).fg(Color::White),
            )]),
            None,
        );
    }

    // ── Normal status line ───────────────────────────────────────────────────
    let mode = app.mode_label();

    // Dirty marker — `*` when the buffer has unsaved changes.
    let dirty = if app.dirty { "*" } else { " " };

    // Readonly indicator.
    let ro_tag = if app.editor.is_readonly() {
        " [RO]"
    } else {
        ""
    };

    // New-file annotation — shown until the user edits or saves.
    let new_tag = if app.is_new_file { " [New File]" } else { "" };

    let raw_filename: String = app
        .filename
        .as_ref()
        .and_then(|p| p.to_str())
        .unwrap_or("[No Name]")
        .to_owned();

    let (row, col) = app.editor.cursor();
    let line_count = app.editor.buffer().line_count() as usize;
    let pct = ((row + 1) * 100).checked_div(line_count).unwrap_or(0);
    let pos = format!("{}:{}", row + 1, col + 1);
    let pct_str = format!("{pct}%");

    // Right side is fixed width — reserve it first.
    // Format: `pos  pct ` (trailing space).
    let right = format!("{pos}  {pct_str} ");
    // Left prefix before filename: ` MODE  d ` + ro_tag + new_tag.
    let left_prefix = format!(" {mode}  {dirty} ");
    let suffix = format!("{ro_tag}{new_tag}");

    // Available columns for the filename.
    let w = width as usize;
    let reserved = left_prefix.len() + suffix.len() + right.len();
    let avail_for_name = w.saturating_sub(reserved);

    // Truncate filename with leading `…` when it doesn't fit (vim style).
    let filename: String = if raw_filename.len() <= avail_for_name {
        raw_filename.clone()
    } else if avail_for_name <= 1 {
        String::new()
    } else {
        let keep = avail_for_name.saturating_sub(1); // 1 char for `…`
        let start = raw_filename.len().saturating_sub(keep);
        format!("\u{2026}{}", &raw_filename[start..])
    };

    // Left side: ` MODE  dirty filename[RO][New File]`
    let left = format!("{left_prefix}{filename}{suffix}");

    // Pad the centre spacer so left + spacer + right == width.
    let used = left.len() + right.len();
    let pad_count = w.saturating_sub(used);
    let spacer: String = " ".repeat(pad_count);

    let content = format!("{left}{spacer}{right}");

    (
        Line::from(vec![Span::styled(
            content,
            Style::default().bg(Color::DarkGray).fg(Color::White),
        )]),
        None,
    )
}

/// Format the status line as a plain string (unit-test helper).
///
/// `readonly` and `is_new_file` mirror the app state flags.
/// Filename is truncated with `…` when necessary.
#[allow(dead_code)]
pub fn format_status_line(
    mode: &str,
    filename: &str,
    dirty: bool,
    row: usize,
    col: usize,
    total_lines: usize,
    width: u16,
) -> String {
    format_status_line_full(
        mode,
        filename,
        dirty,
        false,
        false,
        row,
        col,
        total_lines,
        width,
    )
}

/// Full status line formatter with readonly + new-file flags.
#[allow(clippy::too_many_arguments)]
pub fn format_status_line_full(
    mode: &str,
    filename: &str,
    dirty: bool,
    readonly: bool,
    is_new_file: bool,
    row: usize,
    col: usize,
    total_lines: usize,
    width: u16,
) -> String {
    let dirty_marker = if dirty { "*" } else { " " };
    let ro_tag = if readonly { " [RO]" } else { "" };
    let new_tag = if is_new_file { " [New File]" } else { "" };
    let pct = ((row + 1) * 100).checked_div(total_lines).unwrap_or(0);
    let pos = format!("{}:{}", row + 1, col + 1);
    let pct_str = format!("{pct}%");
    let right = format!("{pos}  {pct_str} ");
    let left_prefix = format!(" {mode}  {dirty_marker} ");
    let suffix = format!("{ro_tag}{new_tag}");
    let w = width as usize;
    let reserved = left_prefix.len() + suffix.len() + right.len();
    let avail_for_name = w.saturating_sub(reserved);
    let truncated: String = if filename.len() <= avail_for_name {
        filename.to_string()
    } else if avail_for_name <= 1 {
        String::new()
    } else {
        let keep = avail_for_name.saturating_sub(1);
        let start = filename.len().saturating_sub(keep);
        format!("\u{2026}{}", &filename[start..])
    };
    let left = format!("{left_prefix}{truncated}{suffix}");
    let used = left.len() + right.len();
    let pad_count = w.saturating_sub(used);
    let spacer = " ".repeat(pad_count);
    format!("{left}{spacer}{right}")
}

/// Format the write-success status message. Used in tests.
#[cfg(test)]
pub fn format_write_message(path: &str, lines: usize, bytes: usize) -> String {
    format!("\"{}\" {}L, {}B written", path, lines, bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn status_line_normal_mode_no_name() {
        let s = format_status_line("NORMAL", "[No Name]", false, 0, 0, 1, 60);
        assert!(s.contains("NORMAL"));
        assert!(s.contains("[No Name]"));
        assert!(s.contains("1:1"));
        assert!(s.contains("100%"));
    }

    #[test]
    fn status_line_dirty_marker() {
        let clean = format_status_line("NORMAL", "foo.txt", false, 0, 0, 1, 60);
        let dirty = format_status_line("NORMAL", "foo.txt", true, 0, 0, 1, 60);
        assert!(clean.contains(" [No Name]") || clean.contains(" foo.txt"));
        // dirty marker is `*` in dirty, ` ` in clean
        let dirty_idx = dirty.find('*');
        assert!(dirty_idx.is_some(), "dirty status should contain '*'");
        let clean_contains_star = clean.contains('*');
        assert!(!clean_contains_star, "clean status should not contain '*'");
    }

    #[test]
    fn status_line_percentage() {
        let s = format_status_line("NORMAL", "f.txt", false, 4, 0, 10, 60);
        // row 4 of 10 = 50%
        assert!(s.contains("50%"));
    }

    #[test]
    fn status_line_fits_width() {
        let width: u16 = 40;
        let s = format_status_line("INSERT", "myfile.rs", true, 0, 0, 100, width);
        assert_eq!(s.len(), width as usize);
    }

    #[test]
    fn write_message_format() {
        let msg = format_write_message("/tmp/foo.txt", 10, 128);
        assert_eq!(msg, "\"/tmp/foo.txt\" 10L, 128B written");
    }

    #[test]
    fn status_line_readonly_tag() {
        let s = format_status_line_full("NORMAL", "foo.txt", false, true, false, 0, 0, 1, 80);
        assert!(s.contains("[RO]"), "readonly tag must appear");
    }

    #[test]
    fn status_line_new_file_tag() {
        let s = format_status_line_full("NORMAL", "newfile.txt", false, false, true, 0, 0, 1, 80);
        assert!(s.contains("[New File]"), "new-file tag must appear");
    }

    #[test]
    fn status_line_truncates_long_filename() {
        // Very narrow terminal — filename must be truncated.
        let long = "some/very/long/path/to/a/deeply/nested/file.rs";
        let s = format_status_line_full("NORMAL", long, false, false, false, 0, 0, 1, 30);
        // Truncated filename starts with `…`
        assert!(
            s.contains('\u{2026}'),
            "truncated filename must start with …"
        );
    }

    #[test]
    fn status_line_arg_parsing_plus_n() {
        // Smoke test: +5 → goto_line=Some(5). Tested via parse logic in main.
        // Here we verify the status-line can handle being on line 5.
        let s = format_status_line("NORMAL", "file.txt", false, 4, 0, 10, 60);
        // row 4 (0-based) → 5 in display
        assert!(s.contains("5:1"));
    }
}