mirror of
https://github.com/nushell/nushell.git
synced 2025-05-19 01:10:48 +02:00
833471241a
4 Commits
Author | SHA1 | Message | Date | |
---|---|---|---|---|
|
833471241a
|
Refactor: Construct IoError from std::io::Error instead of std::io::ErrorKind (#15777)
|
||
|
66bc0542e0
|
Refactor I/O Errors (#14927)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
As mentioned in #10698, we have too many `ShellError` variants, with
some even overlapping in meaning. This PR simplifies and improves I/O
error handling by restructuring `ShellError` related to I/O issues.
Previously, `ShellError::IOError` only contained a message string,
making it convenient but overly generic. It was widely used without
providing spans (#4323).
This PR introduces a new `ShellError::Io` variant that consolidates
multiple I/O-related errors (except for `ShellError::NetworkFailure`,
which remains distinct for now). The new `ShellError::Io` variant
replaces the following:
- `FileNotFound`
- `FileNotFoundCustom`
- `IOInterrupted`
- `IOError`
- `IOErrorSpanned`
- `NotADirectory`
- `DirectoryNotFound`
- `MoveNotPossible`
- `CreateNotPossible`
- `ChangeAccessTimeNotPossible`
- `ChangeModifiedTimeNotPossible`
- `RemoveNotPossible`
- `ReadingFile`
## The `IoError`
`IoError` includes the following fields:
1. **`kind`**: Extends `std::io::ErrorKind` to specify the type of I/O
error without needing new `ShellError` variants. This aligns with the
approach used in `std::io::Error`. This adds a second dimension to error
reporting by combining the `kind` field with `ShellError` variants,
making it easier to describe errors in more detail. As proposed by
@kubouch in [#design-discussion on
Discord](https://discord.com/channels/601130461678272522/615329862395101194/1323699197165178930),
this helps reduce the number of `ShellError` variants. In the error
report, the `kind` field is displayed as the "source" of the error,
e.g., "I/O error," followed by the specific kind of I/O error.
2. **`span`**: A non-optional field to encourage providing spans for
better error reporting (#4323).
3. **`path`**: Optional `PathBuf` to give context about the file or
directory involved in the error (#7695). If provided, it’s shown as a
help entry in error reports.
4. **`additional_context`**: Allows adding custom messages when the
span, kind, and path are insufficient. This is rendered in the error
report at the labeled span.
5. **`location`**: Sometimes, I/O errors occur in the engine itself and
are not caused directly by user input. In such cases, if we don’t have a
span and must set it to `Span::unknown()`, we need another way to
reference the error. For this, the `location` field uses the new
`Location` struct, which records the Rust file and line number where the
error occurred. This ensures that we at least know the Rust code
location that failed, helping with debugging. To make this work, a new
`location!` macro was added, which retrieves `file!`, `line!`, and
`column!` values accurately. If `Location::new` is used directly, it
issues a warning to remind developers to use the macro instead, ensuring
consistent and correct usage.
### Constructor Behavior
`IoError` provides five constructor methods:
- `new` and `new_with_additional_context`: Used for errors caused by
user input and require a valid (non-unknown) span to ensure precise
error reporting.
- `new_internal` and `new_internal_with_path`: Used for internal errors
where a span is not available. These methods require additional context
and the `Location` struct to pinpoint the source of the error in the
engine code.
- `factory`: Returns a closure that maps an `std::io::Error` to an
`IoError`. This is useful for handling multiple I/O errors that share
the same span and path, streamlining error handling in such cases.
## New Report Look
This is simulation how the I/O errors look like (the `open crates` is
simulated to show how internal errors are referenced now):

## `Span::test_data()`
To enable better testing, `Span::test_data()` now returns a value
distinct from `Span::unknown()`. Both `Span::test_data()` and
`Span::unknown()` refer to invalid source code, but having a separate
value for test data helps identify issues during testing while keeping
spans unique.
## Cursed Sneaky Error Transfers
I removed the conversions between `std::io::Error` and `ShellError` as
they often removed important information and were used too broadly to
handle I/O errors. This also removed the problematic implementation
found here:
|
||
|
dfec687a46
|
term query : refactor, add --beginning flag (#14446)
# Description - Refactor code to be simpler. - Make the mentioned changes. - `scopeguard` is added as a direct dependency. Helps simplify the code. Rather than roll an ad-hoc version of it myself, I thought it would be better to use `scopeguard` as it was already an indirect dependency. # User-Facing Changes - Add `--beginning` flag, which is used to validate the response and provide early errors in case of unexpected inputs. - Both `terminator` and `beginning` sequences (when provided) are not included in the command's output. Turns out they are almost always removed from the output, and because they are known beforehand they can be added back by the user. |
||
|
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. |