nushell/crates/nu-std/std/input/mod.nu
Douglas 2a3805c164
Virtual std module subdirectories (#14040)
# Description

Uses "normal" module `std/<submodule>/mod.nu` instead of renaming the
files (as requested in #13842).

# User-Facing Changes

No user-facing changes other than in `view files` results. Imports
remain the same after this PR.

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

Also manually confirmed that it does not interfere with nupm, since we
did have a conflict at one point (and it's not possible to test here).

# Performance Tests

## Linux

### Nushell Startup - No config

```nu
bench --pretty -n 200  { <path_to>/nu -c "exit" }
```

| Release | Startup Time |
| --- | --- |
| 0.98.0 | 22ms 730µs 768ns +/- 1ms 515µs 942ns
| This commit | 9ms 312µs 68ns +/- 709µs 378ns
| Yesterday's nightly | 9ms 230µs 953ns +/- 9ms 67µs 689ns

### Nushell Startup - Load full standard library

Measures relative impact of a full `use std *`, which isn't recommended,
but worth tracking.

```nu
bench --pretty -n 200  { <path_to>/nu -c "use std *; exit" }
```

| Release | Startup Time |
| --- | --- |
| 0.98.0 | 23ms 10µs 636ns +/- 1ms 277µs 854ns
| This commit | 26ms 922µs 769ns +/- 562µs 538ns
| Yesterday's nightly | 28ms 133µs 95ns +/- 761µs 943ns
| `deprecated_dirs` removal PR * | 23ms 610µs 333ns +/- 369µs 436ns

\* Current increase is partially due to double-loading `dirs` with
removal warning in older version.

# After Submitting

Still TODO - Update standard library doc
2024-10-10 06:56:37 -05:00

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