mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 20:03:54 +01:00
2bb0c1c618
# Description Add a `keybindings get` command to listen and get individual "keyboard" events. This includes different keyboard keys (see example of use) on seemingly all terminals and mouse, resize, focus and paste events on some special once. The record returned by this command is similar to crossterm event structure and is documented in help message. For ease of use, option `--types` can get a list of event types to filter only desired events automatically. Additionally `--raw` options displays raw code of char keys and numeric format of modifier flags. Example of use, moving a character around a grid with arrow keys: ```nu def test [] { mut x = 0 mut y = 0 loop { clear $x = ([([$x 4] | math min) 0] | math max) $y = ([([$y 4] | math min) 0] | math max) for i in 0..4 { for j in 0..4 { if $j == $x and $i == $y { print -n "*" } else { print -n "." } } print "" } let inp = (input listen-t [ key ]) match $inp.key { {type: other key: enter} => (break) {type: other key: up} => ($y = $y - 1) {type: other key: down} => ($y = $y + 1) {type: other key: left} => ($x = $x - 1) {type: other key: right} => ($x = $x + 1) _ => () } } } ``` # User-Facing Changes - New `keybindngs get` command - `keybindings listen` is left as is - New `input display` command in std, mirroring functionality of `keybindings listen` # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
65 lines
1.7 KiB
Plaintext
65 lines
1.7 KiB
Plaintext
def format-event [ ] {
|
|
let record = $in
|
|
|
|
# Replace numeric value of raw_code with hex string
|
|
let record = match $record {
|
|
{raw_code: $code} => {
|
|
$record | update raw_code {|| $in | fmt | get upperhex}
|
|
}
|
|
_ => $record
|
|
}
|
|
|
|
# Replace numeric value of raw_modifiers with binary string
|
|
let record = match $record {
|
|
{raw_modifiers: $flags} => {
|
|
$record | update raw_modifiers {|| $in | fmt | get binary}
|
|
}
|
|
_ => $record
|
|
}
|
|
|
|
# Format into oneliner with `to nuon` and remove wrapping bracket pair
|
|
$record | to nuon | str substring 1..-1
|
|
}
|
|
|
|
# Display user interface events
|
|
#
|
|
# Press escape to stop
|
|
#
|
|
# To get individual events as records use "input listen"
|
|
export def display [
|
|
--types(-t): list<string> # Listen for event of specified types only (can be one of: focus, key, mouse, paste, resize)
|
|
--raw(-r) # Add raw_code field with numeric value of keycode and raw_flags with bit mask flags
|
|
] {
|
|
let arg_types = if $types == null {
|
|
[ key focus mouse paste resize ]
|
|
} else if 'key' not-in $types {
|
|
$types | append 'key'
|
|
} else {
|
|
$types
|
|
}
|
|
|
|
# To get exit key 'escape' we need to read key
|
|
# type events, however user may filter them out
|
|
# using --types and they should not be displayed
|
|
let filter_keys = ($types != null and 'key' not-in $types)
|
|
|
|
loop {
|
|
let next_key = if $raw {
|
|
input listen -t $arg_types -r
|
|
} else {
|
|
input listen -t $arg_types
|
|
}
|
|
|
|
match $next_key {
|
|
{type: key key_type: other code: esc modifiers: []} => {
|
|
return
|
|
}
|
|
_ => {
|
|
if (not $filter_keys) or $next_key.type != 'key' {
|
|
$next_key | format-event | print
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|