ncoxide 0.4.0

Modal dual-pane file commander with helix-inspired interface
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
use std::io;
use std::path::Path;
use std::time::Duration;

use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use crossterm::execute;
use crossterm::terminal::{
    EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use ratatui::Terminal;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};

use crate::preview::{self, FilterMatch, LineFilter, LineIndex, PreviewState, SearchKind};

const FILTER_MAX: usize = 200;

/// Viewer input mode.
enum Mode {
    Normal,
    /// Typing a search query (`/`).
    Search {
        buf: String,
        kind: SearchKind,
    },
    /// Fuzzy line-filter (`&`): a query plus the background worker and its
    /// current best matches.
    Filter {
        buf: String,
        filter: Option<LineFilter>,
        results: Vec<FilterMatch>,
        cursor: usize,
    },
}

/// Standalone full-screen pager backed by the windowed preview reader, so it
/// opens arbitrarily large files with bounded memory.
///
/// Keys: `j/k`, `PgUp/PgDn`, `Space`; `g`/`G` top/bottom (or `NG` to jump to a
/// typed line number); `/` search (literal; `Ctrl-R` toggles regex), `n`/`N`
/// next/prev match; `q` quit.
pub fn view_file(path: &Path) -> crate::error::Result<()> {
    let mut state = preview::load_preview(path);
    // For large (windowed) files, scan total lines + a sparse offset index in
    // the background (accurate "line X / N" and fast goto). Aborts on drop.
    let index = state
        .is_large()
        .then(|| LineIndex::spawn(path.to_path_buf()));

    let mut mode = Mode::Normal;
    let mut goto_count: Option<usize> = None;
    let mut last_kind = SearchKind::Literal;
    let mut status_msg: Option<String> = None;

    enable_raw_mode().map_err(crate::error::NcError::Io)?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen).map_err(crate::error::NcError::Io)?;
    let backend = ratatui::backend::CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend).map_err(crate::error::NcError::Io)?;

    loop {
        if let Some(i) = &index {
            state.set_line_count(i.count(), i.is_done());
        }
        // Pull the latest fuzzy-filter results from its background worker.
        if let Mode::Filter {
            filter: Some(f),
            results,
            cursor,
            ..
        } = &mut mode
        {
            *results = f.results();
            if *cursor >= results.len() {
                *cursor = results.len().saturating_sub(1);
            }
        }

        terminal
            .draw(|f| draw_viewer(f, &state, path, &mode, goto_count, status_msg.as_deref()))
            .map_err(crate::error::NcError::Io)?;

        // Poll so the view refreshes while the background scan progresses.
        if !event::poll(Duration::from_millis(250)).map_err(crate::error::NcError::Io)? {
            continue;
        }
        let Event::Key(key) = event::read().map_err(crate::error::NcError::Io)? else {
            continue;
        };

        let page = terminal
            .size()
            .map(|s| s.height as usize)
            .unwrap_or(24)
            .saturating_sub(3);

        match &mut mode {
            Mode::Normal => {
                status_msg = None;
                match key.code {
                    KeyCode::Char('q') | KeyCode::Esc => break,
                    KeyCode::Char(d @ '0'..='9') => {
                        let n = goto_count.unwrap_or(0);
                        goto_count = Some(n.saturating_mul(10) + (d as usize - '0' as usize));
                    }
                    KeyCode::Char('g') => match goto_count.take() {
                        Some(n) => goto(&mut state, index.as_ref(), n),
                        None => state.scroll_to_top(),
                    },
                    KeyCode::Char('G') => match goto_count.take() {
                        Some(n) => goto(&mut state, index.as_ref(), n),
                        None => state.scroll_to_bottom(page),
                    },
                    KeyCode::Char('j') | KeyCode::Down => state.scroll_down(1, page),
                    KeyCode::Char('k') | KeyCode::Up => state.scroll_up(1),
                    KeyCode::PageDown | KeyCode::Char(' ') => state.scroll_down(page, page),
                    KeyCode::PageUp => state.scroll_up(page),
                    KeyCode::Char('n') => {
                        state.search_next(true);
                    }
                    KeyCode::Char('N') => {
                        state.search_next(false);
                    }
                    KeyCode::Char('/') => {
                        mode = Mode::Search {
                            buf: String::new(),
                            kind: last_kind,
                        };
                    }
                    KeyCode::Char('&') => {
                        mode = Mode::Filter {
                            buf: String::new(),
                            filter: None,
                            results: Vec::new(),
                            cursor: 0,
                        };
                    }
                    _ => goto_count = None,
                }
            }
            Mode::Search { buf, kind } => match key.code {
                KeyCode::Esc => mode = Mode::Normal,
                KeyCode::Enter => {
                    last_kind = *kind;
                    match state.set_search(buf, *kind) {
                        Ok(()) => {
                            state.search_next(true);
                        }
                        Err(e) => status_msg = Some(e),
                    }
                    mode = Mode::Normal;
                }
                KeyCode::Backspace => {
                    buf.pop();
                }
                KeyCode::Char('r') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                    *kind = kind.toggled();
                }
                KeyCode::Char(c) if crate::mode::accepts_text(&key) => buf.push(c),
                _ => {}
            },
            Mode::Filter {
                buf,
                filter,
                results,
                cursor,
            } => match key.code {
                KeyCode::Esc => mode = Mode::Normal,
                KeyCode::Enter => {
                    let line = results.get(*cursor).map(|m| m.line as usize);
                    if let Some(line) = line {
                        goto(&mut state, index.as_ref(), line);
                    }
                    mode = Mode::Normal;
                }
                KeyCode::Up => *cursor = cursor.saturating_sub(1),
                KeyCode::Down => {
                    if *cursor + 1 < results.len() {
                        *cursor += 1;
                    }
                }
                KeyCode::Backspace => {
                    buf.pop();
                    respawn_filter(buf, filter, results, cursor, path);
                }
                KeyCode::Char(c) if crate::mode::accepts_text(&key) => {
                    buf.push(c);
                    respawn_filter(buf, filter, results, cursor, path);
                }
                _ => {}
            },
        }
    }

    disable_raw_mode().ok();
    execute!(terminal.backend_mut(), LeaveAlternateScreen).ok();
    terminal.show_cursor().ok();
    Ok(())
}

/// Jump to 1-based `line`, using the sparse index checkpoint when available.
fn goto(state: &mut PreviewState, index: Option<&LineIndex>, line: usize) {
    let checkpoint = index.map(|i| i.checkpoint_for(line)).unwrap_or((0, 1));
    state.goto_line(line, checkpoint);
}

/// (Re)start the fuzzy-filter worker for the current query, or clear it when
/// the query is empty. Results stream in on subsequent loop ticks.
fn respawn_filter(
    buf: &str,
    filter: &mut Option<LineFilter>,
    results: &mut Vec<FilterMatch>,
    cursor: &mut usize,
    path: &Path,
) {
    *cursor = 0;
    results.clear();
    *filter = if buf.is_empty() {
        None
    } else {
        Some(LineFilter::spawn(
            path.to_path_buf(),
            buf.to_string(),
            FILTER_MAX,
        ))
    };
}

fn draw_viewer(
    f: &mut ratatui::Frame,
    state: &PreviewState,
    path: &Path,
    mode: &Mode,
    goto_count: Option<usize>,
    status_msg: Option<&str>,
) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(1), Constraint::Length(1)])
        .split(f.area());

    draw_content(f, state, path, chunks[0]);
    draw_footer(f, state, mode, goto_count, status_msg, chunks[1]);

    if let Mode::Filter {
        buf,
        results,
        cursor,
        ..
    } = mode
    {
        draw_filter_overlay(f, buf, results, *cursor);
    }
}

fn draw_filter_overlay(
    f: &mut ratatui::Frame,
    query: &str,
    results: &[FilterMatch],
    cursor: usize,
) {
    let area = f.area();
    // max-then-min, not clamp(40, w): clamp panics when the terminal is
    // narrower than 40; saturating_mul avoids u16 overflow on huge widths.
    let width = (area.width.saturating_mul(3) / 4).max(40).min(area.width);
    let height = 20u16.min(area.height.saturating_sub(2));
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    let rect = Rect::new(x, y, width, height);
    f.render_widget(Clear, rect);

    let mut lines: Vec<Line> = Vec::new();
    lines.push(Line::from(vec![
        Span::styled(
            " > ",
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw(query.to_string()),
        Span::styled("_", Style::default().fg(Color::DarkGray)),
    ]));
    lines.push(Line::raw(""));

    let rows = height.saturating_sub(4) as usize;
    for (i, m) in results.iter().take(rows).enumerate() {
        let style = if i == cursor {
            Style::default()
                .fg(Color::White)
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::White)
        };
        let marker = if i == cursor { ">" } else { " " };
        lines.push(Line::from(vec![
            Span::styled(
                format!("{marker} {:>7} ", m.line),
                Style::default().fg(Color::DarkGray),
            ),
            Span::styled(m.text.clone(), style),
        ]));
    }
    if results.is_empty() && !query.is_empty() {
        lines.push(Line::styled(
            "  (no matches yet…)",
            Style::default().fg(Color::DarkGray),
        ));
    }

    let block = Block::default()
        .title(" Fuzzy line-filter (Enter jump, Esc cancel) ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));
    f.render_widget(Paragraph::new(lines).block(block), rect);
}

fn draw_content(f: &mut ratatui::Frame, state: &PreviewState, path: &Path, area: Rect) {
    let mut title = format!(" {}{} ", path.display(), state.status_text());
    if let Some(s) = state.active_search() {
        title.push_str(&format!("[/{} {}] ", s.query(), s.kind().label()));
    }

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));

    let inner = block.inner(area);
    let para = Paragraph::new(state.render(inner.height as usize)).block(block);
    f.render_widget(para, area);
}

fn draw_footer(
    f: &mut ratatui::Frame,
    state: &PreviewState,
    mode: &Mode,
    goto_count: Option<usize>,
    status_msg: Option<&str>,
    area: Rect,
) {
    let (text, style) = match mode {
        Mode::Search { buf, kind } => (
            format!(
                "/{buf}  [{}]  (Ctrl-R toggles, Enter search, Esc cancel)",
                kind.label()
            ),
            Style::default().fg(Color::Yellow),
        ),
        Mode::Filter { buf, results, .. } => (
            format!(
                "&{buf}  ({} matches)  (↑/↓ select, Enter jump, Esc cancel)",
                results.len()
            ),
            Style::default().fg(Color::Yellow),
        ),
        Mode::Normal => {
            if let Some(msg) = status_msg {
                (msg.to_string(), Style::default().fg(Color::Red))
            } else if let Some(n) = goto_count {
                (
                    format!(":{n}  (g/G to jump)"),
                    Style::default().fg(Color::Yellow),
                )
            } else {
                let hint = if state.active_search().is_some() {
                    "/ search  n/N next  NG goto  q quit"
                } else {
                    "/ search  NG goto line  g/G top/bottom  q quit"
                };
                (hint.to_string(), Style::default().fg(Color::DarkGray))
            }
        }
    };
    let para = Paragraph::new(text).style(style.add_modifier(Modifier::DIM));
    f.render_widget(para, area);
}

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

    #[test]
    fn test_view_file_nonexistent() {
        // load_preview handles missing files with a message line.
        let state = preview::load_preview(Path::new("/nonexistent/file"));
        assert!(!state.render(1).is_empty());
    }

    #[test]
    fn test_filter_overlay_panic_free_at_tiny_sizes() {
        // The old width computation used clamp(40, area.width), which panics
        // for terminals narrower than 40 columns (R3).
        for (w, h) in [(1u16, 1u16), (10, 3), (39, 4), (80, 24)] {
            let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(w, h)).unwrap();
            terminal
                .draw(|f| draw_filter_overlay(f, "query", &[], 0))
                .unwrap();
        }
    }
}