mod auto;
mod menu;
pub(crate) mod state;
mod whitespace;
use std::path::PathBuf;
use crate::config::review::{
Background, DiffTheme, InlineChanges, Layout, ReviewConfig, SplitShare, TabWidth, Whitespace,
};
use crate::theme::{Theme, theme};
use super::{Mode, Screen};
const SPLIT_STEP: u16 = 50;
#[derive(Debug, Clone)]
pub(crate) struct Settings {
pub(super) now: ReviewConfig,
config: ReviewConfig,
remembered: ReviewConfig,
path: Option<PathBuf>,
notice: Option<String>,
failed: bool,
auto_split: Option<bool>,
shown_whitespace: Whitespace,
hide_as: Whitespace,
}
impl Default for Settings {
fn default() -> Self {
Self::load(ReviewConfig::DEFAULT, None)
}
}
impl Settings {
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,
}
}
pub(super) fn remembers(&self) -> bool {
self.path.is_some()
}
pub(super) fn take_notice(&mut self) -> Option<String> {
self.notice.take()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Row {
Layout,
SplitRatio,
Wrap,
LineNumbers,
Whitespace,
TabWidth,
InlineChanges,
Theme,
Background,
Reset,
}
pub(super) const ROWS: [Row; 10] = [
Row::Layout,
Row::SplitRatio,
Row::Wrap,
Row::LineNumbers,
Row::Whitespace,
Row::TabWidth,
Row::InlineChanges,
Row::Theme,
Row::Background,
Row::Reset,
];
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::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",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Step {
Back,
On,
Enter,
}
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)
}
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 })
}
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" }
}
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)",
}
}
fn light() -> bool {
theme() == Theme::Light
}
impl Screen {
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(())
}
fn settings_changed(&mut self) {
if let Err(error) = self.apply_settings() {
self.status = Some(format!("{error:#}"));
}
}
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"));
}
}
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(),
});
}
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::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(),
}
}
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::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)),
});
}
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)
}
pub(super) fn toggle_layout(&mut self) {
let split = !self.diff.split();
self.pick_layout(split);
}
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();
}
pub(super) fn toggle_wrap(&mut self) {
self.settings.now.wrap = !self.settings.now.wrap;
self.settings_changed();
}
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();
}
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();
}
}
pub(super) fn even_split(&mut self) {
self.settings.now.split_ratio = SplitShare::EVEN;
self.settings_changed();
}
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);
}
}