strop-editor 0.3.0

strop — a modal text editor in Rust: see the cut before you make it
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
//! Diff surface rendering (0010 §4/§5): gutters, row backgrounds,
//! structural rows — plus the 0011 left-margin columns (blame gutter,
//! commit file sidebar). All decoration comes from typed hunk data —
//! never from sniffing `+`/`-` in the text. Anatomy borrowed from
//! tuicr (`[sign][old][new][content]`, quiet bands for structural
//! rows), tuned to the strop palette.

use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};

use crate::editor::{Editor, Surface};
use strop_git::memory::ChangedFile;
use strop_git::{DiffLine, LineOrigin};

use super::{ACCENT, MUTED, SELECT_BG, TEXT};

pub(crate) const ADD_FG: Color = Color::Rgb(0xa9, 0xc4, 0x7c);
pub(crate) const DEL_FG: Color = Color::Rgb(0xe8, 0x67, 0x7a);
/// Quiet full-row backgrounds — a visible scan signal that doesn't
/// shout (tuicr's two-tier idea: quiet under content, loud markers).
const ADD_BG: Color = Color::Rgb(0x1b, 0x26, 0x20);
const DEL_BG: Color = Color::Rgb(0x2a, 0x1d, 0x20);
/// Structural rows (stats, hunk headers) sit on a band, not an accent.
const BAND_BG: Color = Color::Rgb(0x22, 0x24, 0x2e);

pub(crate) fn origin_fg(origin: LineOrigin) -> Color {
    match origin {
        LineOrigin::Addition => ADD_FG,
        LineOrigin::Deletion => DEL_FG,
        LineOrigin::Context => TEXT,
    }
}

/// Full-row background for add/del rows; context rows stay on BASE.
pub(crate) fn origin_bg(origin: LineOrigin) -> Option<Color> {
    match origin {
        LineOrigin::Addition => Some(ADD_BG),
        LineOrigin::Deletion => Some(DEL_BG),
        LineOrigin::Context => None,
    }
}

/// What row `row` of a Diff surface is. Row 0 is the stats line; then
/// per hunk a header row followed by its content rows.
pub(crate) enum DiffRow<'a> {
    Stats,
    HunkHeader,
    Line(&'a DiffLine),
}

pub(crate) fn diff_row<'a>(surface: Option<&'a Surface>, row: usize) -> Option<DiffRow<'a>> {
    let Some(Surface::Diff { hunks, .. }) = surface else {
        return None;
    };
    if row == 0 {
        return Some(DiffRow::Stats);
    }
    let mut row = row - 1;
    for hunk in hunks {
        if row == 0 {
            return Some(DiffRow::HunkHeader);
        }
        row -= 1;
        if row < hunk.lines.len() {
            return Some(DiffRow::Line(&hunk.lines[row]));
        }
        row -= hunk.lines.len();
    }
    None
}

/// Gutter width for a surface's buffer: the diff gutter widens to fit
/// both sides' numbers (min 3 digits each); everything else is the
/// standard sign+number gutter.
pub(crate) fn gutter_width(surface: Option<&Surface>) -> usize {
    let Some(Surface::Diff { hunks, .. }) = surface else {
        return super::buffer::GUTTER as usize;
    };
    let max_lineno = hunks
        .iter()
        .flat_map(|h| &h.lines)
        .flat_map(|l| [l.old_lineno, l.new_lineno])
        .flatten()
        .max()
        .unwrap_or(0);
    let digits = max_lineno.to_string().len().max(3);
    // sign(1) + old(digits) + space + new(digits) + space
    1 + digits + 1 + digits + 1
}

/// The diff gutter for one content row: origin marker + both sides'
/// numbers, right-aligned, absent side blank (never `0`). The cursor
/// row's numbers light up like the standard gutter's do.
pub(crate) fn diff_gutter(
    line: &DiffLine,
    is_cursor_row: bool,
    digits: usize,
) -> Vec<Span<'static>> {
    let (marker, color) = match line.origin {
        LineOrigin::Addition => ("", ADD_FG),
        LineOrigin::Deletion => ("", DEL_FG),
        LineOrigin::Context => (" ", MUTED),
    };
    let number = |n: Option<usize>| {
        let style = if is_cursor_row {
            Style::default().fg(ACCENT)
        } else {
            Style::default().fg(MUTED)
        };
        let text = match n {
            Some(n) => format!("{:>width$}", n, width = digits),
            None => " ".repeat(digits),
        };
        Span::styled(text, style)
    };
    vec![
        Span::styled(
            marker,
            Style::default().fg(color).add_modifier(Modifier::BOLD),
        ),
        number(line.old_lineno),
        Span::styled(" ", Style::default()),
        number(line.new_lineno),
        Span::styled(" ", Style::default()),
    ]
}

/// Stats and hunk-header rows: a quiet band across the full width —
/// label/stats left, nothing loud (0010 §4). `width` pads the band
/// past the text so it reads as a full row.
pub(crate) fn structural_row(surface: &Surface, row: usize, width: u16) -> Line<'static> {
    let mut line = structural_row_inner(surface, row);
    let used: usize = line.spans.iter().map(|s| s.content.len()).sum();
    let pad = (width as usize).saturating_sub(used + 1);
    if pad > 0 {
        line.spans
            .push(Span::styled(" ".repeat(pad), Style::default().bg(BAND_BG)));
    }
    line
}

fn structural_row_inner(surface: &Surface, row: usize) -> Line<'static> {
    match (surface, row) {
        (
            Surface::Diff {
                label,
                added,
                deleted,
                ..
            },
            0,
        ) => Line::from(vec![
            Span::styled(
                format!(" {label}"),
                Style::default().fg(TEXT).add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                format!(" +{added} "),
                Style::default().fg(ADD_FG).add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                format!("-{deleted}"),
                Style::default().fg(DEL_FG).add_modifier(Modifier::BOLD),
            ),
        ])
        .style(Style::default().bg(BAND_BG)),
        (Surface::Diff { hunks, .. }, _) => match hunk_header_at(hunks, row) {
            Some(header) => Line::from(Span::styled(
                format!(" {header}"),
                Style::default().fg(MUTED),
            ))
            .style(Style::default().bg(BAND_BG)),
            None => Line::default(),
        },
        _ => Line::default(),
    }
}

/// The hunk whose header sits at `row` (row 1 + hunk offsets).
fn hunk_header_at(hunks: &[strop_git::Hunk], row: usize) -> Option<String> {
    let mut row = row.checked_sub(1)?;
    for hunk in hunks {
        if row == 0 {
            return Some(hunk.header());
        }
        row -= 1;
        if row < hunk.lines.len() {
            return None;
        }
        row -= hunk.lines.len();
    }
    None
}

/// Commit-log and changed-files rows, decorated from their typed data
/// (0010 §5): graph runes dim, sha accent; paths with right-aligned
/// colored stats. Returns None for rows that render as normal text.
pub(crate) fn surface_content_spans(
    surface: Option<&Surface>,
    line_idx: usize,
    width: u16,
) -> Option<Vec<Span<'static>>> {
    match surface? {
        Surface::CommitLog { rows, .. } => {
            let row = rows.get(line_idx)?;
            Some(log_row_spans(&row.text))
        }
        Surface::ChangedFiles { sha, files, .. } => match line_idx {
            0 => Some(vec![
                Span::styled("commit ", Style::default().fg(MUTED)),
                Span::styled(
                    sha.chars().take(10).collect::<String>(),
                    Style::default().fg(ACCENT),
                ),
            ]),
            1 => Some(vec![]),
            _ => {
                let file = files.get(line_idx - 2)?;
                Some(file_row_spans(
                    &file.path.display().to_string(),
                    file.added,
                    file.deleted,
                    width,
                ))
            }
        },
        _ => None,
    }
}

/// `* 51b63a8 t · 35 seconds ago · subject` → lane-colored graph runes,
/// sha accent bold, `·` separators dim, the rest text.
fn log_row_spans(text: &str) -> Vec<Span<'static>> {
    let graph_len = text
        .chars()
        .take_while(|c| "*|/\\<>-_ ".contains(*c))
        .count();
    let rest = &text[graph_len..];
    let sha_len = rest
        .chars()
        .take_while(|c| c.is_ascii_hexdigit())
        .map(|c| c.len_utf8())
        .sum::<usize>();
    let mut spans = graph_spans(&text[..graph_len]);

    if sha_len > 0 {
        spans.push(Span::styled(
            rest[..sha_len].to_string(),
            Style::default().fg(ACCENT).add_modifier(Modifier::BOLD),
        ));
    }
    for (i, part) in rest[sha_len..].split(" · ").enumerate() {
        if i > 0 {
            spans.push(Span::styled(" · ", Style::default().fg(MUTED)));
        }
        if !part.is_empty() {
            spans.push(Span::styled(part.to_string(), Style::default().fg(TEXT)));
        }
    }
    spans
}

/// Lane-colored graph art: each two-column lane cycles the palette, the
/// commit node `*` is bold in its lane's color (gitui/lazygit lesson —
/// lane color is how the eye tracks a branch through merges).
fn graph_spans(prefix: &str) -> Vec<Span<'static>> {
    const LANES: [Color; 6] = [
        ACCENT,                       // amber
        Color::Rgb(0x9e, 0xce, 0x6a), // green
        Color::Rgb(0x7a, 0xa2, 0xf7), // blue
        Color::Rgb(0xbb, 0x9a, 0xf7), // purple
        Color::Rgb(0x7d, 0xcf, 0xff), // cyan
        Color::Rgb(0xe0, 0xaf, 0x68), // yellow
    ];
    prefix
        .chars()
        .enumerate()
        .map(|(i, c)| {
            if c == ' ' {
                return Span::styled(" ", Style::default());
            }
            let color = LANES[(i / 2) % LANES.len()];
            let style = if c == '*' {
                Style::default().fg(color).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(color)
            };
            Span::styled(c.to_string(), style)
        })
        .collect()
}
/// path left, ` +N -M` right-aligned to the row width.
fn file_row_spans(path: &str, added: usize, deleted: usize, width: u16) -> Vec<Span<'static>> {
    let stats_len = added.to_string().len() + deleted.to_string().len() + 4;
    let gutter = gutter_width(None);
    let room = (width as usize).saturating_sub(gutter + 1 + stats_len);
    let shown: String = path.chars().take(room).collect();
    let pad = room.saturating_sub(path.chars().count());
    vec![
        Span::styled(format!(" {shown}"), Style::default().fg(TEXT)),
        Span::styled(" ".repeat(pad + 1), Style::default()),
        Span::styled(
            format!("+{added} "),
            Style::default().fg(ADD_FG).add_modifier(Modifier::BOLD),
        ),
        Span::styled(
            format!("-{deleted}"),
            Style::default().fg(DEL_FG).add_modifier(Modifier::BOLD),
        ),
    ]
}

// ---- left-margin columns (0011) -----------------------------------------

/// Blame gutter width: `sha˟7 author˟9 age˟3` + separators.
pub(crate) const BLAME_W: usize = 22;
/// Pane-divider color — the sidebar's rule matches it.
const RULE: Color = Color::Rgb(0x3a, 0x3d, 0x4d);
/// Younger than this counts as "recent" → accent (0011 §3).
const RECENT_SECS: i64 = 30 * 86400;

/// The blame cell for one buffer line: `sha7 author9 age3`, muted for
/// old commits, accent for recent ones and uncommitted lines
/// (`0000000 you now`).
pub(crate) fn blame_spans(line: &strop_git::memory::BlameLine) -> Span<'static> {
    let uncommitted = line.is_uncommitted();
    let recent = line.ts > 0 && unix_now() - line.ts < RECENT_SECS;
    let fg = if uncommitted || recent { ACCENT } else { MUTED };
    let sha: String = if uncommitted {
        "0".repeat(7)
    } else {
        line.sha.chars().take(7).collect()
    };
    let author = ellipsize(&line.author, 9);
    let age: String = line.age.chars().take(3).collect();
    Span::styled(
        format!("{sha} {author:<9} {age:>3} "),
        Style::default().fg(fg),
    )
}

/// A blank blame cell (filler rows past the buffer end).
pub(crate) fn blame_blank() -> Span<'static> {
    Span::styled(" ".repeat(BLAME_W), Style::default())
}
/// Sidebar width fits the commit's longest path (clamped 12–24) — a
/// two-file commit shouldn't pay a 28-column pane.
pub(crate) fn sidebar_width(files: &[ChangedFile]) -> usize {
    let longest = files
        .iter()
        .map(|f| f.path.display().to_string().chars().count())
        .max()
        .unwrap_or(0);
    (longest + 2).clamp(12, 24)
}

/// One sidebar row: the commit's changed files, current one marked `▌`
/// on the selection background (house style 0003 §5.3), plus the dim
/// rule that divides sidebar from content. Rows past the file list
/// stay blank so the column reads as one surface.
pub(crate) fn sidebar_spans(
    files: &[ChangedFile],
    current: &str,
    row: usize,
) -> Vec<Span<'static>> {
    let w = sidebar_width(files);
    let cell = match files.get(row) {
        Some(f) => {
            let path = f.path.display().to_string();
            if path == current {
                let shown = ellipsize(&path, w - 2);
                let pad = w - 1 - shown.chars().count();
                vec![
                    Span::styled(
                        format!("{shown}"),
                        Style::default().fg(ACCENT).bg(SELECT_BG),
                    ),
                    Span::styled(" ".repeat(pad), Style::default().bg(SELECT_BG)),
                ]
            } else {
                let shown = ellipsize(&path, w - 1);
                let pad = w - shown.chars().count();
                vec![
                    Span::styled(format!(" {shown}"), Style::default().fg(TEXT)),
                    Span::styled(" ".repeat(pad), Style::default()),
                ]
            }
        }
        None => vec![Span::styled(" ".repeat(w), Style::default())],
    };
    let mut spans = cell;
    spans.push(Span::styled("", Style::default().fg(RULE)));
    spans
}

/// Total left inset before a pane's content: file sidebar + blame
/// column + the surface's number gutter. Cursor placement and the
/// inactive-pane caret both derive from here — one composition, no
/// per-surface drift (0011 §3/§4).
pub(crate) fn left_inset(editor: &Editor, buffer: usize) -> usize {
    let surface = editor.surfaces.get(buffer).and_then(|s| s.as_ref());
    let mut inset = gutter_width(surface);
    if editor.blame_gutter_for(buffer).is_some() {
        inset += BLAME_W;
    }
    if let Some(Surface::Diff {
        commit: Some(cf), ..
    }) = surface
    {
        inset += sidebar_width(&cf.files) + 1;
    }
    inset
}

fn unix_now() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

/// `s` clipped to `n` chars with a trailing `…` when it had more.
fn ellipsize(s: &str, n: usize) -> String {
    if s.chars().count() <= n {
        s.to_string()
    } else {
        let cut: String = s.chars().take(n - 1).collect();
        format!("{cut}")
    }
}

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

    #[test]
    fn graph_lanes_get_distinct_colors() {
        // a two-lane merge row: `*` in lane 0, `|` in lane 1
        let spans = log_row_spans("* | 3a9eeec t · 1s ago · merge");
        let star = spans.iter().find(|s| s.content == "*").unwrap();
        let bar = spans.iter().find(|s| s.content == "|").unwrap();
        assert_ne!(star.style.fg, bar.style.fg, "lanes must differ");
        assert_eq!(star.style.fg, Some(ACCENT));
        assert!(spans
            .iter()
            .any(|s| s.content == "3a9eeec" && s.style.add_modifier.contains(Modifier::BOLD)));
    }
}