Lines
94.55 %
Functions
52.17 %
Branches
100 %
//! The Options dialog: previews the settings as they change, puts them back on Cancel and
//! saves the changed ones through `config-set`.
use cli_core::render::{WireValue, parse_wire};
use cli_core::transaction::submit_call;
use forms::{CONFIG_SET, FormValues};
use tvui::Theme;
use super::UiConfig;
use super::settings::{FIELDS, KEYMAP, Settings, THEME};
use crate::app::App;
use crate::form::{Form, FormKind};
use crate::modal::Modal;
use crate::route::RouteCtx;
use crate::tabs::nms_eval::UNAVAILABLE;
/// The `config-set` calls of `changes`, in one form.
fn save_call(changes: &[(String, String)]) -> String {
let calls: Vec<String> = changes
.iter()
.map(|(key, text)| {
let mut values = FormValues::default();
values.set("key", key);
values.set("value", text);
submit_call(&CONFIG_SET, &values)
})
.collect();
format!("(progn {})", calls.join(" "))
}
/// The built value, or the problems that make the dialog refuse it.
fn strict<T>((built, problems): (T, Option<String>)) -> Result<T, String> {
problems.map_or(Ok(built), Err)
/// What the dialog's values make: the theme that changed and builds, and what is wrong with it or
/// the keymap, which only takes over once saved so the dialog keeps its own keys.
#[derive(Default)]
struct Preview {
theme: Option<Theme>,
problems: Vec<String>,
impl UiConfig {
/// The settings a flag fixes: the form key, the value and the flag.
fn forced(&self) -> Vec<(&'static str, String, &'static str)> {
let flags = [
("theme", self.cli.preset.clone(), "--theme"),
(
"colors",
self.cli.depth.map(|depth| depth.name().to_string()),
"--colors",
),
("keymap", self.cli.keymap.clone(), "--keymap"),
];
flags
.into_iter()
.filter_map(|(key, value, flag)| Some((key, value?, flag)))
.collect()
/// The settings the dialog opens with: the saved ones, but a flag's value where one is given.
fn shown(&self) -> Settings {
let mut shown = self.saved.clone();
for (key, value, _) in self.forced() {
shown.set(key, &value);
shown
/// The settings `form` holds; a flag's settings are not the dialog's to change.
fn read(&self, form: &Form) -> Settings {
let mut next = Settings::from_values(&form.values(), &self.saved);
for (key, _, _) in self.forced() {
next.set(key, self.saved.get(key));
next
/// What saving the dialog's values changes, or the problems that stop it.
fn plan(&self, form: &Form) -> Result<Vec<(String, String)>, Vec<String>> {
let next = self.read(form);
let problems = self.preview(&next).problems;
if problems.is_empty() {
Ok(next.changes(&self.saved))
} else {
Err(problems)
fn preview(&self, next: &Settings) -> Preview {
let mut preview = Preview::default();
if !next.same_in(&self.live, THEME) {
match self.build(next.texts(THEME)).and_then(strict) {
Ok(theme) => preview.theme = Some(theme),
Err(err) => preview.problems.push(format!("theme: {err}")),
if !next.same_in(&self.live, KEYMAP) {
let [preset, overrides] = next.texts(KEYMAP);
if let Err(err) = self.keys(preset, overrides).and_then(strict) {
preview.problems.push(format!("keymap: {err}"));
preview
impl App {
/// Opens the dialog and asks the server what it holds now, which another session may have
/// changed since the attach.
pub(crate) fn open_options(&mut self) {
if !self.dialog_free("options") {
return;
self.read_ui_config_into_dialog();
let mut form = Form::options(&self.ui_config.shown().values());
for (key, value, flag) in self.ui_config.forced() {
form.lock_choice(key, &value, flag);
self.overlays.push(Modal::Form(form));
fn options_form(&self) -> Option<&Form> {
match self.overlays.top() {
Some(Modal::Form(form)) if form.kind == FormKind::Options => Some(form),
_ => None,
/// Shows the dialog's values in the whole UI as they change; one that does not build keeps
/// the last good one and is said in the dialog.
pub(crate) fn preview_options(&mut self) {
let Some(form) = self.options_form() else {
};
let next = self.ui_config.read(form);
let preview = self.ui_config.preview(&next);
if let Some(theme) = preview.theme {
self.set_theme(theme);
self.ui_config.live.copy_in(&next, THEME);
if let Some(Modal::Form(form)) = self.overlays.top_mut() {
form.errors = preview.problems;
/// Esc and Cancel: the UI goes back to what is saved and the dialog closes.
pub(crate) fn cancel_options(&mut self) {
self.show_saved();
self.overlays.pop();
/// The UI as saved.
fn show_saved(&mut self) {
let saved = self.ui_config.saved.clone();
self.show_settings(&saved);
/// Sends `changes` as one request, the reply of which settles the save.
fn send_save(&mut self, changes: Vec<(String, String)>) -> Option<i64> {
let call = save_call(&changes);
self.dispatch_app_eval(RouteCtx::OptionsSave { changes }, call)
/// OK: saves and closes; a problem or a refused save keeps the dialog.
pub(crate) fn submit_options(&mut self, mut form: Form) {
let sent = match self.ui_config.plan(&form) {
Err(problems) => Err(problems),
Ok(changes) if changes.is_empty() => return,
Ok(changes) => self
.send_save(changes)
.ok_or_else(|| vec![UNAVAILABLE.to_string()]),
match sent {
Ok(id) => self.lock_pending_form(id, form),
Err(problems) => {
form.errors = problems;
/// Apply: saves and keeps the dialog.
pub(crate) fn apply_options(&mut self) {
let errors = match self.ui_config.plan(form) {
Err(problems) => problems,
Ok(changes) if changes.is_empty() => Vec::new(),
Ok(changes) => match self.send_save(changes) {
Some(_) => Vec::new(),
None => vec![UNAVAILABLE.to_string()],
},
form.errors = errors;
/// The reply to a save: what was saved is now the saved state; a refusal puts the UI and the
/// dialog back to it.
pub(in crate::app) fn deliver_options_saved(
&mut self,
changes: &[(String, String)],
wire: &str,
) {
match parse_wire(wire) {
Ok(WireValue::Value(_)) => {
self.ui_config.saved.apply(changes);
self.show_saved_keys();
if self.options_form().is_none() && self.ui_config.live != self.ui_config.saved {
Err(err) => {
let message = format!("save failed: {err}");
if let Some(Modal::Form(form)) = self.overlays.top_mut()
&& form.kind == FormKind::Options
{
form.errors = vec![message.clone()];
self.set_status(message);
self.read_ui_config();
/// A refused OK: the UI shows the saved settings and the server is asked what it holds, as a
/// save of several keys may have written some. The form keeps what was typed.
pub(in crate::app) fn rollback_options(&mut self, form: &Form) {
if form.kind == FormKind::Options {
/// Merges the fresh saved settings into the open Options dialog: a field the user edited
/// keeps their value, any other takes the fresh one, and the fresh ones are the baseline.
pub(super) fn merge_into_options_form(&mut self) {
let fresh = self.ui_config.shown().values();
let current = form.values();
let opened = form.baseline.clone().unwrap_or_default();
let mut merged = fresh.clone();
for (key, _) in FIELDS {
if current.text(key) != opened.text(key) {
merged.set(key, current.text(key).unwrap_or_default());
form.load(&merged);
form.baseline = Some(fresh);
self.preview_options();