nushell/crates/nu-command/src/platform
Bahex 32196cfe78
Add term query, for querying information from terminals. (#14427)
## Related
- #10150
- https://github.com/nushell/nushell/pull/10150#issuecomment-1721238336
- #10387
- https://github.com/nushell/nushell/pull/10387#issuecomment-1722228185

# Description
`term query`: a command for querying information from the terminal.

Prints the `$query`, and immediately starts reading raw bytes from
stdin.

The standard input will be read until the `terminator` sequence is
encountered.
The `terminator` is not removed from the output.

It also stops on <kbd>Ctrl-C</kbd> with an error.

```
Usage:
  > term query {flags} <query> 

Flags:
  -h, --help: Display the help message for this command
  -t, --terminator (required parameter) <one_of(binary, string)>: stdin will be read until this sequence is encountered

Parameters:
  query <one_of(binary, string)>: The query that will be printed to stdout
```

This was previously possible with `input` until #10150.
`input` command's features such as cursor control, deleting input etc.
are useful, but interfere with this use case.

`term query` makes the following uses possible:

```nushell
# get the terminal size with ansi escape codes
def terminal-size [] {
    let response = term query (ansi size) --terminator 'R'
    # $response should look like this
    # Length: 9 (0x9) bytes | printable whitespace ascii_other non_ascii
    # 00000000:   1b 5b 33 38  3b 31 35 30  52             •[38;150R

    let sz = $response | bytes at 2..<-1 | decode
    # 38;150

    # $sz should look like 38;150
    let size = ($sz | split row ';' | each {into int})

    # output in record syntax
    {
        rows: $size.0
        columns: $size.1
    }
}
```

```nushell
# read clipboard content using OSC 52
term query $"(ansi --osc '52;c;?')(ansi st)" --terminator (ansi st)
| bytes at 7..<-2
| decode
| decode base64
| decode
```

# User-Facing Changes
- added `ansi query`

# Tests + Formatting
- Integration tests should be added if possible.
2024-11-25 15:13:11 -06:00
..
ansi Drop once_cell dependency (#14198) 2024-10-29 17:33:46 +01:00
input add --default flag to input command (#14374) 2024-11-18 17:14:12 -06:00
term Add term query, for querying information from terminals. (#14427) 2024-11-25 15:13:11 -06:00
clear.rs Send both 2J and 3J on clear (#14181) 2024-10-28 06:42:18 -05:00
dir_info.rs Add and use new Signals struct (#13314) 2024-07-07 22:29:01 +00:00
is_terminal.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
kill.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
mod.rs Add term query, for querying information from terminals. (#14427) 2024-11-25 15:13:11 -06:00
sleep.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
ulimit.rs Drop once_cell dependency (#14198) 2024-10-29 17:33:46 +01:00
whoami.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00