diff: Add file tree toggle

8f10de402acf9d6dd8fc52b7aa1b49b50315bf41
Alexis Sellier committed ago 1 parent fa048cae
crates/diff/src/bin/rustik-diff/session.rs +1 -1
20 20
const FOOTER_FG: Color = Color::Fixed(240);
21 21
/// Background color for the footer row.
22 22
const FOOTER_BG: Color = Color::Fixed(233);
23 23
24 24
/// Terminal backend used by the interactive viewer.
25 -
type DiffTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::StdoutScreen>>;
25 +
type DiffTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>;
26 26
27 27
/// Runs the terminal event loop.
28 28
pub(super) fn run(
29 29
    terminal: &mut DiffTerminal,
30 30
    viewer: &mut DiffViewer,
crates/diff/src/viewer.rs +33 -1
140 140
    CollapseFocusedFile,
141 141
    /// Move focus from the main diff into the file tree.
142 142
    EnterFileTree,
143 143
    /// Move focus from the file tree back to the main diff without changing selection.
144 144
    LeaveFileTree,
145 +
    /// Toggle the file tree between automatic visibility and hidden.
146 +
    ToggleFileTree,
145 147
    /// Move the file-tree cursor by visible file rows.
146 148
    MoveFileTree(isize),
147 149
    /// Extend the selected file range from the current anchor.
148 150
    ExtendFileTreeSelection(isize),
149 151
    /// Activate the current file-tree target.
216 218
    word_diffs: BTreeMap<HunkId, word::WordChanges>,
217 219
    /// Rendered commit-preamble rows for the most recent body width.
218 220
    preamble: Vec<Line>,
219 221
    /// File-tree pane state and model.
220 222
    file_tree: tree::FileTreeViewer,
223 +
    /// Interactive override for file-tree visibility.
224 +
    file_tree_visibility: FileTreeVisibility,
221 225
    /// Cached rendered row positions for visible files.
222 226
    index: RowIndex,
223 227
    /// Hunk-context expansion bookkeeping.
224 228
    context: ContextState,
225 229
}
326 330
    diff: Rect,
327 331
    /// File tree rectangle, when the tree is visible.
328 332
    tree: Option<Rect>,
329 333
}
330 334
335 +
/// Interactive override for the configured file-tree rendering policy.
336 +
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
337 +
enum FileTreeVisibility {
338 +
    /// Follow the configured file-tree rendering policy.
339 +
    #[default]
340 +
    Auto,
341 +
    /// Hide the file tree until it is toggled back on.
342 +
    Hidden,
343 +
}
344 +
331 345
impl Default for DiffOptions {
332 346
    fn default() -> Self {
333 347
        Self {
334 348
            highlight_lookahead: DEFAULT_HIGHLIGHT_LOOKAHEAD,
335 349
            syntax: None,
406 420
            highlights: BTreeMap::new(),
407 421
            pending_highlights: BTreeSet::new(),
408 422
            word_diffs: BTreeMap::new(),
409 423
            preamble,
410 424
            file_tree,
425 +
            file_tree_visibility: FileTreeVisibility::default(),
411 426
            index: RowIndex::default(),
412 427
            context: ContextState::new(),
413 428
        };
414 429
        viewer.rebuild_index();
415 430
        viewer
552 567
    }
553 568
554 569
    /// Returns the viewport layout for `area`.
555 570
    fn layout(&self, area: Rect) -> DiffLayout {
556 571
        if self.options.file_tree != FileTreeMode::Auto
572 +
            || self.file_tree_visibility == FileTreeVisibility::Hidden
557 573
            || area.cols <= tree::MIN_VIEW_COLS
558 574
            || self.document.files.is_empty()
559 575
        {
560 576
            return DiffLayout {
561 577
                diff: area,
616 632
    }
617 633
618 634
    /// Applies an action to the viewer state.
619 635
    pub fn apply(&mut self, action: DiffAction, area: Rect) -> ViewChange {
620 636
        let before = self.state.clone();
637 +
        let before_visibility = self.file_tree_visibility;
621 638
        let layout = self.layout(area);
622 639
        let diff = layout.diff;
623 640
624 641
        match action {
625 642
            DiffAction::Scroll(scroll) => {
662 679
                }
663 680
            }
664 681
            DiffAction::LeaveFileTree => {
665 682
                self.state.pane = DiffPane::Main;
666 683
            }
684 +
            DiffAction::ToggleFileTree => {
685 +
                match self.file_tree_visibility {
686 +
                    FileTreeVisibility::Hidden => {
687 +
                        self.file_tree_visibility = FileTreeVisibility::Auto;
688 +
                    }
689 +
                    FileTreeVisibility::Auto if layout.tree.is_some() => {
690 +
                        self.file_tree_visibility = FileTreeVisibility::Hidden;
691 +
                    }
692 +
                    FileTreeVisibility::Auto => {}
693 +
                }
694 +
                if self.file_tree_visibility == FileTreeVisibility::Hidden {
695 +
                    self.state.pane = DiffPane::Main;
696 +
                }
697 +
            }
667 698
            DiffAction::MoveFileTree(delta) => {
668 699
                self.file_tree.select(delta);
669 700
            }
670 701
            DiffAction::ExtendFileTreeSelection(delta) => {
671 702
                self.state.file_selection = self.file_tree.extend_selection(delta);
686 717
687 718
        if before.invalidates_index(&self.state) {
688 719
            self.rebuild_index();
689 720
            self.state.scroll = View::new(self.state.scroll, self.index.rows, diff.rows).offset;
690 721
        }
691 -
        if self.state == before {
722 +
        if self.state == before && self.file_tree_visibility == before_visibility {
692 723
            ViewChange::Unchanged
693 724
        } else {
694 725
            ViewChange::Changed
695 726
        }
696 727
    }
730 761
        context_wheel: bool,
731 762
        mut apply_context: impl FnMut(&mut Self, ContextChange, Rect) -> Result<Control, E>,
732 763
    ) -> Result<Control, E> {
733 764
        let action = match event {
734 765
            Event::Key(Key::Char('q')) => return Ok(Control::Quit),
766 +
            Event::Key(Key::Char('`')) => DiffAction::ToggleFileTree,
735 767
            Event::Key(Key::Char('.')) => {
736 768
                return apply_context(self, ContextChange::Increase, area);
737 769
            }
738 770
            Event::Key(Key::Char(',')) => {
739 771
                return apply_context(self, ContextChange::Decrease, area);
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
src/a.rs +0 -0
src/b.rs +0 -0
README.md +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
crates/tui-termion/src/term.rs +28 -7
2 2
//!
3 3
//! This module owns the raw-screen setup and event iterator plumbing needed to
4 4
//! run a `rustik-tui` app on a real terminal. Keeping it outside the core crate avoids
5 5
//! tying widget rendering to termion.
6 6
7 -
use std::fs::File;
7 +
use std::fs::{File, OpenOptions};
8 8
use std::io;
9 9
use std::io::Write as _;
10 10
use std::sync::mpsc;
11 11
use std::thread;
12 12
31 31
/// Process stdout descriptor path used as a terminal fallback.
32 32
const STDOUT_DEVICE: &str = "/proc/self/fd/1";
33 33
/// Process stderr descriptor path used as a terminal fallback.
34 34
const STDERR_DEVICE: &str = "/proc/self/fd/2";
35 35
36 -
/// Raw alternate stdout screen.
37 -
pub type StdoutScreen = AlternateScreen<MouseTerminal<RawTerminal<io::Stdout>>>;
36 +
/// Raw alternate terminal-device screen.
37 +
pub type TerminalScreen = AlternateScreen<MouseTerminal<RawTerminal<File>>>;
38 38
39 -
/// Creates a raw alternate-screen backend on stdout.
40 -
pub fn screen() -> io::Result<TermionBackend<StdoutScreen>> {
41 -
    let screen = MouseTerminal::from(io::stdout().into_raw_mode()?).into_alternate_screen()?;
39 +
/// Creates a raw alternate-screen backend on the terminal device.
40 +
pub fn screen() -> io::Result<TermionBackend<TerminalScreen>> {
41 +
    let screen =
42 +
        MouseTerminal::from(terminal_screen()?.into_raw_mode()?).into_alternate_screen()?;
42 43
    Ok(TermionBackend::new(screen))
43 44
}
44 45
45 46
/// Returns typed events from the terminal.
46 47
pub fn events() -> impl Iterator<Item = io::Result<Event<()>>> {
56 57
    Ok(terminal
57 58
        .events_and_raw()
58 59
        .map(|event| event.map(TermionEvent::from).map(Event::from)))
59 60
}
60 61
62 +
/// Opens the terminal device used for interactive screen output.
63 +
fn terminal_screen() -> io::Result<File> {
64 +
    OpenOptions::new()
65 +
        .read(true)
66 +
        .write(true)
67 +
        .open(TTY_DEVICE)
68 +
        .or_else(|_| {
69 +
            OpenOptions::new()
70 +
                .read(true)
71 +
                .write(true)
72 +
                .open(STDOUT_DEVICE)
73 +
        })
74 +
        .or_else(|_| {
75 +
            OpenOptions::new()
76 +
                .read(true)
77 +
                .write(true)
78 +
                .open(STDERR_DEVICE)
79 +
        })
80 +
}
81 +
61 82
/// Returns the current terminal viewport size.
62 83
fn viewport() -> Option<Size> {
63 84
    termion::terminal_size()
64 85
        .map(|(cols, rows)| Size::new(cols as usize, rows as usize))
65 86
        .ok()
95 116
}
96 117
97 118
/// Runs the application event loop.
98 119
fn event_loop<A>(
99 120
    app: &mut A,
100 -
    terminal: &mut Terminal<TermionBackend<StdoutScreen>>,
121 +
    terminal: &mut Terminal<TermionBackend<TerminalScreen>>,
101 122
) -> io::Result<()>
102 123
where
103 124
    A: App<Event = ()>,
104 125
{
105 126
    terminal.draw(|frame| app.render(frame))?;