Lines
96.55 %
Functions
65.38 %
Branches
100 %
//! The five UI settings as the Options dialog edits them: a text each, defaults filled in.
use std::array;
use std::ops::Range;
use forms::FormValues;
use tvui::DEFAULT_PRESET;
use super::Texts;
use crate::keymap::DEFAULT_KEYMAP;
/// The form key and the config key of each setting, in the order of a config read.
pub(super) const FIELDS: [(&str, &str); 5] = [
("theme", "tui-theme"),
("theme-overrides", "tui-theme-overrides"),
("colors", "tui-colors"),
("keymap", "tui-keymap"),
("keymap-overrides", "tui-keymap-overrides"),
];
/// The settings that make the theme, and those that make the keymap.
pub(super) const THEME: Range<usize> = 0..3;
pub(super) const KEYMAP: Range<usize> = 3..5;
const DEFAULTS: [&str; 5] = [DEFAULT_PRESET, "", "auto", DEFAULT_KEYMAP, ""];
#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::app) struct Settings([String; 5]);
impl Default for Settings {
fn default() -> Self {
Self(DEFAULTS.map(str::to_string))
}
impl Settings {
/// What a config read replied with; an unset or blank setting is its default.
pub(super) fn from_texts(texts: &Texts) -> Self {
Self(array::from_fn(|at| {
match texts[at].as_deref().map(str::trim) {
Some(text) if !text.is_empty() => text.to_string(),
_ => DEFAULTS[at].to_string(),
}))
/// The dialog's values; a setting the form does not hold stays as in `base`.
pub(super) fn from_values(values: &FormValues, base: &Self) -> Self {
values
.text(FIELDS[at].0)
.map_or_else(|| base.0[at].clone(), |text| text.trim().to_string())
pub(super) fn values(&self) -> FormValues {
let mut values = FormValues::default();
for ((key, _), text) in FIELDS.iter().zip(&self.0) {
values.set(key, text);
fn at(key: &str) -> Option<usize> {
FIELDS.iter().position(|(form, _)| *form == key)
pub(super) fn get(&self, key: &str) -> &str {
Self::at(key).map_or("", |at| &self.0[at])
pub(super) fn set(&mut self, key: &str, text: &str) {
if let Some(at) = Self::at(key) {
self.0[at] = text.to_string();
/// The settings of `range` as a config read gives them: a blank one unset.
pub(super) fn texts<const N: usize>(&self, range: Range<usize>) -> [Option<String>; N] {
let start = range.start;
array::from_fn(|at| Some(self.0[start + at].clone()).filter(|text| !text.is_empty()))
pub(super) fn same_in(&self, other: &Self, range: Range<usize>) -> bool {
self.0[range.clone()] == other.0[range]
/// The config key and the new text of each setting that differs from `saved`.
pub(super) fn changes(&self, saved: &Self) -> Vec<(String, String)> {
FIELDS
.iter()
.zip(&self.0)
.zip(&saved.0)
.filter(|((_, text), was)| text != was)
.map(|(((_, key), text), _)| ((*key).to_string(), text.clone()))
.collect()
/// Takes the texts a save wrote.
pub(in crate::app) fn apply(&mut self, changes: &[(String, String)]) {
for (key, text) in changes {
if let Some(at) = FIELDS.iter().position(|(_, config)| config == key) {
self.0[at].clone_from(text);
pub(super) fn copy_in(&mut self, other: &Self, range: Range<usize>) {
self.0[range.clone()].clone_from_slice(&other.0[range]);