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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Application state and the single update function. Input events and async
//! results arrive as [`Msg`]s; [`update`] mutates [`State`] and returns
//! [`Cmd`]s for the runtime to execute in the background.

use crate::route::{Route, Target, app, code, projects, runs, settings};
use std::collections::HashMap;
use std::sync::Arc;

use crate::api::ApiError;
use crate::api::model::{NodeId, PrRef, ViewedFiles};
use crate::api::rate_limit::RateLimits;
use crate::diff::FileDiff;
use crate::git::Oid;
use crate::store::ReviewState;
use crate::theme::{Bg, Theme};
use crate::ui::bars::Notice;
use crate::ui::{Ctx, Fetched, Icons};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent};
use ratatui_textarea::TextArea;

use crate::act;
use crate::api::model::{PatchFile, ReviewThread};
use crate::browse;
use crate::compose::{self, Compose};
use crate::data::{Data, DataKey};
use crate::diff_job::{JobId, JobMsg};
use crate::diff_screen::{self, DiffOf, DiffPrefs, DiffScreen, DiffState, Pane};
use crate::join;
use crate::keymap::{Action, Context, Keymap};
use crate::nav::{self, Hints, Menu, PageScreen, SearchBox, Visit};
use crate::notice::Notices;
use crate::picker::{self, Picker};
use crate::review::{EditPurpose, SubmitDialog, on_edited, on_submit_key, review_action};
use crate::tabs::Tab;
use crate::work::Failure;

mod diff;
mod dispatch;
mod fetch;
mod unread;

pub use dispatch::*;
pub use fetch::*;

#[derive(Debug)]
pub enum Msg {
    Key(KeyEvent),
    Resize(u16, u16),
    Viewer(Result<String, ApiError>),
    /// Page data; with `cached_at` (when it was fetched), the cached copy
    /// shown while fetching. Where git could have answered, `source` says
    /// where it came from.
    Fetched {
        key: DataKey,
        result: Result<Data, ApiError>,
        cached_at: Option<u64>,
        source: Option<crate::source::From>,
    },
    /// The next page of a list, asked for after the cursor, and where it
    /// came from.
    FetchedMore(
        DataKey,
        String,
        Result<Data, ApiError>,
        Option<crate::source::From>,
    ),
    Mouse(MouseEvent),
    Timer(Timer),
    /// The search box's input settled; fetch live suggestions if it's
    /// still this.
    SuggestDue(String),
    RateLimits(RateLimits),
    Notice(Notice),
    /// A lasting problem started (`Some`) or cleared up (`None`).
    Problem(Problem, Option<String>),
    /// `$EDITOR` finished (or failed to start).
    Edited(EditPurpose, Result<String, Failure>),
    /// For a PR's diff screen.
    Diff(DiffOf, DiffMsg),
    /// Text pasted into the terminal, whole.
    Paste(String),
    /// The terminal came into focus (`true`) or left it.
    Focus(bool),
    /// What the repository's clone (or code search) answered about code.
    Code(
        crate::code_nav::Ask,
        Result<crate::code_nav::Answer, String>,
    ),
    /// What GitHub answered when asked whether a watched pull request
    /// changed.
    Polled(
        PrRef,
        Vec<(crate::live::Probe, Result<crate::api::Polled, ApiError>)>,
    ),
    /// What GitHub answered when asked whether another watched page
    /// changed.
    Watched(
        crate::live::page::Watched,
        Vec<(
            crate::live::page::Probe,
            Result<crate::api::Polled, ApiError>,
        )>,
    ),
    /// What came of work in the background.
    Done(crate::work::Done),
    /// Whether GitHub can be reached, the queued writes, the kept
    /// repositories ([`crate::offline`]).
    Offline(crate::offline::Msg),
}

/// What arrives for a PR's diff: from its job, GitHub, or the drafts.
#[derive(Debug)]
pub enum DiffMsg {
    /// From the diff job; ignored unless it's the PR's current job.
    Job(JobId, JobMsg),
    ViewedLoaded(Box<Result<ViewedFiles, ApiError>>),
    /// The saved review state, or why it couldn't be read.
    ReviewLoaded(Result<ReviewState, Failure>),
    ThreadsLoaded(Result<Vec<ReviewThread>, ApiError>),
    PatchesLoaded(Result<Vec<PatchFile>, ApiError>),
    /// How many comments your pending review on GitHub holds.
    PendingReview(Result<u64, ApiError>),
    LastReview(Result<Option<String>, ApiError>),
    CommitsListed(Result<Vec<crate::git::repo::Commit>, Failure>),
}

/// What `update` asks the runtime to do.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cmd {
    /// A GitHub request.
    Api(Api),
    /// Work on a PR's diff (git and the diff engine).
    Git(Git),
    OpenUrl(String),
    /// Put text on the clipboard (OSC 52).
    Copy(String),
    /// Send [`Msg::Timer`] after this many milliseconds.
    Timer(Timer, u64),
    /// Send [`Msg::SuggestDue`] after a pause.
    SuggestLater(String),
    LoadReview(PrRef),
    SaveReview(PrRef, ReviewState),
    /// Suspend the TUI and edit `text` in `$EDITOR`.
    Edit {
        purpose: EditPurpose,
        text: String,
    },
    /// A write to GitHub, or git in your clone; answered by
    /// [`Msg::Done`].
    Work(crate::work::Work),
    /// Asking whether GitHub is back, the queue, the kept repositories;
    /// answered by [`Msg::Offline`].
    Offline(crate::offline::Cmd),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Api {
    FetchViewer,
    /// Fetch page data; with `cached`, first send what the cache has.
    Fetch {
        key: DataKey,
        cached: bool,
    },
    /// The next page of a search, after the cursor.
    FetchMore {
        key: DataKey,
        after: String,
    },
    SaveVisits(Vec<Visit>),
    FetchViewed(PrRef),
    FetchThreads(PrRef),
    FetchPatches(PrRef),
    /// How many comments your pending review on GitHub holds.
    FetchPendingReview(PrRef),
    FetchLastReview {
        pr: PrRef,
        login: String,
    },
    /// Whether a watched pull request changed, asked with ETags.
    Poll {
        pr: PrRef,
        asks: Vec<crate::live::Ask>,
    },
    /// Whether another watched page changed, asked with ETags.
    Watch {
        what: crate::live::page::Watched,
        asks: Vec<crate::live::Ask<crate::live::page::Probe>>,
    },
}

/// An outdated thread to place on the current diff: where it was.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutdatedThread {
    pub thread: NodeId,
    pub path: String,
    pub commit: String,
    pub line: u32,
}

/// Files' diffs, with each file's index in the listing.
pub type FileDiffs = Vec<(usize, Arc<FileDiff>)>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Git {
    /// Start (or restart) the diff job for a PR.
    /// With `range`, diff `(from, to)` instead of the whole PR.
    LoadDiff {
        of: DiffOf,
        job: JobId,
        /// What a PR's diff is from (empty for a commit).
        base: crate::diff_job::PrBase,
        range: Option<(String, String)>,
    },
    /// Diff these files next.
    Prioritize(DiffOf, Vec<usize>),
    /// Look for moved code; answered as the job's [`JobMsg::Moves`].
    DetectMoves(join::Joined<(DiffOf, JobId, FileDiffs)>),
    /// Outdated threads mapped onto a job's head, answered as its [`JobMsg::Mapped`].
    MapOutdated(join::Joined<(PrRef, JobId, Oid, Vec<OutdatedThread>)>),
    /// Block hashes of the PR's diff at an old head, for "since my last
    /// review"; answered as the job's [`JobMsg::Since`].
    SinceReview(join::Joined<(PrRef, JobId, String)>),
    ListCommits(PrRef),
    /// A question about the code at a revision (definitions, symbols,
    /// references), answered by [`Msg::Code`].
    Code(crate::code_nav::Ask),
}

/// Problems that last until they're fixed, shown in a banner above the
/// status bar instead of as a notice that expires.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Problem {
    /// GitHub rejects the token: nothing loads until it's fixed.
    Auth,
    /// Review drafts can't be written to disk.
    Drafts,
}

/// Things that happen later.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Timer {
    /// Clear notice number N, if it's still showing.
    ExpireNotice(u64),
    /// The next frame of the loading spinner.
    Spin,
    /// Relative times ("3m ago") move on.
    Minute,
    /// Fetch again what's on screen and still running.
    Live,
    /// Home's sections, on screen, are fetched again when old.
    Home,
    /// Poll the unread count, if this is still the latest poll.
    Unread(u64),
    /// Ask whether the pull request watched (by its watch's id) changed.
    Watch(u64),
    /// Ask whether GitHub can be reached again.
    Probe,
}

/// Braille spinner frames.
pub const SPINNER: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Screen {
    /// A GitHub page: home, repository, file, list, issue, PR, profile,
    /// search.
    Page(Box<PageScreen>),
    /// A pull request's files.
    Diff(Box<DiffScreen>),
}

/// Navigation history, like a browser's. The first screen can't be
/// removed, so there's always one to show.
pub struct Screens {
    first: Screen,
    rest: Vec<Screen>,
}

impl Screens {
    pub(crate) fn new(first: Screen) -> Self {
        Self {
            first,
            rest: Vec::new(),
        }
    }

    pub fn last(&self) -> &Screen {
        self.rest.last().unwrap_or(&self.first)
    }

    pub fn last_mut(&mut self) -> &mut Screen {
        self.rest.last_mut().unwrap_or(&mut self.first)
    }

    pub fn push(&mut self, screen: Screen) {
        self.rest.push(screen);
    }

    /// The screen under the last, if there's one.
    pub fn below(&self) -> Option<&Screen> {
        match self.rest.len() {
            0 => None,
            1 => Some(&self.first),
            n => self.rest.get(n - 2),
        }
    }

    /// The last screen, unless it's the first.
    pub fn pop(&mut self) -> Option<Screen> {
        self.rest.pop()
    }

    pub fn len(&self) -> usize {
        self.rest.len() + 1
    }
}

pub enum Overlay {
    /// `/` prompt: searching the diff or the page on screen.
    Find(Box<TextArea<'static>>),
    /// The header's search box with its suggestions.
    Search(Box<SearchBox>),
    /// Letters over the links on screen.
    Hints(Box<Hints>),
    /// Everything you can do here.
    Menu(Box<Menu>),
    /// The command palette, go to file, branches, commits.
    Picker(Box<Picker>),
    /// Writing a comment, reply or suggestion.
    Compose(Box<Compose>),
    /// Submitting the review.
    Submit(Box<SubmitDialog>),
    /// Asking before a write: a form's, or an action's change.
    Ask(Box<crate::forms::Ask>),
}

pub struct State {
    /// The tab on screen's history.
    pub screens: Screens,
    /// Pages gone back from.
    pub forward: Vec<Screen>,
    /// The open tabs before and after the one on screen, in order.
    pub before: Vec<Tab>,
    pub after: Vec<Tab>,
    /// Pages visited, for the search box.
    pub visits: Vec<Visit>,
    /// Home's sections, as `config.toml` has them.
    pub home: Vec<crate::home::Section>,
    /// Where they're written back to.
    pub config_path: Option<std::path::PathBuf>,
    /// The section just removed, where it was, as its TOML.
    pub removed: Option<(usize, String)>,
    /// A change to the sections is being written.
    pub home_editing: bool,
    /// [`Timer::Home`] is scheduled.
    pub home_timer: bool,
    /// Everything else pages show.
    pub data: HashMap<DataKey, Remote<Data>>,
    /// Bumped whenever what pages show besides GitHub's data and forms
    /// changes (writes on their way, who's signed in, whether GitHub can
    /// be reached), so pages know to rebuild; GitHub's data has its own
    /// versions ([`Remote`]), and forms theirs ([`crate::forms::Forms`]).
    pub shown_gen: u64,
    pub diffs: HashMap<DiffOf, DiffState>,
    pub viewer: Option<String>,
    /// The latest poll of the unread count: an older poll's timer is
    /// let go.
    pub unread_poll: u64,
    pub rate_limits: RateLimits,
    pub overlay: Option<Overlay>,
    pub notices: Notices,
    /// Lasting problems, most important first; the first is shown.
    pub problems: std::collections::BTreeMap<Problem, String>,
    /// Terminal size.
    pub size: (u16, u16),
    pub keymap: Keymap,
    /// The page's other tabs as they were left, to come back to.
    pub parked: Vec<Screen>,
    pub theme: Theme,
    pub icons: Icons,
    pub density: crate::config::Density,
    /// Unix seconds; pages show relative times.
    pub clock: fn() -> u64,
    /// When [`Timer::Minute`] last fired (Unix seconds): a page is built
    /// again for a time it shows only once it reads differently by then,
    /// so all of a minute's times move on together.
    pub minute: u64,
    /// The spinner's frame, and whether its next frame is scheduled.
    pub spinner: usize,
    pub spinning: bool,
    /// [`Timer::Live`] is scheduled.
    pub live: bool,
    /// A change GitHub accepted that its page doesn't show yet.
    pub following: Option<crate::follow::Following>,
    /// An action waiting for what it's about to load.
    pub pending: Option<act::Pending>,
    /// The pull request under review on screen, watched for changes.
    pub watch: Option<crate::live::Watch>,
    /// Another page on screen watched for changes: a running job, runs,
    /// an issue, a discussion.
    pub page_watch: Option<crate::live::page::PageWatch>,
    /// The terminal has the focus.
    pub focused: bool,
    /// The forms being written, by their page: leaving one keeps it.
    pub forms: crate::forms::Forms,
    /// What housekeeping's question is about, and a branch to delete.
    pub housekeeping: crate::housekeeping::Housekeeping,
    /// Conversations: long reviews unfolded, and the like.
    pub talk: crate::talk::Talk,
    /// Whether GitHub can be reached, the writes waiting for it, and the
    /// repositories kept offline.
    pub offline: crate::offline::Offline,
    pub quit: bool,
}

impl State {
    pub fn new(theme: Theme, icons: Icons, keymap: Keymap, size: (u16, u16)) -> Self {
        let mut state = Self {
            screens: Screens::new(Screen::Page(Box::new(PageScreen::new(
                Route::App(app::Home),
                None,
            )))),
            forward: Vec::new(),
            before: Vec::new(),
            after: Vec::new(),
            visits: Vec::new(),
            home: crate::home::defaults(),
            config_path: None,
            removed: None,
            home_editing: false,
            home_timer: false,
            data: HashMap::new(),
            shown_gen: 0,
            diffs: HashMap::new(),
            viewer: None,
            unread_poll: 0,
            rate_limits: RateLimits::default(),
            overlay: None,
            notices: Notices::default(),
            problems: Default::default(),
            size,
            keymap,
            parked: Vec::new(),
            theme,
            icons,
            density: crate::config::Density::default(),
            clock: crate::store::now,
            minute: 0,
            spinner: 0,
            spinning: false,
            live: false,
            following: None,
            pending: None,
            watch: None,
            page_watch: None,
            focused: true,
            forms: crate::forms::Forms::default(),
            housekeeping: crate::housekeeping::Housekeeping::default(),
            talk: crate::talk::Talk::default(),
            offline: crate::offline::Offline::default(),
            quit: false,
        };
        state.sync_page();
        state
    }

    /// Says `text` in the status bar for a few seconds.
    pub fn info(&mut self, text: impl Into<String>) {
        self.notices.info(text);
    }

    /// Says `text` in the status bar, and does nothing else: why an
    /// action can't go ahead, mostly.
    #[must_use]
    pub fn just_say(&mut self, text: impl Into<String>) -> Vec<Cmd> {
        self.info(text);
        Vec::new()
    }

    /// Says what went wrong, in the status bar (and the messages list).
    pub fn error(&mut self, text: impl Into<String>) {
        self.notices.error(text);
    }

    pub fn screen(&self) -> &Screen {
        self.screens.last()
    }

    pub fn screen_mut(&mut self) -> &mut Screen {
        self.screens.last_mut()
    }

    pub fn ctx(&self, now: u64) -> Ctx<'_> {
        Ctx {
            theme: &self.theme,
            icons: self.icons,
            now,
        }
    }

    /// The key contexts active, innermost first.
    pub fn contexts(&self) -> &'static [Context] {
        let innermost = match self.screen() {
            Screen::Diff(d) if d.focus == Pane::Tree && d.prefs.tree_visible => Context::Tree,
            Screen::Diff(_) => Context::Diff,
            Screen::Page(p) => match &p.route {
                Route::Runs(runs::Job { .. }) => Context::Log,
                Route::Code(code::Blob { .. }) | Route::Code(code::Blame { .. }) => Context::File,
                Route::Projects(projects::Project { .. })
                | Route::Projects(projects::ProjectItem { .. }) => Context::Project,
                Route::Settings(settings::Page { .. }) => Context::Settings,
                route if route.family().query().is_some() => Context::List,
                _ => Context::Page,
            },
        };
        innermost.stack()
    }

    /// The page on screen, if it is one.
    pub fn route(&self) -> Option<&Route> {
        match self.screen() {
            Screen::Page(p) => Some(&p.route),
            Screen::Diff(_) => None,
        }
    }
}

impl State {
    /// Rebuilds the page if its data changed and keeps the page (or the
    /// diff) in bounds.
    #[must_use]
    pub fn settle(&mut self) -> Vec<Cmd> {
        let mut cmds = self.settle_ref();
        self.sync_page();
        cmds.extend(self.settle_diff());
        cmds
    }

    /// Opens the page an unsplit one ([`code::Unsplit`]) is, once where its ref
    /// ends is known.
    #[must_use]
    fn settle_ref(&mut self) -> Vec<Cmd> {
        let Screen::Page(p) = self.screen() else {
            return Vec::new();
        };
        let Some(route) = self.split(&p.route, None) else {
            return Vec::new();
        };
        let anchor = p.anchor.clone();
        self.replace(route, false, anchor)
    }

    /// `route` followed from the page on screen: split where its ref ends
    /// if that's known, as when a page at a revision links to that
    /// revision's files.
    pub(crate) fn followed(&self, route: Route) -> Route {
        let rev = (self.route())
            .filter(|here| here.repo() == route.repo())
            .and_then(Route::rev);
        self.split(&route, rev).unwrap_or(route)
    }

    /// The page an unsplit one ([`code::Unsplit`]) is, if where its ref ends is
    /// known here: GitHub said, or its ref-and-path starts with the
    /// repository's default branch or with `rev` (the revision of the page
    /// that links to it). Git keeps a branch from being the start of
    /// another, so a branch already known is the one (short of a tag that
    /// extends it).
    pub(crate) fn split(&self, route: &Route, rev: Option<&str>) -> Option<Route> {
        let Route::Code(code::Unsplit { repo, spot, .. }) = route else {
            return None;
        };
        let key = DataKey::RefIn(repo.clone(), spot.clone());
        if let Some(found) = self.picked::<Option<String>>(&key) {
            return route.split(found.as_deref());
        }
        let default = self
            .overview(repo)
            .and_then(|o| o.default_branch.as_deref());
        let starts = |r: &&str| {
            spot.strip_prefix(*r)
                .is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
        };
        let known = [rev, default].into_iter().flatten().find(starts)?;
        route.split(Some(known))
    }
}

#[cfg(test)]
pub(crate) mod tests;