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
8 changed files with 392 additions and 224 deletions

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

@ -0,0 +1,151 @@
use crossterm::QueueableCommand;
use crossterm::{event::Event, event::KeyCode, event::KeyEvent, terminal};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
};
use std::io::{stdout, Write};
#[derive(Clone)]
pub struct InputKeys;
impl Command for InputKeys {
fn name(&self) -> &str {
"keybindings listen"
}
fn usage(&self) -> &str {
"Get input from the user."
}
fn signature(&self) -> Signature {
Signature::build(self.name()).category(Category::Platform)
}
fn run(
&self,
_engine_state: &EngineState,
stack: &mut Stack,
_call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
println!("Type any key combination to see key details. Press ESC to abort.");
match print_events(stack) {
Ok(v) => Ok(v.into_pipeline_data()),
Err(e) => {
terminal::disable_raw_mode()?;
Err(ShellError::LabeledError(
"Error with input".to_string(),
e.to_string(),
))
}
}
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Type and see key event codes",
example: "keybindings listen",
result: None,
}]
}
}
pub fn print_events(stack: &mut Stack) -> Result<Value, ShellError> {
let config = stack.get_config()?;
stdout().flush()?;
terminal::enable_raw_mode()?;
let mut stdout = std::io::BufWriter::new(std::io::stderr());
loop {
let event = crossterm::event::read()?;
if event == Event::Key(KeyCode::Esc.into()) {
break;
}
// stdout.queue(crossterm::style::Print(format!("event: {:?}", &event)))?;
// stdout.queue(crossterm::style::Print("\r\n"))?;
// Get a record
let v = print_events_helper(event)?;
// Print out the record
let o = match v {
Value::Record { cols, vals, .. } => cols
.iter()
.zip(vals.iter())
.map(|(x, y)| format!("{}: {}", x, y.into_string("", &config)))
.collect::<Vec<String>>()
.join(", "),
_ => "".to_string(),
};
stdout.queue(crossterm::style::Print(o))?;
stdout.queue(crossterm::style::Print("\r\n"))?;
stdout.flush()?;
}
terminal::disable_raw_mode()?;
Ok(Value::nothing(Span::test_data()))
}
// this fn is totally ripped off from crossterm's examples
// it's really a diagnostic routine to see if crossterm is
// even seeing the events. if you press a key and no events
// are printed, it's a good chance your terminal is eating
// those events.
fn print_events_helper(event: Event) -> Result<Value, ShellError> {
if let Event::Key(KeyEvent { code, modifiers }) = event {
match code {
KeyCode::Char(c) => {
let record = Value::Record {
cols: vec![
"char".into(),
"code".into(),
"modifier".into(),
"flags".into(),
],
vals: vec![
Value::string(format!("{}", c), Span::test_data()),
Value::string(format!("{:#08x}", u32::from(c)), Span::test_data()),
Value::string(format!("{:?}", modifiers), Span::test_data()),
Value::string(format!("{:#08b}", modifiers), Span::test_data()),
],
span: Span::test_data(),
};
Ok(record)
}
_ => {
let record = Value::Record {
cols: vec!["code".into(), "modifier".into(), "flags".into()],
vals: vec![
Value::string(format!("{:?}", code), Span::test_data()),
Value::string(format!("{:?}", modifiers), Span::test_data()),
Value::string(format!("{:#08b}", modifiers), Span::test_data()),
],
span: Span::test_data(),
};
Ok(record)
}
}
} else {
let record = Value::Record {
cols: vec!["event".into()],
vals: vec![Value::string(format!("{:?}", event), Span::test_data())],
span: Span::test_data(),
};
Ok(record)
}
}
#[cfg(test)]
mod tests {
use super::InputKeys;
#[test]
fn examples_work_as_expected() {
use crate::test_examples;
test_examples(InputKeys {})
}
}

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;