Lines
97.75 %
Functions
100 %
Branches
//! The Options dialog: it shows the settings, previews them live and puts them back on Cancel.
use super::report_chart::{echoed, pending};
use super::ui_config::item;
use crate::app::{App, CliConfig};
use crate::event::{Intent, apply};
use crate::form::{Form, FormKind};
use crate::modal::Modal;
use crate::route::RouteCtx;
use crate::tabs::nms_eval::ConsoleEval;
use crate::tabs::nms_eval::double::Sent;
use crate::test_support::{draw_frame, key, press};
use ratatui::crossterm::event::KeyCode;
use sqlx::types::Uuid;
use tvui::{ColorDepth, Role, Theme};
pub(super) fn answer_config(app: &mut App, texts: [Option<&str>; 5]) {
let [id] = super::ui_config::reads(app)[..] else {
panic!("one config read");
};
let items = texts.map(item);
app.route_reply(id, &format!(r#"(:id {id} :value ({}))"#, items.join(" ")));
}
pub(super) fn attached_blank() -> (App, Sent) {
let Ok(mut app) = App::new(Uuid::nil(), CliConfig::default(), ColorDepth::Rgb) else {
panic!("the app builds");
let (eval, sent) = ConsoleEval::recording();
app.attach_console(eval);
(app, sent)
pub(super) fn attached_with(cli: CliConfig, texts: [Option<&str>; 5]) -> (App, Sent) {
let Ok(mut app) = App::new(Uuid::nil(), cli, ColorDepth::Rgb) else {
answer_config(&mut app, texts);
pub(super) fn opened(texts: [Option<&str>; 5]) -> (App, Sent) {
let (mut app, sent) = attached_with(CliConfig::default(), texts);
apply(&mut app, Intent::OpenOptions);
pub(super) fn dialog(app: &App) -> &Form {
match app.overlays.top() {
Some(Modal::Form(form)) if form.kind == FormKind::Options => form,
other => panic!("the Options dialog is not on top: {other:?}"),
pub(super) fn label<'a>(app: &'a App, field: &str) -> &'a str {
dialog(app).select_label(field).unwrap_or_default()
/// Opens the focused select, moves `steps` options with `code` and confirms.
pub(super) fn choose(app: &mut App, code: KeyCode, steps: usize) {
press(app, key(KeyCode::Enter));
for _ in 0..steps {
press(app, key(code));
pub(super) fn next_field(app: &mut App) {
press(app, key(KeyCode::Tab));
pub(super) fn saves(app: &App) -> Vec<i64> {
pending(app, |ctx| matches!(ctx, RouteCtx::OptionsSave { .. }))
const UNSET: [Option<&str>; 5] = [None; 5];
fn desktop_bg(app: &App) -> ratatui::style::Color {
draw_frame(app, 100, 30).0[(1, 2)].bg
#[tokio::test]
async fn the_menu_opens_the_dialog_with_the_current_values() {
let (mut app, _) = attached_with(
CliConfig::default(),
[Some("turbo"), None, Some("256"), Some("emacs"), None],
);
app.open_menu_of(crate::app::MenuId::Options);
assert_eq!(app.menu_choice(), Some(Intent::OpenOptions));
apply(&mut app, Intent::MenuSelect);
assert_eq!(label(&app, "theme"), "turbo");
assert_eq!(label(&app, "keymap"), "emacs");
assert_eq!(label(&app, "colors"), "256");
let labels: Vec<_> = dialog(&app).buttons().iter().map(|b| b.0).collect();
assert_eq!(labels, ["~O~K", "~C~ancel", "A~p~ply"]);
async fn the_dialog_shows_what_the_server_holds_now() {
let (mut app, _) = attached_with(CliConfig::default(), UNSET);
assert_eq!(label(&app, "theme"), "dn");
answer_config(&mut app, [Some("turbo"), None, None, None, None]);
async fn an_edit_made_before_the_open_read_replies_survives_it() {
choose(&mut app, KeyCode::Down, 1);
let turbo = Theme::preset("turbo").expect("preset");
assert_eq!(app.theme.bg(Role::Desktop), turbo.bg(Role::Desktop));
answer_config(&mut app, UNSET);
async fn the_open_read_merges_what_another_session_saved_with_the_edits() {
let (mut app, sent) = attached_with(CliConfig::default(), UNSET);
answer_config(&mut app, [None, None, None, Some("emacs"), None]);
assert_eq!(app.keys.indicator(), "emacs");
for _ in 0..5 {
press(&mut app, key(KeyCode::Tab));
press(&mut app, key(KeyCode::Enter));
echoed(&mut app, &sent).await;
let save = sent.frames().pop().unwrap_or_default();
assert!(
save.contains("tui-theme") && save.contains("turbo"),
"{save}"
assert!(!save.contains("tui-keymap"), "{save}");
async fn a_command_line_choice_is_shown_as_set_by_its_flag() {
let cli = CliConfig {
preset: Some("turbo".to_string()),
depth: Some(ColorDepth::Ansi16),
keymap: Some("vim".to_string()),
let (mut app, _) = attached_with(cli, UNSET);
assert_eq!(label(&app, "theme"), "turbo (set by --theme)");
assert_eq!(label(&app, "colors"), "16 (set by --colors)");
assert_eq!(label(&app, "keymap"), "vim (set by --keymap)");
async fn picking_a_theme_redraws_the_whole_ui_at_once() {
let (mut app, _) = opened(UNSET);
let before = desktop_bg(&app);
assert_ne!(desktop_bg(&app), before);
assert_eq!(desktop_bg(&app), turbo.bg(Role::Desktop));
async fn picking_a_colour_depth_applies_it() {
next_field(&mut app);
assert_eq!(label(&app, "colors"), "rgb");
assert_eq!(app.theme.depth(), ColorDepth::Rgb);
choose(&mut app, KeyCode::Down, 2);
assert_eq!(label(&app, "colors"), "16");
assert_eq!(app.theme.depth(), ColorDepth::Ansi16);
/// Changes the theme and the keymap and returns the desktop background it had before.
fn change_two(app: &mut App) -> ratatui::style::Color {
let before = desktop_bg(app);
choose(app, KeyCode::Down, 1);
next_field(app);
assert_ne!(desktop_bg(app), before);
before
async fn assert_put_back(app: &mut App, sent: &Sent, before: ratatui::style::Color) {
assert!(app.overlays.is_empty());
assert_eq!(desktop_bg(app), before);
assert_eq!(app.keys.indicator(), "cua");
assert!(saves(app).is_empty());
let echoed = echoed(app, sent).await;
assert!(!echoed.contains("config-set"), "{echoed}");
async fn esc_puts_everything_back_and_sends_nothing() {
let (mut app, sent) = opened(UNSET);
let before = change_two(&mut app);
press(&mut app, key(KeyCode::Esc));
assert_put_back(&mut app, &sent, before).await;
async fn the_cancel_button_puts_everything_back_and_sends_nothing() {
assert_eq!(app.overlays.top().and_then(Modal::focused_button), Some(1));
async fn the_dialog_draws_over_the_screen() {
let (app, _) = opened(UNSET);
let (buffer, _) = draw_frame(&app, 100, 30);
let text: String = buffer.content().iter().map(|cell| cell.symbol()).collect();
for word in [
"Options",
"Theme",
"Keymap",
"Colours",
"Cancel",
"Apply",
"applies once saved",
] {
assert!(text.contains(word), "{word} is not drawn");