1
//! The five UI settings as the Options dialog edits them: a text each, defaults filled in.
2

            
3
use std::array;
4
use std::ops::Range;
5

            
6
use forms::FormValues;
7
use tvui::DEFAULT_PRESET;
8

            
9
use super::Texts;
10
use crate::keymap::DEFAULT_KEYMAP;
11

            
12
/// The form key and the config key of each setting, in the order of a config read.
13
pub(super) const FIELDS: [(&str, &str); 5] = [
14
    ("theme", "tui-theme"),
15
    ("theme-overrides", "tui-theme-overrides"),
16
    ("colors", "tui-colors"),
17
    ("keymap", "tui-keymap"),
18
    ("keymap-overrides", "tui-keymap-overrides"),
19
];
20

            
21
/// The settings that make the theme, and those that make the keymap.
22
pub(super) const THEME: Range<usize> = 0..3;
23
pub(super) const KEYMAP: Range<usize> = 3..5;
24

            
25
const DEFAULTS: [&str; 5] = [DEFAULT_PRESET, "", "auto", DEFAULT_KEYMAP, ""];
26

            
27
#[derive(Debug, Clone, PartialEq, Eq)]
28
pub(in crate::app) struct Settings([String; 5]);
29

            
30
impl Default for Settings {
31
4204
    fn default() -> Self {
32
4204
        Self(DEFAULTS.map(str::to_string))
33
4204
    }
34
}
35

            
36
impl Settings {
37
    /// What a config read replied with; an unset or blank setting is its default.
38
110
    pub(super) fn from_texts(texts: &Texts) -> Self {
39
550
        Self(array::from_fn(|at| {
40
550
            match texts[at].as_deref().map(str::trim) {
41
45
                Some(text) if !text.is_empty() => text.to_string(),
42
505
                _ => DEFAULTS[at].to_string(),
43
            }
44
550
        }))
45
110
    }
46

            
47
    /// The dialog's values; a setting the form does not hold stays as in `base`.
48
485
    pub(super) fn from_values(values: &FormValues, base: &Self) -> Self {
49
2425
        Self(array::from_fn(|at| {
50
2425
            values
51
2425
                .text(FIELDS[at].0)
52
2425
                .map_or_else(|| base.0[at].clone(), |text| text.trim().to_string())
53
2425
        }))
54
485
    }
55

            
56
60
    pub(super) fn values(&self) -> FormValues {
57
60
        let mut values = FormValues::default();
58
60
        for ((key, _), text) in FIELDS.iter().zip(&self.0) {
59
300
            values.set(key, text);
60
        }
61
60
        values
62
60
    }
63

            
64
41
    fn at(key: &str) -> Option<usize> {
65
84
        FIELDS.iter().position(|(form, _)| *form == key)
66
41
    }
67

            
68
18
    pub(super) fn get(&self, key: &str) -> &str {
69
18
        Self::at(key).map_or("", |at| &self.0[at])
70
18
    }
71

            
72
23
    pub(super) fn set(&mut self, key: &str, text: &str) {
73
23
        if let Some(at) = Self::at(key) {
74
23
            self.0[at] = text.to_string();
75
        }
76
23
    }
77

            
78
    /// The settings of `range` as a config read gives them: a blank one unset.
79
599
    pub(super) fn texts<const N: usize>(&self, range: Range<usize>) -> [Option<String>; N] {
80
599
        let start = range.start;
81
1447
        array::from_fn(|at| Some(self.0[start + at].clone()).filter(|text| !text.is_empty()))
82
599
    }
83

            
84
970
    pub(super) fn same_in(&self, other: &Self, range: Range<usize>) -> bool {
85
970
        self.0[range.clone()] == other.0[range]
86
970
    }
87

            
88
    /// The config key and the new text of each setting that differs from `saved`.
89
15
    pub(super) fn changes(&self, saved: &Self) -> Vec<(String, String)> {
90
15
        FIELDS
91
15
            .iter()
92
15
            .zip(&self.0)
93
15
            .zip(&saved.0)
94
75
            .filter(|((_, text), was)| text != was)
95
16
            .map(|(((_, key), text), _)| ((*key).to_string(), text.clone()))
96
            .collect()
97
15
    }
98

            
99
    /// Takes the texts a save wrote.
100
7
    pub(in crate::app) fn apply(&mut self, changes: &[(String, String)]) {
101
7
        for (key, text) in changes {
102
24
            if let Some(at) = FIELDS.iter().position(|(_, config)| config == key) {
103
8
                self.0[at].clone_from(text);
104
            }
105
        }
106
7
    }
107

            
108
20
    pub(super) fn copy_in(&mut self, other: &Self, range: Range<usize>) {
109
20
        self.0[range.clone()].clone_from_slice(&other.0[range]);
110
20
    }
111
}