ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
//! The view: a pure function of [`State`] (plus the current time).

use crate::theme::Bg;
use crate::ui::bars::{Banner, StatusBar};
use crate::ui::chrome::{Header, KeyPanel, SearchField, SearchPanel, TabBar, TitleBar};
use crate::ui::overlays::Palette;
use crate::ui::page::{Link, PageView, columns};
use crate::ui::{Ctx, PAD_X, PAD_Y, fill};
use ratatui::Frame;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::Widget;

use crate::ui::diff_view::{DiffView, Keys};
use crate::ui::file_tree::{FileTree, TREE_BG};
use crate::ui::review_sheets::{ComposeSheet, ConfirmSheet, SubmitSheet};

use crate::compose::{Compose, Target};
use crate::diff_screen::{self, DiffScreen, Pane};
use crate::keymap::Action;
use crate::state::{Overlay, Screen, State};

/// The single content pane is focused, so it sits one tone above surface.
const PANE: Bg = Bg::ContainerLow;

pub fn view(state: &State, frame: &mut Frame, now: u64) {
    let area = frame.area();
    let buf = frame.buffer_mut();
    let ctx = state.ctx(now);
    if area.height < 3 || area.width < 10 {
        fill(buf, area, ctx.theme, Bg::Surface);
        return;
    }
    let chrome = state.chrome();
    let lay = state.layout_of(&chrome);
    let status = lay.status;

    // Header: where you are, the search field, you.
    let crumbs: Vec<_> = chrome.crumbs.iter().map(|(c, _)| c.clone()).collect();
    let right: Vec<_> = chrome.right.iter().map(|(t, _)| t.clone()).collect();
    // A filter is typed into the list's own field, not the header's.
    let search_input = match &state.overlay {
        Some(Overlay::Search(sb)) if !state.search_field(sb).1 => Some(&sb.input),
        _ => None,
    };
    let placeholder = format!("Type {} to search", state.first_key(Action::CommandPalette));
    Header {
        ctx,
        crumbs: &crumbs,
        right: &right,
        input: search_input,
        placeholder: &placeholder,
    }
    .render(lay.header, buf);
    if let (Some(rect), Some(pr)) = (lay.title, &chrome.title) {
        TitleBar {
            ctx,
            line: pr_title(state, pr),
        }
        .render(rect, buf);
    }
    if let Some(rect) = lay.tabs {
        let tabs: Vec<_> = chrome.tabs.iter().map(|(t, _)| t.clone()).collect();
        TabBar {
            ctx,
            tabs: &tabs,
            active: chrome.active,
        }
        .render(rect, buf);
    }

    let content = lay.content;
    let composing = match &state.overlay {
        Some(Overlay::Compose(compose)) => Some(compose.as_ref()),
        _ => None,
    };
    let slot = match state.screen() {
        Screen::Page(p) => {
            fill(buf, content, ctx.theme, PANE);
            let hints = match &state.overlay {
                Some(Overlay::Hints(h)) => h.shown(),
                _ => Vec::new(),
            };
            let area = state.page_area();
            let page = p.page();
            // The composer takes the comment box's place, goes under the
            // comment it quotes, or takes the place of the comment it
            // edits, scrolled into view.
            let comment_box = composing.and_then(|c| {
                let (at, under) = match &c.target {
                    Target::Conversation {
                        quoting: Some(at), ..
                    } => (Some(at), true),
                    Target::Edit(e) => (Some(&e.at), false),
                    Target::Discussion(w) => w.place(),
                    _ => (None, false),
                };
                let comment = at.and_then(|at| {
                    page.items.iter().find(|it| {
                        matches!(page.target(it.link), Some(Link::Quote { at: was, .. }) if was == at)
                    })
                });
                // A form's text box, or the conversation's comment box.
                let anchor = match &c.target {
                    Target::Form(t) => Link::Form(crate::ui::form::Row::Text(t.part)),
                    _ => Link::Comment,
                };
                let (item, under) = match comment {
                    Some(item) => (*item, under),
                    None => (*page.items.get(page.find(&anchor, 0)?)?, false),
                };
                Some((usize::from(c.rows()), item, under))
            });
            let scroll = comment_box.map_or(p.scroll, |(rows, item, under)| {
                let at = if under { item.end } else { item.start };
                scroll_to(item.start, at, rows, p.scroll, area.height)
            });
            PageView {
                ctx,
                page,
                scroll,
                selected: p.selected,
                from: p.range,
                current: p.current_match(),
                hints: &hints,
                shift: p.shift,
                word: p
                    .word
                    .and_then(|_| p.word_cursor())
                    .map(|(at, _, w)| (at, w.cols)),
            }
            .render(area, buf);
            comment_box.map(|(_, item, under)| {
                let x = columns(page, area).main_x;
                let (at, replaced) = if under {
                    (item.end, 0)
                } else {
                    (item.start, item.end.saturating_sub(item.start))
                };
                Slot {
                    area: Rect {
                        x,
                        width: page.width.min(area.right().saturating_sub(x)),
                        ..area
                    },
                    offset: at.saturating_sub(scroll),
                    replaced,
                }
            })
        }
        Screen::Diff(screen) => render_diff(state, ctx, content, buf, screen, composing),
    };
    separate_collapsed_tones(ctx, content, buf);

    if let (Some(rect), Some(problem)) = (lay.problem, state.problems.values().next()) {
        Banner {
            ctx,
            text: problem,
            hint: None,
            error: true,
        }
        .render(rect, buf);
    }

    let busy = state.busy();
    let rate_limit = state
        .rate_limits
        .tightest()
        .map(|(_, b)| ("API", b.remaining, b.limit));
    let view = match state.screen() {
        Screen::Diff(screen) => Some(screen.prefs.label()),
        Screen::Page(_) => None,
    };
    StatusBar {
        ctx,
        view: view.as_deref(),
        busy: busy.as_deref(),
        spinner: crate::state::SPINNER
            .get(state.spinner % crate::state::SPINNER.len())
            .unwrap_or(&""),
        notice: state.notices.shown(),
        rate_limit,
        hints: &state.key_hints(),
    }
    .render(status, buf);

    match &state.overlay {
        Some(Overlay::Picker(p)) => {
            let items: Vec<_> = state
                .picker_rows(p)
                .into_iter()
                .map(|(item, _)| item)
                .collect();
            Palette {
                ctx,
                prompt: p.title(),
                input: &p.input,
                items: &items,
                selected: p.selected,
            }
            .render(area, buf);
        }
        Some(Overlay::Compose(compose)) => {
            let title = compose.target.title(state);
            // Saved replies, while they're listed; else what an `@` or
            // `#` can be.
            let saved = crate::site::replies::shown(state);
            let completions: Option<Vec<String>> = match &saved {
                Some((rows, _)) => Some(rows.clone()),
                None => crate::typing::completions(state)
                    .map(|choices| choices.into_iter().map(|c| c.label).collect()),
            };
            let selected = saved.map_or(compose.completing.selected, |(_, at)| at);
            let rows = compose.rows();
            // Where the comment will go, or (without room there) a strip
            // above the status bar.
            let sheet = match slot.filter(|s| s.area.height >= rows) {
                Some(slot) => slot.make_room(buf, rows),
                None => {
                    let height = rows.min(area.height.saturating_sub(3));
                    Rect {
                        y: area.bottom().saturating_sub(height.saturating_add(2)),
                        height,
                        ..area
                    }
                }
            };
            ComposeSheet {
                ctx,
                title: &title,
                input: &compose.input,
                preview: compose
                    .preview
                    .as_ref()
                    .map(|p| (p.start_line, p.original.as_slice(), p.suggested.as_slice())),
                note: compose.note(),
                error: compose.sent.error.as_deref(),
                sending: compose.sent.sending,
                save: compose.target.save(state),
                completions: (completions.as_deref())
                    .map(|c| (c, selected.min(c.len().saturating_sub(1)))),
            }
            .render(sheet, buf);
        }
        Some(Overlay::Submit(dialog)) => {
            let pending = dialog.drafts(state).unwrap_or_default();
            let rejected = pending.iter().filter(|p| p.error.is_some()).count();
            SubmitSheet {
                ctx,
                event: dialog.event,
                input: &dialog.input,
                pending: pending.len(),
                on_github: dialog.on_github,
                rejected,
                error: dialog.sent.error.as_deref(),
                sending: dialog.sent.sending,
                quick: dialog.quick,
            }
            .render(area, buf);
        }
        Some(Overlay::Ask(ask)) => {
            let choices: Vec<String> = ask.choices.iter().map(|(l, _)| l.clone()).collect();
            ConfirmSheet {
                ctx,
                title: &ask.title,
                facts: &ask.facts,
                choices: &choices,
                selected: ask.selected,
                error: ask.sent.error.as_deref(),
                sending: ask.busy().as_deref(),
            }
            .render(area, buf);
        }
        Some(Overlay::Menu(menu)) => {
            let (rows, selected) = state.menu_rows(menu);
            let title = match &menu.filter {
                Some(query) => format!("Here you can: /{query}▏"),
                None => "Here you can".to_owned(),
            };
            KeyPanel {
                ctx,
                title: &title,
                rows: &rows,
                selected: Some(selected),
                position: (menu.selected + 1, menu.shown().len()),
            }
            .render(area, buf);
        }
        Some(Overlay::Search(sb)) => {
            let rows: Vec<_> = state.suggestions(sb).into_iter().map(|(r, _)| r).collect();
            let (field, in_place) = state.search_field(sb);
            if in_place {
                SearchField {
                    ctx,
                    input: &sb.input,
                }
                .render(field, buf);
            }
            SearchPanel {
                ctx,
                rows: &rows,
                selected: sb.selected,
                field,
            }
            .render(area, buf);
        }
        Some(Overlay::Find(input)) => {
            // The prompt replaces the status bar.
            fill(buf, status, ctx.theme, Bg::Container);
            let row = Rect {
                x: status.x + PAD_X,
                width: status.width.saturating_sub(2 * PAD_X),
                ..status
            };
            Span::styled("/", ctx.theme.accent(Bg::Container)).render(row, buf);
            input.render(
                Rect {
                    x: row.x + 1,
                    width: row.width.saturating_sub(1),
                    ..row
                },
                buf,
            );
        }
        None | Some(Overlay::Hints(_)) => {}
    }
}

/// Draws the diff; with `composing`, scrolled to show the composer under
/// the row it's about, whose slot it returns.
fn render_diff(
    state: &State,
    ctx: Ctx<'_>,
    content: Rect,
    buf: &mut Buffer,
    screen: &DiffScreen,
    composing: Option<&Compose>,
) -> Option<Slot> {
    let theme = ctx.theme;
    fill(buf, content, theme, Bg::Surface);
    let diff = state.diffs.get(&screen.of)?;
    let lay = diff_screen::layout(content, &screen.prefs, &diff.tree);
    let mut area = lay.diff;
    if let Some(err) = &diff.error {
        let text = format!("Couldn't load the diff: {err}");
        let retry = format!("{} retry", state.first_key(Action::Refresh));
        let banner = Banner {
            ctx,
            text: &text,
            hint: Some(&retry),
            error: true,
        };
        // Long git errors wrap, up to a third of the pane.
        let height = banner.height(area.width, area.height / 3);
        banner.render(Rect { height, ..area }, buf);
        area.y += height;
        area.height = area.height.saturating_sub(height);
    }
    let message_area = Rect {
        x: area.x + PAD_X,
        y: area.y + PAD_Y.min(area.height),
        width: area.width.saturating_sub(2 * PAD_X),
        height: 1,
    };
    if !diff.listed() {
        if let Some(progress) = &diff.progress {
            Span::styled(
                crate::ui::text::truncate(progress, usize::from(message_area.width)),
                theme.meta(Bg::Surface),
            )
            .render(message_area, buf);
        }
        return None;
    }
    if diff.doc.is_empty() {
        Span::styled("No changed files.", theme.meta(Bg::Surface)).render(message_area, buf);
        return None;
    }
    let key = |a| state.first_key(a);
    let jump = key(Action::Open);
    let (show, expand, viewed, reply, resolve, delete, file_comment) = (
        key(Action::Open),
        key(Action::Open),
        key(Action::ToggleViewed),
        key(Action::Comment),
        key(Action::ResolveThread),
        key(Action::Delete),
        key(Action::FileComment),
    );
    // The composer goes under its row, which stays clear of the sticky
    // header.
    let doc = &diff.doc;
    let anchor = composing.map(|c| crate::review::compose_anchor(doc, screen, &c.target));
    let mut top = doc.to_global(screen.top);
    let slot = composing.zip(anchor).map(|(c, anchor)| {
        let row = doc.to_global(anchor);
        let first = row.saturating_sub(usize::from(anchor.row > 0));
        let rows = usize::from(c.rows());
        top = scroll_to(first, row.saturating_add(1), rows, top, area.height);
        Slot {
            area,
            offset: row.saturating_add(1).saturating_sub(top),
            replaced: 0,
        }
    });
    DiffView {
        ctx,
        doc,
        cursor: screen.cursor,
        half: screen.half,
        top: doc.to_pos(top),
        keys: Keys {
            show: &show,
            jump: &jump,
            expand: &expand,
            viewed: &viewed,
            reply: &reply,
            resolve: &resolve,
            delete: &delete,
            file_comment: &file_comment,
        },
        selection: screen.selection.map(|anchor| (anchor, screen.cursor)),
        shift: screen.shift,
        word: screen.word,
    }
    .render(area, buf);
    if let Some(Overlay::Hints(hints)) = &state.overlay {
        crate::ui::page::draw_hints(theme, &hints.shown(), area, buf);
    }

    if let Some(tree) = lay.tree {
        FileTree {
            ctx,
            doc: &diff.doc,
            rows: &crate::ui::file_tree::unfolded(&diff.tree, &screen.folded),
            selected: screen.tree_selected,
            scroll: screen.tree_scroll,
            focused: screen.focus == Pane::Tree,
        }
        .render(tree, buf);
        // Tone separates the panes unless the colors quantized together.
        if theme.tones_collapse(TREE_BG, Bg::Surface) {
            let x = tree.right().saturating_sub(1);
            for y in tree.top()..tree.bottom() {
                if let Some(cell) = buf.cell_mut((x, y)) {
                    cell.set_symbol("│").set_style(theme.separator(TREE_BG));
                }
            }
        }
    }
    slot
}

/// The scroll that shows rows `start..start + rows` while row `first`
/// stays in view: `first` wins when they don't fit together.
fn scroll_to(first: usize, start: usize, rows: usize, scroll: usize, height: u16) -> usize {
    let lowest = start
        .saturating_add(rows)
        .saturating_sub(usize::from(height));
    scroll.max(lowest).min(first)
}

/// Where the composer goes: `offset` rows into `area`, in place of
/// `replaced` rows.
#[derive(Clone, Copy)]
struct Slot {
    area: Rect,
    offset: usize,
    replaced: usize,
}

impl Slot {
    /// Moves what's below the slot down to make room for `rows` rows
    /// (kept on screen, over what's above if they must), and returns them.
    fn make_room(self, buf: &mut Buffer, rows: u16) -> Rect {
        let Slot { area, .. } = self;
        let offset = u16::try_from(self.offset).unwrap_or(u16::MAX);
        let replaced = u16::try_from(self.replaced).unwrap_or(u16::MAX);
        let y = area
            .y
            .saturating_add(offset)
            .min(area.bottom().saturating_sub(rows));
        let shift = rows.saturating_sub(replaced);
        for to in (y.saturating_add(rows)..area.bottom()).rev() {
            let from = to.saturating_sub(shift);
            for x in area.left()..area.right() {
                if let Some(cell) = buf.cell((x, from)).cloned()
                    && let Some(dest) = buf.cell_mut((x, to))
                {
                    *dest = cell;
                }
            }
        }
        Rect {
            y,
            height: rows,
            ..area
        }
    }
}

/// Where the pane and the bars quantize to the same color (256-color
/// terminals), tone can't separate them; draw outline-variant rules in the
/// pane's padding rows instead.
fn separate_collapsed_tones(ctx: Ctx<'_>, content: Rect, buf: &mut Buffer) {
    if !ctx.theme.tones_collapse(Bg::Container, PANE) || content.height < 2 {
        return;
    }
    let style = ctx.theme.separator(PANE);
    for y in [content.y, content.bottom() - 1] {
        // Leave banners alone.
        let row_is_pane = buf
            .cell((content.x, y))
            .is_some_and(|c| c.bg == ctx.theme.bg_color(PANE) && c.symbol() == " ");
        if !row_is_pane {
            continue;
        }
        for x in content.left()..content.right() {
            if let Some(cell) = buf.cell_mut((x, y))
                && cell.symbol() == " "
            {
                cell.set_symbol("─").set_style(style);
            }
        }
    }
}

/// A pull request's sticky title: state, title, number.
fn pr_title<'a>(state: &'a State, pr: &crate::api::model::PrRef) -> Line<'a> {
    let theme = &state.theme;
    let bar = Bg::Container;
    let Some(d) = state.pr(pr) else {
        return Line::from(Span::styled(pr.to_string(), theme.title(bar)));
    };
    let (text, bg) = crate::ui::pages::state_label(crate::api::browse::Status::Pr(d.state));
    Line::from(vec![
        Span::styled(
            format!(" {} {text} ", state.icons.pr_state(d.state)),
            theme.fill(bg),
        ),
        Span::styled("  ", theme.body(bar)),
        Span::styled(d.title.clone(), theme.title(bar)),
        Span::styled(format!("  #{}", pr.number), theme.meta(bar)),
    ])
}