summaryrefslogtreecommitdiff
path: root/crates/nu-command/src
diff options
context:
space:
mode:
authorFernando Herrera <fernando.j.herrera@gmail.com>2022-02-04 06:47:18 +0000
committerGitHub <noreply@github.com>2022-02-04 06:47:18 +0000
commit1e86af2fb98565b5dcbccba7da96a62f5262703b (patch)
treed9aeafdc274223bbee2f524ebca0fd62a5acdee8 /crates/nu-command/src
parenta008f1aa80ad8076d0b4343b7e17b7028a2a4c45 (diff)
list keybinding options (#906)
* list keybinding optins * list keybinding options * clippy error
Diffstat (limited to 'crates/nu-command/src')
-rw-r--r--crates/nu-command/src/default_context.rs2
-rw-r--r--crates/nu-command/src/platform/mod.rs4
-rw-r--r--crates/nu-command/src/platform/reedline_commands/command.rs42
-rw-r--r--crates/nu-command/src/platform/reedline_commands/input_keys.rs (renamed from crates/nu-command/src/platform/input_keys.rs)6
-rw-r--r--crates/nu-command/src/platform/reedline_commands/list_keybindings.rs114
-rw-r--r--crates/nu-command/src/platform/reedline_commands/mod.rs7
6 files changed, 170 insertions, 5 deletions
diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs
index 1a227270a..c1f4b1b74 100644
--- a/crates/nu-command/src/default_context.rs
+++ b/crates/nu-command/src/default_context.rs
@@ -188,7 +188,9 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
Clear,
Input,
InputKeys,
+ Keybindings,
Kill,
+ ListKeybindings,
Sleep,
TermSize,
};
diff --git a/crates/nu-command/src/platform/mod.rs b/crates/nu-command/src/platform/mod.rs
index 03654af5c..5cbc35c4b 100644
--- a/crates/nu-command/src/platform/mod.rs
+++ b/crates/nu-command/src/platform/mod.rs
@@ -3,8 +3,8 @@ mod clear;
mod dir_info;
mod du;
mod input;
-mod input_keys;
mod kill;
+mod reedline_commands;
mod sleep;
mod term_size;
@@ -13,7 +13,7 @@ pub use clear::Clear;
pub use dir_info::{DirBuilder, DirInfo, FileInfo};
pub use du::Du;
pub use input::Input;
-pub use input_keys::InputKeys;
pub use kill::Kill;
+pub use reedline_commands::{InputKeys, Keybindings, ListKeybindings};
pub use sleep::Sleep;
pub use term_size::TermSize;
diff --git a/crates/nu-command/src/platform/reedline_commands/command.rs b/crates/nu-command/src/platform/reedline_commands/command.rs
new file mode 100644
index 000000000..be5af7844
--- /dev/null
+++ b/crates/nu-command/src/platform/reedline_commands/command.rs
@@ -0,0 +1,42 @@
+use nu_engine::get_full_help;
+use nu_protocol::{
+ ast::Call,
+ engine::{Command, EngineState, Stack},
+ Category, IntoPipelineData, PipelineData, Signature, Value,
+};
+
+#[derive(Clone)]
+pub struct Keybindings;
+
+impl Command for Keybindings {
+ fn name(&self) -> &str {
+ "keybindings"
+ }
+
+ fn signature(&self) -> Signature {
+ Signature::build(self.name()).category(Category::Platform)
+ }
+
+ fn usage(&self) -> &str {
+ "Keybindings related commands"
+ }
+
+ fn run(
+ &self,
+ engine_state: &EngineState,
+ stack: &mut Stack,
+ call: &Call,
+ _input: PipelineData,
+ ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
+ Ok(Value::String {
+ val: get_full_help(
+ &Keybindings.signature(),
+ &Keybindings.examples(),
+ engine_state,
+ stack,
+ ),
+ span: call.head,
+ }
+ .into_pipeline_data())
+ }
+}
diff --git a/crates/nu-command/src/platform/input_keys.rs b/crates/nu-command/src/platform/reedline_commands/input_keys.rs
index bd50d278e..99d0116bf 100644
--- a/crates/nu-command/src/platform/input_keys.rs
+++ b/crates/nu-command/src/platform/reedline_commands/input_keys.rs
@@ -12,7 +12,7 @@ pub struct InputKeys;
impl Command for InputKeys {
fn name(&self) -> &str {
- "input-keys"
+ "keybindings listen"
}
fn usage(&self) -> &str {
@@ -20,7 +20,7 @@ impl Command for InputKeys {
}
fn signature(&self) -> Signature {
- Signature::build("input-keys").category(Category::Platform)
+ Signature::build(self.name()).category(Category::Platform)
}
fn run(
@@ -47,7 +47,7 @@ impl Command for InputKeys {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Type and see key event codes",
- example: "input-keys",
+ example: "keybindings listen",
result: None,
}]
}
diff --git a/crates/nu-command/src/platform/reedline_commands/list_keybindings.rs b/crates/nu-command/src/platform/reedline_commands/list_keybindings.rs
new file mode 100644
index 000000000..d1d9760b0
--- /dev/null
+++ b/crates/nu-command/src/platform/reedline_commands/list_keybindings.rs
@@ -0,0 +1,114 @@
+use nu_protocol::{
+ ast::Call,
+ engine::{Command, EngineState, Stack},
+ Category, Example, IntoPipelineData, PipelineData, Signature, Span, Value,
+};
+use reedline::{
+ get_reedline_edit_commands, get_reedline_keybinding_modifiers, get_reedline_keycodes,
+ get_reedline_prompt_edit_modes, get_reedline_reedline_events,
+};
+
+#[derive(Clone)]
+pub struct ListKeybindings;
+
+impl Command for ListKeybindings {
+ fn name(&self) -> &str {
+ "keybindings list"
+ }
+
+ fn signature(&self) -> Signature {
+ Signature::build(self.name())
+ .switch("modifiers", "list of modifiers", Some('m'))
+ .switch("keycodes", "list of keycodes", Some('k'))
+ .switch("modes", "list of edit modes", Some('o'))
+ .switch("events", "list of reedline event", Some('e'))
+ .switch("edits", "list of edit commands", Some('d'))
+ .category(Category::Platform)
+ }
+
+ fn usage(&self) -> &str {
+ "List available options that can be used to create keybindings"
+ }
+
+ fn examples(&self) -> Vec<Example> {
+ vec![
+ Example {
+ description: "Get list of key modifiers",
+ example: "keybindings list -m",
+ result: None,
+ },
+ Example {
+ description: "Get list of reedline events and edit commands",
+ example: "keybindings list -e -d",
+ result: None,
+ },
+ Example {
+ description: "Get list with all the available options",
+ example: "keybindings list",
+ result: None,
+ },
+ ]
+ }
+
+ fn run(
+ &self,
+ _engine_state: &EngineState,
+ _stack: &mut Stack,
+ call: &Call,
+ _input: PipelineData,
+ ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
+ let records = if call.named.is_empty() {
+ let all_options = vec!["modifiers", "keycodes", "edits", "modes", "events"];
+ all_options
+ .iter()
+ .flat_map(|argument| get_records(argument, &call.head))
+ .collect()
+ } else {
+ call.named
+ .iter()
+ .flat_map(|(argument, _)| get_records(argument.item.as_str(), &call.head))
+ .collect()
+ };
+
+ Ok(Value::List {
+ vals: records,
+ span: call.head,
+ }
+ .into_pipeline_data())
+ }
+}
+
+fn get_records(entry_type: &str, span: &Span) -> Vec<Value> {
+ let values = match entry_type {
+ "modifiers" => get_reedline_keybinding_modifiers(),
+ "keycodes" => get_reedline_keycodes(),
+ "edits" => get_reedline_edit_commands(),
+ "modes" => get_reedline_prompt_edit_modes(),
+ "events" => get_reedline_reedline_events(),
+ _ => Vec::new(),
+ };
+
+ values
+ .iter()
+ .map(|edit| edit.split('\n'))
+ .flat_map(|edit| edit.map(|edit| convert_to_record(edit, entry_type, span)))
+ .collect()
+}
+
+fn convert_to_record(edit: &str, entry_type: &str, span: &Span) -> Value {
+ let entry_type = Value::String {
+ val: entry_type.to_string(),
+ span: *span,
+ };
+
+ let name = Value::String {
+ val: edit.to_string(),
+ span: *span,
+ };
+
+ Value::Record {
+ cols: vec!["type".to_string(), "name".to_string()],
+ vals: vec![entry_type, name],
+ span: *span,
+ }
+}
diff --git a/crates/nu-command/src/platform/reedline_commands/mod.rs b/crates/nu-command/src/platform/reedline_commands/mod.rs
new file mode 100644
index 000000000..0f3e1247b
--- /dev/null
+++ b/crates/nu-command/src/platform/reedline_commands/mod.rs
@@ -0,0 +1,7 @@
+mod command;
+mod input_keys;
+mod list_keybindings;
+
+pub use command::Keybindings;
+pub use input_keys::InputKeys;
+pub use list_keybindings::ListKeybindings;