list keybinding options (#906)

* list keybinding optins

* list keybinding options

* clippy error
This commit is contained in:
Fernando Herrera 2022-02-04 06:47:18 +00:00 committed by GitHub
parent a008f1aa80
commit 1e86af2fb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 392 additions and 224 deletions

436
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -74,7 +74,8 @@ unicode-segmentation = "1.8.0"
url = "2.2.1"
uuid = { version = "0.8.2", features = ["v4"] }
which = { version = "4.2.2", optional = true }
zip = { version="0.5.9", optional=true }
reedline = { git = "https://github.com/nushell/reedline", branch = "main" }
zip = { version="0.5.9", optional = true }
[target.'cfg(unix)'.dependencies]
umask = "1.0.0"
@ -101,4 +102,4 @@ shadow-rs = "0.8.1"
hamcrest2 = "0.3.0"
dirs-next = "2.0.0"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
quickcheck_macros = "1.0.0"

View file

@ -188,7 +188,9 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
Clear,
Input,
InputKeys,
Keybindings,
Kill,
ListKeybindings,
Sleep,
TermSize,
};

View file

@ -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;

View file

@ -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())
}
}

View file

@ -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,
}]
}

View file

@ -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,
}
}

View file

@ -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;