Lines
96.21 %
Functions
100 %
Branches
//! The per-user keymap keys `tui-keymap` and `tui-keymap-overrides`.
use super::ui_config::{attached, item, reads};
use crate::app::{App, CliConfig};
use crate::form::Form;
use crate::keymap::VimState;
use crate::modal::Modal;
use crate::test_support::{key, press};
use crate::view::ViewId;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use sqlx::types::Uuid;
use tvui::ColorDepth;
/// Answers the one read with the theme keys unset and the two keymap values.
fn answer_keymap(app: &mut App, keymap: Option<&str>, overrides: Option<&str>) {
let [id] = reads(app)[..] else {
panic!("one config read");
};
let items = [None, None, None, keymap, overrides].map(item);
app.route_reply(id, &format!(r#"(:id {id} :value ({}))"#, items.join(" ")));
}
#[tokio::test]
async fn no_keys_keep_the_default_preset() {
let mut app = attached();
answer_keymap(&mut app, None, None);
assert_eq!(app.keys.indicator(), "cua");
assert!(app.status.is_empty(), "{}", app.status);
async fn the_config_preset_switches_the_keymap() {
answer_keymap(&mut app, Some("vim"), None);
assert_eq!(app.keys.indicator(), "vim:insert");
assert_eq!(app.keys.vim(), VimState::Insert);
async fn the_command_line_preset_beats_the_config() {
let cli = CliConfig {
keymap: Some("emacs".to_string()),
..CliConfig::default()
let Ok(mut app) = App::new(Uuid::nil(), cli, ColorDepth::Rgb) else {
panic!("the command-line keymap builds");
app.attach_console(crate::tabs::nms_eval::ConsoleEval::recording().0);
assert_eq!(app.keys.indicator(), "emacs");
async fn overrides_rebind_a_key_over_the_preset() {
answer_keymap(
&mut app,
None,
Some("global: F2 = open-help; global: M-x = none"),
);
press(&mut app, key(KeyCode::F(2)));
assert!(crate::test_support::help_open(&app));
answer_keymap(&mut app, None, Some("global: M-x = none"));
press(
KeyEvent::new(KeyCode::Char('x'), KeyModifiers::ALT),
assert!(!app.should_quit);
async fn a_bad_preset_keeps_the_keymap_and_shows_the_error() {
answer_keymap(&mut app, Some("nano"), None);
assert_eq!(
app.status,
"keymap: unknown keymap `nano`; known: cua, emacs, vim"
async fn bad_overrides_are_skipped_and_the_preset_stays() {
Some("emacs"),
Some("global: F2 = open-help\nlsit: q = quit"),
"keymap: overrides: line 2: unknown scope `lsit`"
assert!(app.overlays.is_empty());
async fn overrides_that_would_stop_typing_are_skipped() {
for text in ["global: q = quit", "cmdline: x = close"] {
answer_keymap(&mut app, Some("vim"), Some(text));
assert!(
app.status.starts_with("keymap: overrides: line 1:"),
"{}",
app.status
async fn saving_a_config_key_rereads_the_ui_config() {
let mut values = forms::FormValues::default();
values.set("key", "tui-keymap");
values.set("value", "vim");
app.open_dialog_for(ViewId::Config, Modal::Form(Form::config_set()));
let Some(id) = app.submit_config_set(&values) else {
panic!("the save is dispatched");
app.route_reply(id, &format!(r#"(:id {id} :value "ok")"#));
assert_eq!(reads(&app).len(), 1);
async fn a_keymap_error_leaves_the_theme_applied() {
let [id] = reads(&app)[..] else {
let items = [Some("turbo"), None, None, Some("nano"), None].map(item);
assert!(app.status.starts_with("keymap: "), "{}", app.status);
assert_ne!(
app.theme.style(tvui::Role::Panel),
crate::test_support::dn_theme().style(tvui::Role::Panel)
async fn a_swap_clears_the_half_typed_binding() {
let overrides = Some("global-view: z z = quit");
answer_keymap(&mut app, None, overrides);
press(&mut app, key(KeyCode::Char('z')));
app.read_ui_config();
press(&mut app, key(KeyCode::Char('y')));
assert!(app.should_quit);
async fn a_stale_reply_is_ignored() {
let [old, new] = reads(&app)[..] else {
panic!("two config reads");
let items = [None, None, None, Some("vim"), None].map(item);
app.route_reply(old, &format!(r#"(:id {old} :value ({}))"#, items.join(" ")));
let items = [None, None, None, Some("emacs"), None].map(item);
app.route_reply(new, &format!(r#"(:id {new} :value ({}))"#, items.join(" ")));