Files
b87e218506 docs(input listen): describe the actual --raw fields and key_type variants (#19517)
## Description

Fixes #19519

`input listen --raw` and `std/input display --raw` both documented a
`raw_flags` field that does not exist. The implementation emits
`raw_modifiers` instead:

```console
$ printf 'a' | script -qfec "nu -n -c 'input listen --types [key] --raw | to nuon'" /dev/null
{type: key, key_type: char, code: a, modifiers: [], raw_code: 97, raw_modifiers: 0}

$ printf '\033OP' | script -qfec "nu -n -c 'input listen --types [key] --raw | to nuon'" /dev/null
{type: key, key_type: f, code: "1", modifiers: [], raw_modifiers: 0}
```

So `raw_modifiers` is added to key and mouse events, while `raw_code` is
added
only for character keys (`create_key_event` pushes it inside
`if let KeyCode::Char(c)`). The old help said neither of these.

This also fixes two related gaps in `extra_description`:

- It listed "4 `key_type` variants", but `get_keycode_name` has a fifth,
`KeyCode::Modifier(m) => "modifier"` (crossterm maps these through the
kitty
  keyboard protocol, e.g. `ModifierKeyCode::LeftShift`).
- It listed the five base event shapes without mentioning the extra
fields
  added under `--raw`.

Help text only — no behavior change. Two files:
`crates/nu-command/src/platform/input/input_listen.rs` and
`crates/nu-std/std/input/mod.nu`.

## User-facing changes (Release notes)

- n/a (documentation only; no behavior change)

## Additional notes

- Verified with a rebuilt `nu` binary that `help input listen` and
  `help display` render the corrected text, and that `--raw` output is
  unchanged for both a character key and F1.
- `cargo test -p nu-command --lib`: 953 passed / 0 failed / 2 ignored.
Example tests: 261 passed / 0 failed. `cargo fmt --check` and the
`typos`
  check on both changed files are clean.
- Related: #15868 (older report about `input listen`'s wrong output-type
docs),
  #19513 (the output-type fix). This PR does not close either.

Co-authored-by: wangjianjun <passionworkeer@users.noreply.github.com>
Co-authored-by: wangjianjun <opuswang@tencent.com>
2026-11-14 09:03:54 -06:00

69 lines
1.9 KiB
Nu

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 | format number | get upperhex}
}
_ => $record
}
# Replace numeric value of raw_modifiers with binary string
let record = match $record {
{raw_modifiers: $flags} => {
$record | update raw_modifiers {|| $in | format number | get binary}
}
_ => $record
}
# Format into oneliner with `to nuon` and remove wrapping bracket pair
$record | to nuon | str substring 1..-2
}
# Display user interface events
#
# Press escape to stop
#
# To get individual events as records use "input listen"
#
# If focus events repeat endlessly, the terminal reports focus whenever focus
# reporting is turned on. Leave focus out of --types to stop them:
# input display --types [key mouse paste resize]
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 a raw_modifiers bit mask to key and mouse events, plus raw_code for character keys
] {
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
}
}
}
}
}