mlux 1.7.0

A rich Markdown viewer for modern terminals
Documentation
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
//! Search functionality: grep Markdown source and display results as a picker.

use crossterm::{
    QueueableCommand, cursor,
    style::{self, Stylize},
    terminal::{Clear, ClearType},
};
use regex::RegexBuilder;
use std::io::{self, Write, stdout};

use super::Effect;
use super::ViewerMode;
use super::input::SearchAction;
use super::state::{Layout, visual_line_offset};
use crate::tile::VisualLine;

/// A single search match within the Markdown source.
#[derive(Debug, Clone)]
pub(super) struct SearchMatch {
    /// 1-based Markdown line number.
    pub md_line: usize,
    /// Index into the `visual_lines` array (for jumping).
    pub visual_line_idx: usize,
    /// The full text of the matching line.
    pub context: String,
    /// Byte offset of match start within `context`.
    pub col_start: usize,
    /// Byte offset of match end within `context`.
    pub col_end: usize,
}

/// Mutable search state while in search mode.
pub(super) struct SearchState {
    pub query: String,
    pub matches: Vec<SearchMatch>,
    pub selected: usize,
    pub scroll_offset: usize,
    pub pattern_valid: bool,
}

impl SearchState {
    pub(super) fn new() -> Self {
        Self {
            query: String::new(),
            matches: Vec::new(),
            selected: 0,
            scroll_offset: 0,
            pattern_valid: true,
        }
    }
}

/// Persisted search results for n/N navigation in normal mode.
pub(super) struct LastSearch {
    pub matches: Vec<SearchMatch>,
    pub current_idx: usize,
}

impl LastSearch {
    /// Create from a completed SearchState, using the selected match as current.
    pub(super) fn from_search_state(ss: &SearchState) -> Self {
        Self {
            matches: ss.matches.clone(),
            current_idx: ss.selected,
        }
    }

    /// Advance to the next match. Wraps around.
    pub(super) fn advance_next(&mut self) {
        if !self.matches.is_empty() {
            self.current_idx = (self.current_idx + 1) % self.matches.len();
        }
    }

    /// Advance to the previous match. Wraps around.
    pub(super) fn advance_prev(&mut self) {
        if !self.matches.is_empty() {
            if self.current_idx == 0 {
                self.current_idx = self.matches.len() - 1;
            } else {
                self.current_idx -= 1;
            }
        }
    }

    /// Get the visual_line_idx of the current match.
    pub(super) fn current_visual_line_idx(&self) -> Option<usize> {
        self.matches
            .get(self.current_idx)
            .map(|m| m.visual_line_idx)
    }
}

/// Find the visual line index corresponding to a 1-based Markdown line number.
fn find_visual_line(visual_lines: &[VisualLine], md_line: usize) -> Option<usize> {
    visual_lines.iter().position(|vl| {
        vl.md_line_range
            .is_some_and(|(s, e)| md_line >= s && md_line <= e)
    })
}

/// Search the Markdown source for lines matching `query` as a regular expression.
///
/// Uses smartcase: if `query` is all lowercase, search is case-insensitive;
/// otherwise it's case-sensitive.
///
/// Returns `(matches, pattern_valid)`. On invalid regex, returns empty matches
/// with `pattern_valid = false`.
pub(super) fn grep_markdown(
    query: &str,
    markdown: &str,
    visual_lines: &[VisualLine],
) -> (Vec<SearchMatch>, bool) {
    if query.is_empty() {
        return (Vec::new(), true);
    }

    let smartcase = query.chars().all(|c| !c.is_uppercase());
    let re = match RegexBuilder::new(query).case_insensitive(smartcase).build() {
        Ok(re) => re,
        Err(_) => return (Vec::new(), false),
    };

    let mut matches = Vec::new();

    for (line_idx, line_text) in markdown.lines().enumerate() {
        let md_line = line_idx + 1; // 1-based

        if let Some(m) = re.find(line_text)
            && let Some(vl_idx) = find_visual_line(visual_lines, md_line)
        {
            matches.push(SearchMatch {
                md_line,
                visual_line_idx: vl_idx,
                context: line_text.to_string(),
                col_start: m.start(),
                col_end: m.end(),
            });
        }
    }

    (matches, true)
}

/// Truncate a string to at most `max_bytes`, respecting UTF-8 char boundaries.
fn truncate_str(s: &str, max_bytes: usize) -> &str {
    if s.len() <= max_bytes {
        return s;
    }
    &s[..s.floor_char_boundary(max_bytes)]
}

/// Draw the search screen (replaces tile images).
///
/// Layout:
///   Row 0: `/query_` prompt
///   Row 1..N: result list (scrolled, with selection highlight)
///   Last row: status line with match count and key hints
pub(super) fn draw_search_screen(
    layout: &Layout,
    query: &str,
    matches: &[SearchMatch],
    selected: usize,
    scroll_offset: usize,
    pattern_valid: bool,
) -> io::Result<()> {
    let mut out = stdout();
    out.queue(Clear(ClearType::All))?;

    let total_cols = (layout.sidebar_cols + layout.image_cols) as usize;

    // Row 0: search prompt
    out.queue(cursor::MoveTo(0, 0))?;
    let prompt = format!("/{query}_");
    let prompt_display = truncate_str(&prompt, total_cols);
    write!(out, "{}", prompt_display.white().bold())?;

    // Result list: rows 1 .. status_row-1
    let list_start_row: u16 = 1;
    let list_end_row = layout.status_row; // exclusive
    let visible_count = (list_end_row - list_start_row) as usize;

    for i in 0..visible_count {
        let match_idx = scroll_offset + i;
        let row = list_start_row + i as u16;
        out.queue(cursor::MoveTo(0, row))?;

        if match_idx >= matches.len() {
            // Empty row
            write!(out, "{:width$}", "", width = total_cols)?;
            continue;
        }

        let m = &matches[match_idx];
        let is_selected = match_idx == selected;

        // Format: "  {line_num}: {context}" with match highlight
        let line_prefix = format!("  {:>4}: ", m.md_line);
        let prefix_len = line_prefix.len();

        // Truncate context to fit, respecting UTF-8 boundaries
        let max_context = total_cols.saturating_sub(prefix_len);
        let context = truncate_str(&m.context, max_context);

        // Clamp highlight offsets to truncated context length and char boundaries
        let col_start = context.floor_char_boundary(m.col_start.min(context.len()));
        let col_end = context.floor_char_boundary(m.col_end.min(context.len()));

        let before = &context[..col_start];
        let highlight = &context[col_start..col_end];
        let after = &context[col_end..];

        if is_selected {
            // Selected line: full line on blue background
            write!(out, "{}", line_prefix.on_dark_blue().white())?;
            write!(out, "{}", before.on_dark_blue().white())?;
            write!(out, "{}", highlight.on_dark_blue().yellow().bold())?;
            // Pad remaining
            let remaining = total_cols.saturating_sub(prefix_len + context.len());
            write!(
                out,
                "{}",
                format!("{after}{:remaining$}", "").on_dark_blue().white()
            )?;
        } else {
            // Normal line: highlight match in reverse
            write!(out, "{}", line_prefix.dark_grey())?;
            write!(out, "{before}")?;
            write!(out, "{}", highlight.yellow().bold())?;
            write!(out, "{after}")?;
        }
    }

    // Status line
    out.queue(cursor::MoveTo(0, layout.status_row))?;
    let status = if !pattern_valid {
        " invalid pattern | Esc:cancel".to_string()
    } else {
        format!(
            " {} matches | Enter:jump  Esc:cancel  j/k:select",
            matches.len()
        )
    };
    let padded = format!("{:<width$}", status, width = total_cols);
    if !pattern_valid {
        write!(out, "{}", padded.on_dark_red().white())?;
    } else {
        write!(out, "{}", padded.on_dark_grey().white())?;
    }
    out.queue(style::ResetColor)?;

    out.flush()
}

pub(super) fn handle(
    action: SearchAction,
    ss: &mut SearchState,
    markdown: &str,
    visual_lines: &[VisualLine],
    visible_count: usize,
    max_scroll: u32,
) -> Vec<Effect> {
    match action {
        SearchAction::Type(c) => {
            ss.query.push(c);
            update_grep(ss, markdown, visual_lines);
            vec![Effect::RedrawSearch]
        }
        SearchAction::Backspace => {
            ss.query.pop();
            update_grep(ss, markdown, visual_lines);
            vec![Effect::RedrawSearch]
        }
        SearchAction::SelectNext => {
            if !ss.matches.is_empty() {
                ss.selected = (ss.selected + 1).min(ss.matches.len() - 1);
                if ss.selected >= ss.scroll_offset + visible_count {
                    ss.scroll_offset = ss.selected - visible_count + 1;
                }
            }
            vec![Effect::RedrawSearch]
        }
        SearchAction::SelectPrev => {
            if !ss.matches.is_empty() {
                ss.selected = ss.selected.saturating_sub(1);
                if ss.selected < ss.scroll_offset {
                    ss.scroll_offset = ss.selected;
                }
            }
            vec![Effect::RedrawSearch]
        }
        SearchAction::Confirm => {
            if ss.matches.is_empty() {
                return vec![Effect::SetMode(ViewerMode::Normal), Effect::MarkDirty];
            }
            let vl_idx = ss.matches[ss.selected].visual_line_idx;
            let last = LastSearch::from_search_state(ss);
            let line_num = (vl_idx + 1) as u32;
            let y = visual_line_offset(visual_lines, max_scroll, line_num);
            let flash = format!("match {}/{}", ss.selected + 1, ss.matches.len());
            vec![
                Effect::SetLastSearch(last),
                Effect::ScrollTo(y),
                Effect::Flash(flash),
                Effect::SetMode(ViewerMode::Normal),
            ]
        }
        SearchAction::Cancel => vec![Effect::SetMode(ViewerMode::Normal), Effect::MarkDirty],
    }
}

/// Re-run grep on the current query and reset selection.
fn update_grep(ss: &mut SearchState, markdown: &str, visual_lines: &[VisualLine]) {
    let (matches, valid) = grep_markdown(&ss.query, markdown, visual_lines);
    ss.matches = matches;
    ss.pattern_valid = valid;
    ss.selected = 0;
    ss.scroll_offset = 0;
}

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

    /// Helper: build visual_lines where line N maps to md_line_range (N, N).
    fn make_visual_lines(n: usize) -> Vec<VisualLine> {
        (1..=n)
            .map(|i| VisualLine {
                y_pt: 0.0,
                y_px: 0,
                md_line_range: Some((i, i)),
                md_line_exact: None,
            })
            .collect()
    }

    #[test]
    fn regex_heading_pattern() {
        let md = "# Title\nsome text\n## Subtitle\nmore text";
        let vl = make_visual_lines(4);
        let (matches, valid) = grep_markdown("^#", md, &vl);
        assert!(valid);
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].md_line, 1);
        assert_eq!(matches[1].md_line, 3);
    }

    #[test]
    fn smartcase_all_lower_is_insensitive() {
        let md = "Hello World\nhello world\nHELLO";
        let vl = make_visual_lines(3);
        let (matches, valid) = grep_markdown("hello", md, &vl);
        assert!(valid);
        assert_eq!(matches.len(), 3);
    }

    #[test]
    fn smartcase_upper_is_sensitive() {
        let md = "Hello World\nhello world\nHELLO";
        let vl = make_visual_lines(3);
        let (matches, valid) = grep_markdown("Hello", md, &vl);
        assert!(valid);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].md_line, 1);
    }

    #[test]
    fn invalid_pattern_returns_empty() {
        let md = "some [text] here";
        let vl = make_visual_lines(1);
        let (matches, valid) = grep_markdown("[", md, &vl);
        assert!(!valid);
        assert!(matches.is_empty());
    }

    #[test]
    fn literal_string_still_works() {
        let md = "foo bar baz\nqux foo quux";
        let vl = make_visual_lines(2);
        let (matches, valid) = grep_markdown("foo", md, &vl);
        assert!(valid);
        assert_eq!(matches.len(), 2);
        // Check highlight positions
        assert_eq!(matches[0].col_start, 0);
        assert_eq!(matches[0].col_end, 3);
        assert_eq!(matches[1].col_start, 4);
        assert_eq!(matches[1].col_end, 7);
    }

    #[test]
    fn empty_query_returns_empty() {
        let md = "anything";
        let vl = make_visual_lines(1);
        let (matches, valid) = grep_markdown("", md, &vl);
        assert!(valid);
        assert!(matches.is_empty());
    }

    // --- handle() tests (pure, no I/O) ---

    #[test]
    fn handle_type_updates_query_and_redraws() {
        let md = "hello world\nfoo bar";
        let vl = make_visual_lines(2);
        let mut ss = SearchState::new();
        let effects = handle(SearchAction::Type('h'), &mut ss, md, &vl, 20, 1000);
        assert_eq!(ss.query, "h");
        assert!(!ss.matches.is_empty()); // "hello" matches
        assert!(matches!(effects[0], Effect::RedrawSearch));
    }

    #[test]
    fn handle_backspace_pops_query() {
        let md = "hello";
        let vl = make_visual_lines(1);
        let mut ss = SearchState::new();
        ss.query = "he".into();
        let effects = handle(SearchAction::Backspace, &mut ss, md, &vl, 20, 1000);
        assert_eq!(ss.query, "h");
        assert!(matches!(effects[0], Effect::RedrawSearch));
    }

    #[test]
    fn handle_select_next_moves_selection() {
        let md = "aaa\naaa\naaa";
        let vl = make_visual_lines(3);
        let mut ss = SearchState::new();
        // Pre-populate with matches
        handle(SearchAction::Type('a'), &mut ss, md, &vl, 20, 1000);
        assert_eq!(ss.selected, 0);
        handle(SearchAction::SelectNext, &mut ss, md, &vl, 20, 1000);
        assert_eq!(ss.selected, 1);
    }

    #[test]
    fn handle_select_prev_clamps_at_zero() {
        let md = "aaa\naaa";
        let vl = make_visual_lines(2);
        let mut ss = SearchState::new();
        handle(SearchAction::Type('a'), &mut ss, md, &vl, 20, 1000);
        assert_eq!(ss.selected, 0);
        handle(SearchAction::SelectPrev, &mut ss, md, &vl, 20, 1000);
        assert_eq!(ss.selected, 0);
    }

    #[test]
    fn handle_confirm_sets_last_search_and_scrolls() {
        let md = "hello world";
        let vl = make_visual_lines(1);
        let mut ss = SearchState::new();
        handle(SearchAction::Type('h'), &mut ss, md, &vl, 20, 1000);
        let effects = handle(SearchAction::Confirm, &mut ss, md, &vl, 20, 1000);
        assert!(
            effects
                .iter()
                .any(|e| matches!(e, Effect::SetLastSearch(_)))
        );
        assert!(effects.iter().any(|e| matches!(e, Effect::ScrollTo(_))));
        assert!(
            effects
                .iter()
                .any(|e| matches!(e, Effect::SetMode(ViewerMode::Normal)))
        );
    }

    #[test]
    fn handle_confirm_empty_returns_normal() {
        let md = "hello";
        let vl = make_visual_lines(1);
        let mut ss = SearchState::new();
        // No matches (empty query)
        let effects = handle(SearchAction::Confirm, &mut ss, md, &vl, 20, 1000);
        assert!(
            effects
                .iter()
                .any(|e| matches!(e, Effect::SetMode(ViewerMode::Normal)))
        );
        assert!(effects.iter().any(|e| matches!(e, Effect::MarkDirty)));
    }

    #[test]
    fn handle_cancel_returns_normal() {
        let md = "hello";
        let vl = make_visual_lines(1);
        let mut ss = SearchState::new();
        let effects = handle(SearchAction::Cancel, &mut ss, md, &vl, 20, 1000);
        assert!(
            effects
                .iter()
                .any(|e| matches!(e, Effect::SetMode(ViewerMode::Normal)))
        );
        assert!(effects.iter().any(|e| matches!(e, Effect::MarkDirty)));
    }

    #[test]
    fn handle_select_next_scrolls_when_past_visible() {
        let md = "a\nb\nc\nd\ne";
        let vl = make_visual_lines(5);
        let mut ss = SearchState::new();
        // Search with visible_count = 2 (only 2 rows visible)
        handle(SearchAction::Type('a'), &mut ss, md, &vl, 2, 1000);
        // This matches only line 1, so we need a query that matches all
        ss.query.clear();
        handle(SearchAction::Type('.'), &mut ss, md, &vl, 2, 1000);
        assert_eq!(ss.matches.len(), 5);
        handle(SearchAction::SelectNext, &mut ss, md, &vl, 2, 1000);
        handle(SearchAction::SelectNext, &mut ss, md, &vl, 2, 1000);
        // selected=2, visible_count=2, should scroll
        assert!(ss.scroll_offset > 0);
    }
}