plannotator-tui 0.13.1

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
//! The diff's settings: what they are now, changing them from the settings menu (`,`), the
//! header's switches, `u`, `w`, `W`, `(`/`)` and the line between a split diff's sides, and
//! remembering the person's choices for the next review.
//!
//! Three layers: the defaults, `[review]` in the config file (the person's, never written),
//! and the choices remembered in the data folder (`state`). The menu's Reset forgets the
//! choices and goes back to the config file's values.
//!
//! `layout = "auto"` is split or unified by the terminal's width (`auto`), never by the panes
//! beside the diff.
//!
//! The settings here are the truth; the viewers follow them (`Diff::apply`), the main diff
//! and the whole-file view alike. A change is remembered once nothing is being dragged, so a
//! drag of the line between the sides is written once, when it ends.

mod auto;
mod menu;
pub(crate) mod state;
mod whitespace;

use std::path::PathBuf;

use crate::config::review::{
    Background, DiffTheme, InlineChanges, Layout, Notes, ReviewConfig, SplitShare, TabWidth, Whitespace,
};
use crate::theme::{Theme, theme};

use super::{Mode, Screen};

/// The split ratio's step for the keys and the menu: five hundredths of the width.
const SPLIT_STEP: u16 = 50;

/// The settings in force, and what they are measured against.
#[derive(Debug, Clone)]
pub(crate) struct Settings {
    pub(super) now: ReviewConfig,
    /// `[review]` in the config file: what Reset goes back to.
    config: ReviewConfig,
    /// The settings as this review last read or wrote the state file: a key that differs
    /// from it is one changed here, and only those are written.
    remembered: ReviewConfig,
    /// The state file; `None` remembers nothing.
    path: Option<PathBuf>,
    /// What the review says once when it opens: a state file it could not wholly read.
    notice: Option<String>,
    /// A write failed and the footer said so: later ones stay quiet.
    failed: bool,
    /// What `auto` is for the terminal drawn last (`auto`): split or not; `None` before
    /// the first frame.
    auto_split: Option<bool>,
    /// The whitespace the viewers draw with: `now`'s once the change is taken
    /// (`whitespace`), which waits for a frame or another key.
    shown_whitespace: Whitespace,
    /// How `W` hides whitespace: as it was hidden last in this review, else as the remembered
    /// choice or `[review]` hides it, else as `git diff -w`.
    hide_as: Whitespace,
}

impl Default for Settings {
    fn default() -> Self {
        Self::load(ReviewConfig::DEFAULT, None)
    }
}

impl Settings {
    /// `config` (the config file's `[review]`) with the choices remembered at `path` over it.
    pub(crate) fn load(config: ReviewConfig, path: Option<PathBuf>) -> Self {
        let (now, notice) = path.as_deref().map_or((config, None), |path| state::load(path, config));
        let hide_as = [now.whitespace, config.whitespace]
            .into_iter()
            .find(|w| *w != Whitespace::Show)
            .unwrap_or(Whitespace::IgnoreAll);
        Self {
            now,
            config,
            remembered: now,
            path,
            notice,
            failed: false,
            auto_split: None,
            shown_whitespace: now.whitespace,
            hide_as,
        }
    }

    /// Whether choices are remembered for the next review.
    pub(super) fn remembers(&self) -> bool {
        self.path.is_some()
    }

    /// What to say when the review opens, once.
    pub(super) fn take_notice(&mut self) -> Option<String> {
        self.notice.take()
    }
}

/// One row of the settings menu.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Row {
    Layout,
    SplitRatio,
    Wrap,
    /// Where the notes show: the panel, boxes in the diff, or both.
    Notes,
    LineNumbers,
    Whitespace,
    TabWidth,
    InlineChanges,
    Theme,
    Background,
    /// Forget the remembered choices: back to the config file's values and the defaults.
    Reset,
}

/// The menu's rows, in display order.
pub(super) const ROWS: [Row; 11] = [
    Row::Layout,
    Row::SplitRatio,
    Row::Wrap,
    Row::Notes,
    Row::LineNumbers,
    Row::Whitespace,
    Row::TabWidth,
    Row::InlineChanges,
    Row::Theme,
    Row::Background,
    Row::Reset,
];

/// The tab widths the menu steps through; the config file can set any from 1 to 16.
const TAB_WIDTHS: [u8; 3] = [2, 4, 8];

impl Row {
    pub(super) fn label(self) -> &'static str {
        match self {
            Self::Layout => "Layout",
            Self::SplitRatio => "Split ratio",
            Self::Wrap => "Wrap",
            Self::Notes => "Notes",
            Self::LineNumbers => "Line numbers",
            Self::Whitespace => "Whitespace",
            Self::TabWidth => "Tab width",
            Self::InlineChanges => "Changed text",
            Self::Theme => "Colors",
            Self::Background => "Background",
            Self::Reset => "Reset to defaults",
        }
    }
}

/// How a row's value moves: back (left), on (right), or Enter's move.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Step {
    Back,
    On,
    /// The next value; on the split ratio, back to even.
    Enter,
}

/// The value after `now` in `values`, or before it, wrapping around.
fn cycle<T: Copy + PartialEq>(values: &[T], now: T, back: bool) -> T {
    let at = values.iter().position(|v| *v == now).unwrap_or(0);
    let next = if back { at + values.len() - 1 } else { at + 1 };
    values.get(next % values.len()).copied().unwrap_or(now)
}

/// `from` rounded to the nearest [`SPLIT_STEP`], then a step right (toward the new side's
/// loss) or left.
fn clean_step(from: SplitShare, right: bool) -> SplitShare {
    let rounded = (from.permille() + SPLIT_STEP / 2) / SPLIT_STEP * SPLIT_STEP;
    SplitShare::from_permille(if right { rounded + SPLIT_STEP } else { rounded - SPLIT_STEP })
}

/// The menu's tab width after `now`: the next of 2, 4 and 8 up or down, so a width the config
/// file set between them steps to its neighbours.
fn tab_step(now: TabWidth, back: bool) -> TabWidth {
    let now = now.get();
    let next = if back {
        TAB_WIDTHS.iter().rev().find(|&&w| w < now).or(TAB_WIDTHS.last())
    } else {
        TAB_WIDTHS.iter().find(|&&w| w > now).or(TAB_WIDTHS.first())
    };
    TabWidth::new(next.copied().unwrap_or(now))
}

fn on_off(on: bool) -> &'static str {
    if on { "on" } else { "off" }
}

/// The whitespace setting as the menu, the footer and the help name it: plain words, then the
/// `git diff` option it matches.
fn whitespace_label(whitespace: Whitespace) -> &'static str {
    match whitespace {
        Whitespace::Show => "show",
        Whitespace::IgnoreAll => "hide (like git -w)",
        Whitespace::IgnoreChange => "hide amount changes (git -b)",
    }
}

/// Whether the app's own palette is the light one, which `auto` colours follow.
fn light() -> bool {
    theme() == Theme::Light
}

impl Screen {
    /// Bring the viewers to the settings; cheap when nothing differs.
    pub(super) fn apply_settings(&mut self) -> anyhow::Result<()> {
        let (settings, light) = (self.settings.for_viewer(), light());
        self.diff.apply(settings, light)?;
        if let Some(diff) = self.whole.as_mut().and_then(super::whole::Whole::diff_mut) {
            diff.apply(settings, light)?;
        }
        Ok(())
    }

    /// The settings changed: the viewers follow now, the state file once nothing is dragged.
    fn settings_changed(&mut self) {
        if let Err(error) = self.apply_settings() {
            self.status = Some(format!("{error:#}"));
        }
        if self.inline.view != self.settings.now.notes {
            self.inline.view = self.settings.now.notes;
            self.notes_view_changed();
        }
    }

    /// Remember the keys changed since the state file was last read or written here, once no
    /// drag of the line between the sides is under way. A failure is said once, in the
    /// footer; the settings still hold for this review.
    pub(super) fn remember_settings(&mut self) {
        let settings = &mut self.settings;
        if settings.now == settings.remembered || self.dividers.dragging_split() {
            return;
        }
        let changes = state::changes(settings.remembered, settings.now);
        settings.remembered = settings.now;
        let Some(path) = &settings.path else { return };
        if let Err(error) = state::write(path, &changes)
            && !std::mem::replace(&mut settings.failed, true)
        {
            self.status = Some(format!("{error:#}; the settings hold for this review"));
        }
    }

    /// The menu's Reset: forget the remembered choices, back to the config file's values.
    fn reset_settings(&mut self) {
        let settings = &mut self.settings;
        settings.now = settings.config;
        settings.remembered = settings.config;
        let forgotten = settings.path.as_deref().map_or(Ok(()), state::forget);
        self.settings_changed();
        self.status = Some(match forgotten {
            Err(error) => format!("{error:#}"),
            Ok(()) if self.settings.config == ReviewConfig::DEFAULT => "settings back to the defaults".into(),
            Ok(()) => "settings back to [review] in the config file".into(),
        });
    }

    /// The row's value as the menu shows it.
    pub(super) fn setting_value(&self, row: Row) -> String {
        let now = &self.settings.now;
        let split = self.diff.split();
        match row {
            Row::Layout => match now.layout {
                Layout::Auto => format!("auto \u{b7} {} now", if split { "split" } else { "unified" }),
                Layout::Split => "split".to_owned(),
                Layout::Unified => "unified".to_owned(),
            },
            Row::SplitRatio if split => now.split_ratio.to_string(),
            Row::SplitRatio => format!("{} \u{b7} when split", now.split_ratio),
            Row::Wrap => on_off(now.wrap).to_owned(),
            Row::Notes => match now.notes {
                Notes::Panel => "panel",
                Notes::Inline => "inline, in the diff",
                Notes::Both => "both, diff and panel",
            }
            .to_owned(),
            Row::LineNumbers => on_off(now.line_numbers).to_owned(),
            Row::Whitespace => whitespace_label(now.whitespace).to_owned(),
            Row::TabWidth => format!("{} columns", now.tab_width.get()),
            Row::InlineChanges => match now.inline_changes {
                InlineChanges::Word => "words",
                InlineChanges::WordAlt => "words, joined",
                InlineChanges::Char => "characters",
                InlineChanges::None => "off",
            }
            .to_owned(),
            Row::Theme => match now.theme {
                DiffTheme::Auto => format!("auto \u{b7} {}", if light() { "light" } else { "dark" }),
                DiffTheme::Dark => "dark".to_owned(),
                DiffTheme::Light => "light".to_owned(),
            },
            Row::Background => match now.background {
                Background::Terminal => "terminal".to_owned(),
                Background::Theme => "theme".to_owned(),
            },
            Row::Reset => String::new(),
        }
    }

    /// Change one setting from the menu, and say what it is now.
    pub(super) fn change_setting(&mut self, row: Row, step: Step) {
        if row == Row::Reset {
            self.reset_settings();
            return;
        }
        let back = step == Step::Back;
        let stepped = self.split_stepped(step == Step::On);
        let now = &mut self.settings.now;
        match row {
            Row::Layout => {
                now.layout = cycle(&[Layout::Auto, Layout::Split, Layout::Unified], now.layout, back);
            }
            Row::SplitRatio => {
                now.split_ratio = match step {
                    Step::Enter => SplitShare::EVEN,
                    Step::Back | Step::On => stepped,
                };
            }
            Row::Wrap => now.wrap = !now.wrap,
            Row::Notes => now.notes = cycle(&[Notes::Panel, Notes::Inline, Notes::Both], now.notes, back),
            Row::LineNumbers => now.line_numbers = !now.line_numbers,
            Row::Whitespace => {
                let order = [Whitespace::Show, Whitespace::IgnoreAll, Whitespace::IgnoreChange];
                now.whitespace = cycle(&order, now.whitespace, back);
            }
            Row::TabWidth => now.tab_width = tab_step(now.tab_width, back),
            Row::InlineChanges => {
                let order =
                    [InlineChanges::Word, InlineChanges::WordAlt, InlineChanges::Char, InlineChanges::None];
                now.inline_changes = cycle(&order, now.inline_changes, back);
            }
            Row::Theme => {
                now.theme = cycle(&[DiffTheme::Auto, DiffTheme::Dark, DiffTheme::Light], now.theme, back);
            }
            Row::Background => {
                now.background = cycle(&[Background::Terminal, Background::Theme], now.background, back);
            }
            Row::Reset => {}
        }
        self.settings_changed();
        self.status = Some(match row {
            Row::Whitespace => self.whitespace_status(),
            _ => format!("{} {}", row.label().to_lowercase(), self.setting_value(row)),
        });
    }

    /// The split ratio one step further, in clean steps: from the ratio drawn (which a narrow
    /// pane can hold nearer even than the one set, else the one set), rounded to the nearest
    /// step, then one step on: 0.52 goes to 0.55 or 0.45, 0.55 to 0.60 or 0.50.
    fn split_stepped(&self, right: bool) -> SplitShare {
        let drawn = self.diff.split_line().and_then(|line| self.diff.split_share_at(line));
        clean_step(drawn.unwrap_or(self.settings.now.split_ratio), right)
    }

    /// `u`: split if the diff is unified now, else unified.
    pub(super) fn toggle_layout(&mut self) {
        let split = !self.diff.split();
        self.pick_layout(split);
    }

    /// The header's switch: `split` or unified, whatever `auto` would pick. The layout in
    /// force already does nothing.
    pub(super) fn pick_layout(&mut self, split: bool) {
        if self.diff.split() == split {
            return;
        }
        self.settings.now.layout = if split { Layout::Split } else { Layout::Unified };
        self.settings_changed();
    }

    /// `w`, or the header's wrap switch.
    pub(super) fn toggle_wrap(&mut self) {
        self.settings.now.wrap = !self.settings.now.wrap;
        self.settings_changed();
    }

    /// `(`/`)`: the line between the sides, one step left or right.
    pub(super) fn step_split(&mut self, right: bool) {
        if !self.diff.split() {
            self.status = Some("the split ratio applies to the split layout".into());
            return;
        }
        self.settings.now.split_ratio = self.split_stepped(right);
        self.settings_changed();
    }

    /// The line between the sides dragged to screen `column`.
    pub(super) fn drag_split(&mut self, column: u16) {
        if let Some(share) = self.diff.split_share_at(column) {
            self.settings.now.split_ratio = share;
            self.settings_changed();
        }
    }

    /// A double click on the line between the sides: even again.
    pub(super) fn even_split(&mut self) {
        self.settings.now.split_ratio = SplitShare::EVEN;
        self.settings_changed();
    }

    /// `,` or the header's Settings button.
    pub(super) fn open_settings(&mut self) {
        self.mode = Mode::Settings(0);
    }
}

#[cfg(test)]
mod tests {
    use super::{SplitShare, TabWidth, clean_step, cycle, tab_step};

    #[test]
    fn the_split_keys_step_by_clean_twentieths() {
        let step = |from, right| clean_step(SplitShare::from_permille(from), right).permille();
        assert_eq!((step(500, true), step(550, true), step(600, false)), (550, 600, 550));
        assert_eq!((step(520, true), step(520, false)), (550, 450));
        assert_eq!((step(208, true), step(208, false)), (250, 150));
        assert_eq!((step(575, true), step(574, true)), (650, 600), "half way rounds up");
        assert_eq!((step(900, true), step(100, false)), (900, 100), "within the limits");
    }

    #[test]
    fn the_tab_width_steps_through_2_4_and_8_and_a_width_between_steps_to_its_neighbours() {
        let step = |from, back| tab_step(TabWidth::new(from), back).get();
        assert_eq!((step(2, false), step(4, false), step(8, false)), (4, 8, 2));
        assert_eq!((step(2, true), step(4, true), step(8, true)), (8, 2, 4));
        assert_eq!((step(3, false), step(3, true)), (4, 2));
        assert_eq!((step(16, false), step(16, true), step(1, true)), (2, 8, 8));
    }

    #[test]
    fn a_cycle_wraps_both_ways() {
        let values = [1, 2, 3];
        assert_eq!(cycle(&values, 1, false), 2);
        assert_eq!(cycle(&values, 3, false), 1);
        assert_eq!(cycle(&values, 1, true), 3);
        assert_eq!(cycle(&values, 2, true), 1);
    }
}