chronogit 0.3.0

A read-only terminal UI for Git history, diffs, and source code
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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! The authoritative application model and its bounded view-specific state.

use std::collections::{HashMap, VecDeque};

use crate::app::{Action, Event, GitEffect, SearchState};
use crate::domain::{
    ChangedFile, CommitMessage, CommitSummary, DiffDocument, DiffTarget, FileDocument, ObjectId,
    RepoPath, RepositoryRoot, SearchHit, TreeEntry, WorktreeChange,
};

const MAX_DIFF_CACHE_ENTRIES: usize = 16;
const MAX_DIFF_CACHE_BYTES: usize = 16 * 1024 * 1024;

/// The main screen currently rendered by the TUI.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AppView {
    /// Unstaged worktree paths and an index-to-worktree diff.
    Changes,
    /// Commit list, changed files or tree, and selected diff.
    History,
    /// Commit list, full message body, and changed files.
    CommitDetails,
    /// Commit list rendered with parent lanes.
    Graph,
    /// Graph plus changed-file and diff details.
    GraphDetails,
    /// History and current content for one repository path.
    FileHistory,
    /// Expandable working-tree file tree and current source content.
    Code,
}

/// The pane receiving navigation actions in the current view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FocusedPane {
    /// The first list or search-input pane.
    Primary,
    /// The second list, body, or search-results pane.
    Secondary,
    /// The diff or document pane.
    Diff,
}

/// Content displayed in the middle section of the history view.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HistoryPanel {
    /// Paths changed relative to the commit baseline.
    ChangedFiles,
    /// Lazily expandable entries from the selected commit tree.
    Tree,
}

/// A modal surface drawn above a main [`AppView`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Overlay {
    /// No modal surface is active.
    None,
    /// Built-in key and navigation help.
    Help,
    /// The selected commit's complete message.
    CommitMessage,
    /// A full-screen diff document.
    Diff,
    /// Repository-wide file-name or content search.
    RepositorySearch,
    /// A full-screen current working-tree file.
    FileContent,
    /// A full-screen file opened from the code viewer.
    CodeContent,
}

/// Repository-wide search mode.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RepositorySearchKind {
    /// Match fixed text against tracked and untracked path names.
    Files,
    /// Match fixed text within non-binary working-tree files.
    Content,
}

impl RepositorySearchKind {
    /// Returns the lowercase label shown in the search prompt.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Files => "files",
            Self::Content => "content",
        }
    }
}

/// A monotonically increasing identifier attached to asynchronous work.
///
/// The value is scoped to one [`AppState`]. Reducers compare it with the current
/// [`LoadState::Loading`] request to discard stale completions.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RequestId(u64);

impl RequestId {
    /// Returns the numeric identifier for executor-side atomic comparison.
    #[must_use]
    pub fn value(self) -> u64 {
        self.0
    }
}

/// A sanitized, display-ready recoverable error message.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ErrorNotice(String);

impl ErrorNotice {
    /// Creates a notice from boundary-provided text.
    #[must_use]
    pub fn new(message: impl Into<String>) -> Self {
        Self(message.into())
    }

    /// Returns the text shown in the affected pane or footer.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.0
    }
}

/// The lifecycle of one asynchronously loaded resource.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LoadState<T> {
    /// No request has been issued for the current resource.
    Idle,
    /// A request is in flight.
    Loading {
        /// Identifier required for a completion to be accepted.
        request_id: RequestId,
    },
    /// Data for the current resource is available.
    Ready(T),
    /// The most recent request failed and may be retried by user action.
    Failed(ErrorNotice),
}

impl<T> LoadState<T> {
    /// Returns the current request identifier only while loading.
    #[must_use]
    pub fn loading_request(&self) -> Option<RequestId> {
        match self {
            Self::Loading { request_id } => Some(*request_id),
            Self::Idle | Self::Ready(_) | Self::Failed(_) => None,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Selection {
    index: Option<usize>,
}

impl Selection {
    pub(crate) fn new() -> Self {
        Self { index: None }
    }

    pub(crate) fn index(&self) -> Option<usize> {
        self.index
    }

    pub(crate) fn reset(&mut self, len: usize) {
        self.index = (len > 0).then_some(0);
    }

    pub(crate) fn reset_to(&mut self, len: usize, preferred: Option<usize>) {
        self.index = if len == 0 {
            None
        } else {
            Some(preferred.unwrap_or(0).min(len - 1))
        };
    }

    pub(crate) fn clamp(&mut self, len: usize) {
        self.index = match (self.index, len) {
            (_, 0) => None,
            (Some(index), _) => Some(index.min(len - 1)),
            (None, _) => Some(0),
        };
    }

    pub(crate) fn move_by(&mut self, delta: isize, len: usize) -> bool {
        let previous = self.index;
        if len == 0 {
            self.index = None;
            return previous != self.index;
        }
        let current = self.index.unwrap_or(0);
        let next = current.saturating_add_signed(delta).min(len - 1);
        self.index = Some(next);
        previous != self.index
    }

    pub(crate) fn top(&mut self, len: usize) -> bool {
        let previous = self.index;
        self.index = (len > 0).then_some(0);
        previous != self.index
    }

    pub(crate) fn bottom(&mut self, len: usize) -> bool {
        let previous = self.index;
        self.index = len.checked_sub(1);
        previous != self.index
    }
}

/// A flattened commit-tree entry ready for list rendering.
///
/// The underlying [`TreeEntry`] name is relative to its direct parent, while
/// [`VisibleTreeEntry::path`] stores the complete repository-relative path.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VisibleTreeEntry {
    entry: TreeEntry,
    path: RepoPath,
    depth: usize,
    expanded: bool,
}

impl VisibleTreeEntry {
    /// Creates a collapsed visible entry at the supplied tree depth.
    #[must_use]
    pub fn new(entry: TreeEntry, path: RepoPath, depth: usize) -> Self {
        Self {
            entry,
            path,
            depth,
            expanded: false,
        }
    }

    /// Returns the direct Git tree entry.
    #[must_use]
    pub fn entry(&self) -> &TreeEntry {
        &self.entry
    }

    /// Returns the complete repository-relative path.
    #[must_use]
    pub fn path(&self) -> &RepoPath {
        &self.path
    }

    /// Returns the zero-based nesting depth used for indentation.
    #[must_use]
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Reports whether a directory's loaded children are currently visible.
    #[must_use]
    pub fn expanded(&self) -> bool {
        self.expanded
    }

    pub(crate) fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }
}

#[derive(Clone, Debug)]
pub(crate) struct DiffViewState {
    pub(crate) target: Option<DiffTarget>,
    pub(crate) content: LoadState<DiffDocument>,
    pub(crate) vertical: usize,
    pub(crate) horizontal: usize,
}

#[derive(Clone, Debug)]
pub(crate) struct MessageState {
    pub(crate) commit: Option<ObjectId>,
    pub(crate) content: LoadState<CommitMessage>,
    pub(crate) scroll: usize,
}

#[derive(Clone, Debug)]
pub(crate) struct HistoryPageState {
    pub(crate) has_more: bool,
    pub(crate) loading_more: Option<RequestId>,
}

#[derive(Clone, Debug)]
pub(crate) struct TreeState {
    pub(crate) commit: Option<ObjectId>,
    pub(crate) visible: LoadState<Vec<VisibleTreeEntry>>,
    pub(crate) selection: Selection,
    pub(crate) children: HashMap<ObjectId, Vec<TreeEntry>>,
    pub(crate) pending: Option<RequestId>,
}

#[derive(Debug)]
pub(crate) struct RepositorySearchState {
    pub(crate) kind: RepositorySearchKind,
    pub(crate) prompt: Option<String>,
    pub(crate) query: String,
    pub(crate) results: LoadState<Vec<SearchHit>>,
    pub(crate) selection: Selection,
    pub(crate) return_view: AppView,
}

impl RepositorySearchState {
    fn new(view: AppView) -> Self {
        Self {
            kind: RepositorySearchKind::Files,
            prompt: None,
            query: String::new(),
            results: LoadState::Idle,
            selection: Selection::new(),
            return_view: view,
        }
    }
}

#[derive(Debug)]
pub(crate) struct FileViewState {
    pub(crate) path: Option<RepoPath>,
    pub(crate) commits: LoadState<Vec<CommitSummary>>,
    pub(crate) selection: Selection,
    pub(crate) content: LoadState<FileDocument>,
    pub(crate) showing_history_diff: bool,
    pub(crate) vertical: usize,
    pub(crate) horizontal: usize,
    pub(crate) return_view: AppView,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CodeEntryKind {
    Directory,
    File,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct VisibleCodeEntry {
    path: RepoPath,
    name: RepoPath,
    depth: usize,
    kind: CodeEntryKind,
    expanded: bool,
}

impl VisibleCodeEntry {
    pub(crate) fn new(path: RepoPath, name: RepoPath, depth: usize, kind: CodeEntryKind) -> Self {
        Self {
            path,
            name,
            depth,
            kind,
            expanded: false,
        }
    }

    pub(crate) fn path(&self) -> &RepoPath {
        &self.path
    }

    pub(crate) fn name(&self) -> &RepoPath {
        &self.name
    }

    pub(crate) fn depth(&self) -> usize {
        self.depth
    }

    pub(crate) fn kind(&self) -> CodeEntryKind {
        self.kind
    }

    pub(crate) fn expanded(&self) -> bool {
        self.expanded
    }

    pub(crate) fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }
}

#[derive(Debug)]
pub(crate) struct CodeViewState {
    pub(crate) files: Vec<RepoPath>,
    pub(crate) visible: LoadState<Vec<VisibleCodeEntry>>,
    pub(crate) selection: Selection,
    pub(crate) path: Option<RepoPath>,
    pub(crate) content: LoadState<FileDocument>,
    pub(crate) vertical: usize,
    pub(crate) horizontal: usize,
    pub(crate) pending_reveal: Option<RepoPath>,
}

impl CodeViewState {
    fn new() -> Self {
        Self {
            files: Vec::new(),
            visible: LoadState::Idle,
            selection: Selection::new(),
            path: None,
            content: LoadState::Idle,
            vertical: 0,
            horizontal: 0,
            pending_reveal: None,
        }
    }
}

impl FileViewState {
    fn new(view: AppView) -> Self {
        Self {
            path: None,
            commits: LoadState::Idle,
            selection: Selection::new(),
            content: LoadState::Idle,
            showing_history_diff: false,
            vertical: 0,
            horizontal: 0,
            return_view: view,
        }
    }
}

#[derive(Debug)]
struct DiffCache {
    entries: VecDeque<(DiffTarget, DiffDocument, usize)>,
    bytes: usize,
}

impl DiffCache {
    fn new() -> Self {
        Self {
            entries: VecDeque::new(),
            bytes: 0,
        }
    }

    fn get(&mut self, target: &DiffTarget) -> Option<DiffDocument> {
        let index = self.entries.iter().position(|(key, _, _)| key == target)?;
        let entry = self.entries.remove(index)?;
        let result = entry.1.clone();
        self.entries.push_front(entry);
        Some(result)
    }

    fn insert(&mut self, target: DiffTarget, document: DiffDocument) {
        let size = document.approximate_bytes();
        if size > MAX_DIFF_CACHE_BYTES {
            return;
        }
        if let Some(index) = self.entries.iter().position(|(key, _, _)| key == &target)
            && let Some((_, _, old_size)) = self.entries.remove(index)
        {
            self.bytes = self.bytes.saturating_sub(old_size);
        }
        self.bytes = self.bytes.saturating_add(size);
        self.entries.push_front((target, document, size));
        while self.entries.len() > MAX_DIFF_CACHE_ENTRIES || self.bytes > MAX_DIFF_CACHE_BYTES {
            if let Some((_, _, removed)) = self.entries.pop_back() {
                self.bytes = self.bytes.saturating_sub(removed);
            } else {
                break;
            }
        }
    }

    fn clear(&mut self) {
        self.entries.clear();
        self.bytes = 0;
    }
}

/// The complete mutable state consumed by ChronoGit's reducer and renderer.
///
/// State owns selections, load lifecycles, overlay navigation, stale-request
/// tracking, and the bounded diff cache. External callers drive it exclusively
/// through [`AppState::start`], [`AppState::handle_action`], and
/// [`AppState::handle_event`]; fields remain crate-visible so the renderer can
/// borrow them without exposing mutation as a public contract.
#[derive(Debug)]
pub struct AppState {
    pub(crate) root: RepositoryRoot,
    pub(crate) view: AppView,
    pub(crate) focus: FocusedPane,
    pub(crate) history_panel: HistoryPanel,
    pub(crate) overlay: Overlay,
    pub(crate) should_quit: bool,
    pub(crate) changes: LoadState<Vec<WorktreeChange>>,
    pub(crate) change_selection: Selection,
    pub(crate) commits: LoadState<Vec<CommitSummary>>,
    pub(crate) commit_selection: Selection,
    pub(crate) history_page: HistoryPageState,
    pub(crate) files: LoadState<Vec<ChangedFile>>,
    pub(crate) file_selection: Selection,
    pub(crate) diff: DiffViewState,
    pub(crate) message: MessageState,
    pub(crate) tree: TreeState,
    pub(crate) search: SearchState,
    pub(crate) repository_search: RepositorySearchState,
    pub(crate) file_view: FileViewState,
    pub(crate) code_view: CodeViewState,
    pub(crate) notice: Option<ErrorNotice>,
    pub(crate) preferred_change: Option<RepoPath>,
    pub(crate) preferred_commit: Option<ObjectId>,
    next_request: u64,
    diff_cache: DiffCache,
}

impl AppState {
    /// Creates an idle application rooted at a discovered repository.
    ///
    /// Call [`AppState::start`] after constructing the matching effect executor
    /// to request data for `view`.
    #[must_use]
    pub fn new(root: RepositoryRoot, view: AppView) -> Self {
        Self {
            root,
            view,
            focus: FocusedPane::Primary,
            history_panel: HistoryPanel::ChangedFiles,
            overlay: Overlay::None,
            should_quit: false,
            changes: LoadState::Idle,
            change_selection: Selection::new(),
            commits: LoadState::Idle,
            commit_selection: Selection::new(),
            history_page: HistoryPageState {
                has_more: false,
                loading_more: None,
            },
            files: LoadState::Idle,
            file_selection: Selection::new(),
            diff: DiffViewState {
                target: None,
                content: LoadState::Idle,
                vertical: 0,
                horizontal: 0,
            },
            message: MessageState {
                commit: None,
                content: LoadState::Idle,
                scroll: 0,
            },
            tree: TreeState {
                commit: None,
                visible: LoadState::Idle,
                selection: Selection::new(),
                children: HashMap::new(),
                pending: None,
            },
            search: SearchState::new(),
            repository_search: RepositorySearchState::new(view),
            file_view: FileViewState::new(view),
            code_view: CodeViewState::new(),
            notice: None,
            preferred_change: None,
            preferred_commit: None,
            next_request: 1,
            diff_cache: DiffCache::new(),
        }
    }

    /// Starts initial loading for the configured main view.
    ///
    /// Calling this again issues a fresh request; callers should normally invoke
    /// it once when entering the terminal loop.
    pub fn start(&mut self) -> Vec<GitEffect> {
        match self.view {
            AppView::Changes => self.request_changes(),
            AppView::History | AppView::CommitDetails | AppView::Graph | AppView::GraphDetails => {
                self.request_commits(false)
            }
            AppView::FileHistory => Vec::new(),
            AppView::Code => crate::app::code_view::request_tree(self),
        }
    }

    /// Applies semantic user input and returns any resulting repository work.
    pub fn handle_action(&mut self, action: Action) -> Vec<GitEffect> {
        crate::app::update::apply_action(self, action)
    }

    /// Applies an asynchronous completion and returns follow-up repository work.
    ///
    /// Obsolete events are ignored according to their request ID and resource
    /// identity.
    pub fn handle_event(&mut self, event: Event) -> Vec<GitEffect> {
        crate::app::update::apply_event(self, event)
    }

    /// Reports whether the event loop should exit without dispatching new work.
    #[must_use]
    pub fn should_quit(&self) -> bool {
        self.should_quit
    }

    /// Returns the current main view.
    #[must_use]
    pub fn view(&self) -> AppView {
        self.view
    }

    /// Returns the pane receiving navigation actions.
    #[must_use]
    pub fn focus(&self) -> FocusedPane {
        self.focus
    }

    /// Returns the active modal overlay.
    #[must_use]
    pub fn overlay(&self) -> Overlay {
        self.overlay
    }

    /// Reports whether ordinary character keys should edit a search prompt.
    #[must_use]
    pub fn is_search_input_active(&self) -> bool {
        self.search.is_input_active()
            || (self.overlay == Overlay::RepositorySearch
                && self.repository_search.prompt.is_some())
    }

    pub(crate) fn request_id(&mut self) -> RequestId {
        let id = RequestId(self.next_request);
        self.next_request = self.next_request.saturating_add(1);
        id
    }

    pub(crate) fn request_changes(&mut self) -> Vec<GitEffect> {
        let request_id = self.request_id();
        self.changes = LoadState::Loading { request_id };
        self.notice = None;
        vec![GitEffect::LoadChanges { request_id }]
    }

    pub(crate) fn request_commits(&mut self, append: bool) -> Vec<GitEffect> {
        const PAGE_SIZE: usize = 200;
        let request_id = self.request_id();
        let skip = if append {
            match &self.commits {
                LoadState::Ready(commits) => commits.len(),
                _ => 0,
            }
        } else {
            self.commits = LoadState::Loading { request_id };
            self.history_page.loading_more = None;
            0
        };
        if append {
            self.history_page.loading_more = Some(request_id);
        }
        self.notice = None;
        vec![GitEffect::LoadCommits {
            request_id,
            skip,
            limit: PAGE_SIZE,
            append,
        }]
    }

    pub(crate) fn request_diff(&mut self, target: DiffTarget) -> Vec<GitEffect> {
        self.diff.vertical = 0;
        self.diff.horizontal = 0;
        self.search.clear();
        if let Some(cached) = self.diff_cache.get(&target) {
            self.diff.target = Some(target);
            self.diff.content = LoadState::Ready(cached);
            return Vec::new();
        }
        let request_id = self.request_id();
        self.diff.target = Some(target.clone());
        self.diff.content = LoadState::Loading { request_id };
        vec![GitEffect::LoadDiff { request_id, target }]
    }

    pub(crate) fn cache_diff(&mut self, target: DiffTarget, document: DiffDocument) {
        self.diff_cache.insert(target, document);
    }

    pub(crate) fn clear_cache(&mut self) {
        self.diff_cache.clear();
    }
}