rootle 0.8.4

Modal TUI for browsing remote source-control systems (GitHub in-tree, any forge via stdio providers) from the terminal
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
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
//! Drawing for the search view: the field row, the folded result
//! blocks, and the scope radio popup.

use super::GlobalSearch;
use super::model::{Scope, SearchHit};
use crate::components::pane::fit;
use crate::components::{centered, scrollbar};
use crate::keymap;
use crate::mode::Mode;
use crate::theme::Theme;

use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::symbols;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph};
use unicode_width::UnicodeWidthStr;

impl GlobalSearch {
    pub fn render(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let sem = &theme.semantic;

        let hint = if self.filtering {
            " type to filter · enter commit · esc cancel ".into()
        } else if self.scope_popup {
            " j/k move · enter done · esc revert ".into()
        } else if self.finding {
            keymap::hint_row(keymap::hints(Mode::Find))
        } else if self.focus == super::Focus::Facets {
            keymap::hint_row(keymap::search_facets())
        } else if self.expanded.is_some() {
            keymap::hint_row(keymap::search_file())
        } else {
            keymap::hint_row(keymap::search_results())
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(theme.border_type())
            .border_style(Style::default().fg(sem.border_focused))
            .style(Style::default().bg(sem.mantle))
            .title(Span::styled(
                self.kind.title(),
                Style::default().fg(sem.text).add_modifier(Modifier::BOLD),
            ))
            .title_bottom(Span::styled(hint, Style::default().fg(sem.hint)));
        let inner = block.inner(area);
        frame.render_widget(block, area);
        let rows = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3),
                // The chip row (plans/0012 M3) exists only once hits
                // hold a facet; a zero-height row keeps the layout
                // indices stable either way.
                Constraint::Length(if self.facets().is_empty() { 0 } else { 1 }),
                Constraint::Min(1),
            ])
            .split(inner);

        let fields = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(50),
                Constraint::Percentage(25),
                Constraint::Percentage(25),
            ])
            .split(rows[0]);

        self.render_field(
            frame,
            fields[0],
            theme,
            " query ",
            &self.query.value(),
            self.focus == super::Focus::Query,
            Some(self.query.cursor()),
            true,
        );
        self.render_field(
            frame,
            fields[1],
            theme,
            " scope ",
            &format!("{} ▾", self.scope_label()),
            self.focus == super::Focus::Scope,
            None,
            false,
        );
        self.render_field(
            frame,
            fields[2],
            theme,
            " extension ",
            &self.extension.value(),
            self.focus == super::Focus::Extension,
            Some(self.extension.cursor()),
            false,
        );

        self.render_facets(frame, rows[1], theme);
        self.render_results(frame, rows[2], theme);

        if self.scope_popup {
            self.render_scope_popup(frame, area, theme);
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn render_field(
        &self,
        frame: &mut Frame,
        area: Rect,
        theme: &Theme,
        title: &str,
        value: &str,
        focused: bool,
        cursor: Option<usize>,
        styled: bool,
    ) {
        let sem = &theme.semantic;
        let border = if focused {
            sem.border_focused
        } else {
            sem.border_unfocused
        };
        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(theme.border_type())
            .border_style(Style::default().fg(border))
            .style(Style::default().bg(sem.base))
            .title(Span::styled(title, Style::default().fg(sem.subtext0)));
        let inner = block.inner(area);
        frame.render_widget(block, area);
        let width = inner.width.saturating_sub(2) as usize;
        let prompt = if focused {
            Span::styled("❯ ", Style::default().fg(sem.border_focused))
        } else {
            Span::styled("❯ ", Style::default().fg(sem.overlay0))
        };
        let mut spans = vec![prompt];
        if styled {
            // Grammar eye candy (plans/0012 M1): qualifiers/quoted
            // literals/negation markers take syntax colors — the spans
            // partition the value byte-exactly, so a query we can't
            // segment renders verbatim and nothing bleeds elsewhere.
            let styled_spans = fit_spans(super::grammar::style_query(value, theme), width);
            spans.extend(styled_spans);
        } else {
            spans.push(Span::styled(
                fit(value, width),
                Style::default().fg(sem.text),
            ));
        }
        frame.render_widget(Paragraph::new(Line::from(spans)), inner);
        if focused && let Some(cursor) = cursor {
            let x = inner.x + 2 + cursor as u16;
            if x < inner.x + inner.width {
                frame.set_cursor_position((x, inner.y));
            }
        }
    }

    /// The facet chip row (plans/0012 M3): repos, a dim divider, then
    /// languages — each chip `name·count`, counts live over the whole
    /// accumulated set. The committed chip glows (search-match) as the
    /// visible source of the narrowing; the keyboard cursor takes
    /// selection colors. Whole chips drop off the tail (`…` marks the
    /// cut), or off the head if that's what keeps the cursor visible.
    fn render_facets(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        if area.height == 0 {
            return;
        }
        let sem = &theme.semantic;
        let chips = self.facets();
        if chips.is_empty() {
            return;
        }
        let focused = self.focus == super::Focus::Facets;
        let width = area.width as usize;

        // Display width of chip idx (text + two-space separator),
        // plus the group divider between the repo and language
        // halves.
        let chip_w = |idx: usize| -> usize {
            format!("{}·{}", chips[idx].id.name, chips[idx].count).width() + 2
        };
        let divider =
            |idx: usize| -> bool { idx > 0 && chips[idx].id.kind != chips[idx - 1].id.kind };
        let w = |idx: usize| chip_w(idx) + usize::from(divider(idx));

        // Chips render from the head and drop off the tail (`…` marks
        // the cut); when the keyboard cursor's chip would fall off,
        // the window starts later instead — the cursor stays visible.
        let mut start = 0;
        if focused {
            let tail_end = {
                let mut used = 8; // " facets "
                let mut idx = 0;
                while idx < chips.len() && used + w(idx) < width {
                    // +1: … reserve
                    used += w(idx);
                    idx += 1;
                }
                idx
            };
            if self.facet_cursor >= tail_end {
                let mut lo = self.facet_cursor;
                let mut used = 9; // label + a leading …
                while lo > 0 && used + w(lo - 1) <= width {
                    used += w(lo - 1);
                    lo -= 1;
                }
                start = lo;
            }
        }

        let mut spans: Vec<Span<'static>> =
            vec![Span::styled(" facets ", Style::default().fg(sem.hint))];
        let mut used = 8;
        if start > 0 {
            spans.push(Span::styled("…", Style::default().fg(sem.overlay0)));
            used += 1;
        }
        for (idx, chip) in chips.iter().enumerate().skip(start) {
            if divider(idx) {
                spans.push(Span::styled("│ ", Style::default().fg(sem.overlay0)));
                used += 2;
            }
            let cursor = focused && idx == self.facet_cursor;
            let active = self.facet.as_ref() == Some(&chip.id);
            let style = if cursor {
                Style::default().fg(sem.selection_fg).bg(sem.selection_bg)
            } else if active {
                Style::default().fg(sem.crust).bg(sem.search_match)
            } else {
                Style::default().fg(sem.subtext0)
            };
            let text = format!("{}·{}  ", chip.id.name, chip.count);
            used += text.width();
            if used + 1 > width {
                spans.push(Span::styled("…", Style::default().fg(sem.overlay0)));
                break;
            }
            spans.push(Span::styled(text, style));
        }
        frame.render_widget(Paragraph::new(Line::from(spans)), area);
    }

    fn render_results(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) {
        // Expanded (plans/0012 M2): the results area becomes the hit's
        // whole file — the re-used Preview renders into the same rect,
        // no popup, nothing else drawn.
        if let Some(exp) = &mut self.expanded {
            exp.preview.render(frame, area, theme);
            return;
        }
        let sem = &theme.semantic;
        let focused = self.focus == super::Focus::Results;
        let border = if focused {
            sem.border_focused
        } else {
            sem.border_unfocused
        };
        let mut title = if let Some(error) = &self.error {
            format!(" results — error: {error} ")
        } else if self.pending && self.hits.is_empty() {
            " results — searching… ".to_string()
        } else if self.pending {
            // v1.3: hits stream in — the count climbs live.
            let suffix = if self.clipped { " · clipped" } else { "" };
            format!(" results — {} · streaming{suffix} ", self.visible().len())
        } else if self.hits.is_empty() && self.submitted_once {
            " results — no matches ".into()
        } else if self.submitted_once {
            let mut suffix = String::new();
            if let Some(as_of) = &self.index_as_of {
                // Indexed backends say when the index was built — a
                // lagging index is worth the badge.
                let short: String = as_of.chars().take(19).collect();
                suffix.push_str(&format!(" · index {short}"));
            }
            if self.clipped {
                suffix.push_str(" · clipped");
            }
            if let Some(note) = &self.search_ref_note {
                suffix.push_str(&format!(" · {note}"));
            }
            // plans/0012 M1 honesty chips: hits rootle subtracted
            // client-side (the backend couldn't express the grammar),
            // and tokens nobody could express.
            if self.client_filtered > 0 {
                suffix.push_str(&format!(" · filtered {}", self.client_filtered));
            }
            if !self.unfiltered.is_empty() {
                suffix.push_str(&format!(" · unfiltered: {}", self.unfiltered.join(" ")));
            }
            format!(" results — {}{suffix} ", self.visible().len())
        } else {
            " results ".into()
        };
        if !self.filter_value.is_empty() {
            title = format!("{} /{}", title.trim_end(), self.filter_value);
        }

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(theme.border_type())
            .border_style(Style::default().fg(border))
            .style(Style::default().bg(sem.base))
            .title(Span::styled(title, Style::default().fg(sem.subtext0)));
        let inner = block.inner(area);
        frame.render_widget(block, area);

        let width = inner.width as usize;
        let height = inner.height as usize;
        let visible = self.visible();

        // Build one bordered box per hit; remember each hit's line
        // range so the selection can be kept in view.
        let mut lines: Vec<Line> = Vec::new();
        let mut ranges: Vec<(usize, usize)> = Vec::new(); // [start, end)
        for (idx, hit) in visible.iter().enumerate() {
            let start = lines.len();
            let selected = idx == self.selected && focused;
            lines.extend(self.hit_box(hit, width, selected, theme));
            ranges.push((start, lines.len()));
        }
        let total = lines.len();

        // Keep the selected hit visible; J/K free scroll otherwise.
        if focused && let Some((start, end)) = ranges.get(self.selected).copied() {
            if start < self.scroll as usize {
                self.scroll = start as u16;
            } else if end >= self.scroll as usize + height {
                self.scroll = (end + 1).saturating_sub(height) as u16;
            }
        }
        let max_scroll = total.saturating_sub(height) as u16;
        self.scroll = self.scroll.min(max_scroll);

        frame.render_widget(Paragraph::new(lines).scroll((self.scroll, 0)), inner);
        scrollbar(frame, area, height, total, self.scroll as usize, theme);
    }

    /// One hit as a bordered box — the bat/delta file-header
    /// convention in the pane idiom this TUI already uses: the
    /// filename rides the top rule as the box's title (the same
    /// decoration the pane titles carry), the match badge closes the
    /// rule on the right, and the match lines keep their `│` gutter
    /// between the box's rails. Selection paints rails + title instead
    /// of a `▌` gutter row. The border shape follows `[ui] border`.
    fn hit_box(
        &self,
        hit: &SearchHit,
        width: usize,
        selected: bool,
        theme: &Theme,
    ) -> Vec<Line<'static>> {
        let sem = &theme.semantic;
        let set = match theme.border_type() {
            BorderType::Rounded => symbols::border::ROUNDED,
            BorderType::Thick => symbols::border::THICK,
            BorderType::Double => symbols::border::DOUBLE,
            BorderType::Plain => symbols::border::PLAIN,
            // [ui] border only offers the four above.
            _ => symbols::border::PLAIN,
        };
        let rail = Style::default().fg(if selected {
            sem.border_focused
        } else {
            sem.overlay0
        });
        let h = set.horizontal_top;

        let meta = if hit.unlocatable {
            "unlocatable".to_string()
        } else if hit.stale {
            "stale".to_string()
        } else if hit.match_count > 0 {
            // Grep hits carry a match-count badge (folded
            // multi-matches); file-find hits show the anchor line.
            format!(
                "{} match{}",
                hit.match_count,
                if hit.match_count == 1 { "" } else { "es" }
            )
        } else {
            format!(":{}", hit.line)
        };
        let meta_style = Style::default().fg(if hit.stale { sem.warning } else { sem.subtext0 });
        let title_style = {
            let mut s = Style::default()
                .fg(if selected { sem.selection_fg } else { sem.text })
                .add_modifier(Modifier::BOLD);
            if selected {
                s = s.bg(sem.selection_bg);
            }
            s
        };

        // Cross-repo results need the repo in the title; repo-scope
        // results keep it too — unambiguous everywhere.
        let full = format!("{}/{}", hit.repo, hit.path);
        let inner = width.saturating_sub(2); // between the corners
        // Top rule: ╭─ path ─fill─ meta ─╮
        let fixed = 2 + meta.width() + 6; // "─ " … " " + meta + " ─"
        let path = fit(&full, inner.saturating_sub(fixed).max(8));
        let fill = inner.saturating_sub(2 + path.width() + 1 + 1 + meta.width() + 2);
        let mut lines = vec![Line::from(vec![
            Span::styled(set.top_left.to_string(), rail),
            Span::styled(h.to_string(), rail),
            Span::raw(" "),
            Span::styled(path, title_style),
            Span::raw(" "),
            Span::styled(h.repeat(fill), rail),
            Span::raw(" "),
            Span::styled(meta, meta_style),
            Span::raw(" "),
            Span::styled(h.to_string(), rail),
            Span::styled(set.top_right.to_string(), rail),
        ])];

        // Content lines between the rails; disjoint match regions get
        // a dim ellipsis aligned over the gutter divider.
        let pad = |line: Line<'static>| -> Line<'static> {
            let w = line.width();
            let mut spans = vec![Span::styled(set.vertical_left.to_string(), rail)];
            spans.extend(line.spans);
            spans.push(Span::raw(" ".repeat(inner.saturating_sub(w))));
            spans.push(Span::styled(set.vertical_right.to_string(), rail));
            Line::from(spans)
        };
        let mut prev_no: Option<u32> = None;
        for (no, line) in &hit.preview {
            if let Some(prev) = prev_no
                && *no > prev + 1
            {
                lines.push(pad(Line::from(Span::styled(
                    format!("{:>6} ", "⋮"),
                    Style::default().fg(sem.subtext0),
                ))));
            }
            prev_no = Some(*no);
            lines.push(pad(preview_line(*no, line, theme)));
        }

        lines.push(Line::from(vec![
            Span::styled(set.bottom_left.to_string(), rail),
            Span::styled(h.repeat(inner), rail),
            Span::styled(set.bottom_right.to_string(), rail),
        ]));
        lines
    }

    fn render_scope_popup(&self, frame: &mut Frame, area: Rect, theme: &Theme) {
        let sem = &theme.semantic;
        let items = self.scope_items();
        let height = items.len() as u16 + 2; // rows + border
        let popup_area = centered(area, 40, 30);
        let popup = Rect {
            height: height.min(popup_area.height),
            ..popup_area
        };

        frame.render_widget(Clear, popup);
        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(theme.border_type())
            .border_style(Style::default().fg(sem.border_focused))
            .style(Style::default().bg(sem.mantle))
            .title(Span::styled(
                " scope ",
                Style::default().fg(sem.text).add_modifier(Modifier::BOLD),
            ))
            .title_bottom(Span::styled(
                " j/k move · enter done · esc revert ",
                Style::default().fg(sem.hint),
            ));
        let inner = block.inner(popup);
        frame.render_widget(block, popup);

        let mut lines = Vec::new();
        for (idx, (scope, enabled)) in items.iter().enumerate() {
            let radio = if *scope == self.scope { "(•)" } else { "( )" };
            let label = match scope {
                Scope::Repo => match &self.repo {
                    Some(repo) => format!("current repo  repo:{repo}"),
                    None => "current repo  (no repo open)".to_string(),
                },
                Scope::Org => match &self.org {
                    Some(org) => format!("current org  org:{org}"),
                    None => "current org  (no org selected)".to_string(),
                },
                Scope::Global => "all of github".to_string(),
            };
            let cursor = idx == self.scope_cursor;
            let fg = if !enabled {
                sem.subtext0
            } else if cursor {
                sem.selection_fg
            } else {
                sem.text
            };
            let mut style = Style::default().fg(fg);
            if cursor {
                style = style.bg(sem.selection_bg);
            }
            lines.push(Line::from(Span::styled(
                format!("{} {}", radio, label),
                style,
            )));
        }
        frame.render_widget(Paragraph::new(lines), inner);
    }
}

/// fit() for styled spans: cut whole spans at the width, then the
/// last span per-char.
fn fit_spans(spans: Vec<Span<'static>>, width: usize) -> Vec<Span<'static>> {
    let mut out = Vec::new();
    let mut used = 0;
    for span in spans {
        let w = span.content.width();
        if used + w <= width {
            used += w;
            out.push(span);
        } else {
            let mut cut = String::new();
            for c in span.content.chars() {
                let cw = unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
                if used + cw > width {
                    break;
                }
                cut.push(c);
                used += cw;
            }
            out.push(Span::styled(cut, span.style));
            break;
        }
    }
    out
}

/// Preview line: right-aligned line-number gutter with the same dim
/// `│` divider the browser preview uses, then the highlighted spans.
fn preview_line(no: u32, line: &Line<'static>, theme: &Theme) -> Line<'static> {
    let sem = &theme.semantic;
    let mut spans = vec![
        Span::styled(format!("{no:>4} "), Style::default().fg(sem.subtext0)),
        Span::styled("│ ", Style::default().fg(sem.overlay0)),
    ];
    spans.extend(line.spans.iter().cloned());
    Line::from(spans)
}