From 7cbb9d2baf30137bc5af74d29539a527f2cee866 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Fri, 2 Apr 2021 19:56:18 +0530 Subject: [PATCH] Saner key bindings --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/app.rs | 2 +- src/config.rs | 239 ++++++++++++++++++++++++++++++-------------- src/ui.rs | 2 +- tests/test_input.rs | 16 --- 6 files changed, 169 insertions(+), 94 deletions(-) delete mode 100644 tests/test_input.rs diff --git a/Cargo.lock b/Cargo.lock index aadd86b..901f34f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1123,7 +1123,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "xplr" -version = "0.2.12" +version = "0.2.13" dependencies = [ "anyhow", "criterion", diff --git a/Cargo.toml b/Cargo.toml index 9135a3a..3adcb8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xplr" -version = "0.2.12" # Update app.rs +version = "0.2.13" # Update app.rs authors = ["Arijit Basu "] edition = "2018" description = "An experimental, minimal, configurable TUI file explorer, stealing ideas from nnn and fzf." diff --git a/src/app.rs b/src/app.rs index adb5d79..024af0d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,7 +13,7 @@ use std::fs; use std::io; use std::path::PathBuf; -pub const VERSION: &str = "v0.2.12"; // Update Cargo.toml +pub const VERSION: &str = "v0.2.13"; // Update Cargo.toml pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW"; diff --git a/src/config.rs b/src/config.rs index 805b27a..7b20410 100644 --- a/src/config.rs +++ b/src/config.rs @@ -245,50 +245,72 @@ impl Default for KeyBindings { let on_key: BTreeMap = serde_yaml::from_str( r###" up: - help: up + help: up [k] + messages: + - FocusPrevious + + k: messages: - FocusPrevious down: - help: down + help: down [j] + messages: + - FocusNext + + j: messages: - FocusNext right: - help: enter + help: enter [l] + messages: + - Enter + + l: messages: - Enter left: - help: back + help: back [h] + messages: + - Back + + h: messages: - Back g: help: go to messages: - - SwitchMode: goto + - SwitchMode: go to G: - help: bottom + help: go to bottom messages: - FocusLast - s: - help: shell + ctrl-f: + help: search [/] messages: - Call: command: bash - args: [] - - Explore + args: + - -c + - | + PTH=$(echo -e "${XPLR_DIRECTORY_NODES:?}" | fzf) + if [ -d "$PTH" ]; then + echo "ChangeDirectory: ${PTH:?}" >> "${XPLR_PIPE_MSG_IN:?}" + elif [ -f "$PTH" ]; then + echo "FocusPath: ${PTH:?}" >> "${XPLR_PIPE_MSG_IN:?}" + fi /: - help: search messages: - Call: command: bash args: - - "-c" + - -c - | PTH=$(echo -e "${XPLR_DIRECTORY_NODES:?}" | fzf) if [ -d "$PTH" ]; then @@ -297,89 +319,46 @@ impl Default for KeyBindings { echo "FocusPath: ${PTH:?}" >> "${XPLR_PIPE_MSG_IN:?}" fi - space: - help: toggle selection - messages: - - ToggleSelection - - FocusNext - - n: - help: create new - messages: - - SwitchMode: create - d: help: delete messages: - SwitchMode: delete - c: - help: copy here + ":": + help: action messages: - - Call: - command: bash - args: - - -c - - | - while IFS= read -r line; do - cp -v "${line:?}" ./ - done <<< "${XPLR_SELECTION:?}" - read -p "[enter to continue]" - - ClearSelection - - Explore - - m: - help: move here - messages: - - Call: - command: bash - args: - - -c - - | - while IFS= read -r line; do - mv -v "${line:?}" ./ - done <<< "${XPLR_SELECTION:?}" - read -p "[enter to continue]" - - Explore + - SwitchMode: action - enter: - help: quit with result + space: + help: toggle selection [v] messages: - - PrintResultAndQuit + - ToggleSelection + - FocusNext - o: - help: open + v: messages: - - Call: - command: bash - args: - - -c - - | - xdg-open "${XPLR_FOCUS_PATH:?}" &> /dev/null + - ToggleSelection + - FocusNext - ctrl-l: - help: clear + enter: + help: quit with result messages: - - ClearScreen - - Refresh + - PrintResultAndQuit "#": - help: quit with debug messages: - PrintAppStateAndQuit - esc: - help: cancel & quit + ctrl-c: + help: cancel & quit [q|esc] messages: - Terminate q: - help: cancel & quit messages: - Terminate - ctrl-c: - help: cancel & quit + esc: messages: - Terminate "###, @@ -461,6 +440,104 @@ impl Default for Config { - FocusFirst - SwitchMode: default + x: + help: open in gui + messages: + - Call: + command: bash + args: + - -c + - | + xdg-open "${XPLR_FOCUS_PATH:?}" &> /dev/null + - SwitchMode: default + + ctrl-c: + help: cancel & quit + messages: + - Terminate + + default: + messages: + - SwitchMode: default + "###, + ) + .unwrap(); + + let action_mode: Mode = serde_yaml::from_str( + r###" + name: action to + key_bindings: + on_key: + "!": + help: shell + messages: + - Call: + command: bash + - Explore + - SwitchMode: default + + n: + help: create new + messages: + - SwitchMode: create + + s: + help: selection operations + messages: + - SwitchMode: selection ops + + ctrl-c: + help: cancel & quit [q] + messages: + - Terminate + + q: + messages: + - Terminate + + default: + messages: + - SwitchMode: default + "###, + ) + .unwrap(); + + let selection_ops_mode: Mode = serde_yaml::from_str( + r###" + name: selection ops + key_bindings: + on_key: + c: + help: copy here + messages: + - Call: + command: bash + args: + - -c + - | + while IFS= read -r line; do + cp -v "${line:?}" ./ + done <<< "${XPLR_SELECTION:?}" + read -p "[enter to continue]" + - ClearSelection + - Explore + - SwitchMode: default + + m: + help: move here + messages: + - Call: + command: bash + args: + - -c + - | + while IFS= read -r line; do + mv -v "${line:?}" ./ + done <<< "${XPLR_SELECTION:?}" + read -p "[enter to continue]" + - Explore + - SwitchMode: default + ctrl-c: help: cancel & quit messages: @@ -479,14 +556,26 @@ impl Default for Config { key_bindings: on_key: up: - help: go up + help: to up [k] + messages: + - FocusPreviousByRelativeIndexFromInput + - ResetInputBuffer + - SwitchMode: default + + k: messages: - FocusPreviousByRelativeIndexFromInput - ResetInputBuffer - SwitchMode: default down: - help: go down + help: to down [j] + messages: + - FocusNextByRelativeIndexFromInput + - ResetInputBuffer + - SwitchMode: default + + j: messages: - FocusNextByRelativeIndexFromInput - ResetInputBuffer @@ -617,10 +706,12 @@ impl Default for Config { let mut modes: HashMap = Default::default(); modes.insert("default".into(), Mode::default()); - modes.insert("goto".into(), goto_mode); + modes.insert("go to".into(), goto_mode); modes.insert("number".into(), number_mode); modes.insert("create".into(), create_mode); modes.insert("delete".into(), delete_mode); + modes.insert("action".into(), action_mode); + modes.insert("selection ops".into(), selection_ops_mode); Self { version: VERSION.into(), diff --git a/src/ui.rs b/src/ui.rs index 13a1b98..6f74da0 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -353,7 +353,7 @@ fn draw_help_menu(f: &mut Frame, rect: Rect, app: &app::App, _: & .borders(Borders::ALL) .title(format!(" Help [{}] ", &mode.name)), ) - .widths(&[TUIConstraint::Percentage(40), TUIConstraint::Percentage(60)]); + .widths(&[TUIConstraint::Percentage(30), TUIConstraint::Percentage(70)]); f.render_widget(help_menu, rect); } diff --git a/tests/test_input.rs b/tests/test_input.rs deleted file mode 100644 index 98d07d5..0000000 --- a/tests/test_input.rs +++ /dev/null @@ -1,16 +0,0 @@ -use xplr::*; - -#[test] -fn test_key_down() { - let mut app = app::App::create().expect("failed to create app"); - - assert_eq!(app.focus(), Some(0)); - - let actions = app.actions_from_key(&input::Key::Down).unwrap(); - - for action in actions { - app = app.handle(&action).unwrap() - } - - assert_eq!(app.directory_buffer.focus, Some(1)); -}