Lines
63.24 %
Functions
43.33 %
Branches
100 %
//! The Options dialog: the theme, the keymap and the colour depth, picked from lists, and the
//! text of their overrides.
use forms::{Field as Descriptor, FormValues, Kind};
use tvui::{ColorDepth, Theme};
use super::{Form, FormKind};
use crate::event::Intent;
use crate::keymap::Keys;
use crate::modal::Spec;
use crate::widgets::{SelectOption, Widget};
/// The buttons of the Options dialog: OK saves and closes, Apply saves, Cancel puts back.
pub const OPTIONS_BUTTONS: [Spec; 3] = [
("~O~K", Intent::Submit),
("~C~ancel", Intent::CloseTopmost),
("A~p~ply", Intent::ApplyOptions),
];
const fn choice(key: &'static str, label: &'static str) -> Descriptor {
Descriptor {
key,
label,
kind: Kind::Choice {
options: &[],
default: None,
},
required: true,
}
const fn text_area(key: &'static str, label: &'static str) -> Descriptor {
kind: Kind::Text {
multiline: true,
suggest: None,
required: false,
pub(super) const OPTIONS: forms::Form = forms::Form {
name: "options",
title: "Options",
fields: &[
choice("theme", "Theme"),
choice("keymap", "Keymap"),
choice("colors", "Colours"),
text_area("theme-overrides", "Theme overrides"),
text_area("keymap-overrides", "Keymap overrides"),
],
action: forms::Action::Mutation,
};
fn colour_names() -> impl Iterator<Item = &'static str> {
let depths = [ColorDepth::Rgb, ColorDepth::Indexed256, ColorDepth::Ansi16];
std::iter::once("auto").chain(depths.map(ColorDepth::name))
fn options_of<'a>(names: impl Iterator<Item = &'a str>) -> Vec<SelectOption> {
names
.map(|name| SelectOption {
id: name.to_string(),
label: name.to_string(),
})
.collect()
impl Form {
/// The Options dialog showing `shown`, the settings by their form keys.
#[must_use]
pub fn options(shown: &FormValues) -> Self {
let mut form = Self::from_descriptor(&OPTIONS, FormKind::Options);
let lists = [
("theme", options_of(Theme::preset_names())),
("keymap", options_of(Keys::preset_names())),
("colors", options_of(colour_names())),
for (key, options) in lists {
if let Some(select) = form.select_mut(key) {
select.set_options(options);
form.load(shown);
form
/// Makes the select `key` offer only `id`, marked as set by `flag`.
pub fn lock_choice(&mut self, key: &str, id: &str, flag: &str) {
if let Some(select) = self.select_mut(key) {
select.set_options(vec![SelectOption {
id: id.to_string(),
label: format!("{id} (set by {flag})"),
}]);
fn select_mut(&mut self, key: &str) -> Option<&mut crate::widgets::SelectWidget> {
self.fields
.iter_mut()
.find(|field| field.key == Some(key))
.and_then(|field| match &mut field.widget {
Widget::Select(select) => Some(select),
_ => None,
/// The buttons the dialog has: the Options dialog's own three, else OK and Close.
pub fn buttons(&self) -> &'static [Spec] {
match self.kind {
FormKind::Options => &OPTIONS_BUTTONS,
_ => &super::BUTTONS,