Add 'help escapes' command for quick reference of nushell string escapes (#10522)

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

resolves #4869 

# 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.
-->
Adds a `help escape` command that can be used to display a table of
string escape sequences and their outputs.
```nu
help escapes
```
```nu
help escapes -h
```

The command should also appear in the list displayed when tab
autocompleting on `help`.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users can now use a new `help escapes` command to output a table of
string escape sequences and their outputs.

# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` 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.
-->
Need to update docs to reflect existence of the new `help escapes`
command.
This commit is contained in:
Ryan Armstrong 2023-09-30 10:04:27 -04:00 committed by GitHub
parent d34581db4a
commit 7eaa6d01ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 160 additions and 1 deletions

View File

@ -1,7 +1,7 @@
use nu_protocol::engine::{EngineState, StateWorkingSet}; use nu_protocol::engine::{EngineState, StateWorkingSet};
use crate::{ use crate::{
help::{HelpAliases, HelpCommands, HelpExterns, HelpModules, HelpOperators}, help::{HelpAliases, HelpCommands, HelpEscapes, HelpExterns, HelpModules, HelpOperators},
*, *,
}; };
pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState { pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
@ -127,6 +127,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
HelpCommands, HelpCommands,
HelpModules, HelpModules,
HelpOperators, HelpOperators,
HelpEscapes,
}; };
// Debug // Debug

View File

@ -0,0 +1,156 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
Value,
};
#[derive(Clone)]
pub struct HelpEscapes;
impl Command for HelpEscapes {
fn name(&self) -> &str {
"help escapes"
}
fn usage(&self) -> &str {
"Show help on nushell string escapes."
}
fn signature(&self) -> Signature {
Signature::build("help escapes")
.category(Category::Core)
.input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
.allow_variants_without_examples(true)
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let escape_info = generate_escape_info();
let mut recs = vec![];
for escape in escape_info {
recs.push(Value::record(
record! {
"sequence" => Value::string(escape.sequence, head),
"output" => Value::string(escape.output, head),
},
head,
));
}
Ok(recs
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
}
}
struct EscapeInfo {
sequence: String,
output: String,
}
fn generate_escape_info() -> Vec<EscapeInfo> {
vec![
EscapeInfo {
sequence: "\\\"".into(),
output: "\"".into(),
},
EscapeInfo {
sequence: "\\\'".into(),
output: "\'".into(),
},
EscapeInfo {
sequence: "\\\\".into(),
output: "\\".into(),
},
EscapeInfo {
sequence: "\\/".into(),
output: "/".into(),
},
EscapeInfo {
sequence: "\\(".into(),
output: "(".into(),
},
EscapeInfo {
sequence: "\\)".into(),
output: ")".into(),
},
EscapeInfo {
sequence: "\\{".into(),
output: "{".into(),
},
EscapeInfo {
sequence: "\\}".into(),
output: "}".into(),
},
EscapeInfo {
sequence: "\\$".into(),
output: "$".into(),
},
EscapeInfo {
sequence: "\\^".into(),
output: "^".into(),
},
EscapeInfo {
sequence: "\\#".into(),
output: "#".into(),
},
EscapeInfo {
sequence: "\\|".into(),
output: "|".into(),
},
EscapeInfo {
sequence: "\\~".into(),
output: "~".into(),
},
EscapeInfo {
sequence: "\\a".into(),
output: "alert bell".into(),
},
EscapeInfo {
sequence: "\\b".into(),
output: "backspace".into(),
},
EscapeInfo {
sequence: "\\e".into(),
output: "escape".into(),
},
EscapeInfo {
sequence: "\\f".into(),
output: "form feed".into(),
},
EscapeInfo {
sequence: "\\n".into(),
output: "newline (line feed)".into(),
},
EscapeInfo {
sequence: "\\r".into(),
output: "carriage return".into(),
},
EscapeInfo {
sequence: "\\t".into(),
output: "tab".into(),
},
EscapeInfo {
sequence: "\\u{X...}".into(),
output: "a single unicode character, where X... is 1-6 digits (0-9, A-F)".into(),
},
]
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::HelpEscapes;
use crate::test_examples;
test_examples(HelpEscapes {})
}
}

View File

@ -1,6 +1,7 @@
mod help_; mod help_;
mod help_aliases; mod help_aliases;
mod help_commands; mod help_commands;
mod help_escapes;
mod help_externs; mod help_externs;
mod help_modules; mod help_modules;
mod help_operators; mod help_operators;
@ -8,6 +9,7 @@ mod help_operators;
pub use help_::Help; pub use help_::Help;
pub use help_aliases::HelpAliases; pub use help_aliases::HelpAliases;
pub use help_commands::HelpCommands; pub use help_commands::HelpCommands;
pub use help_escapes::HelpEscapes;
pub use help_externs::HelpExterns; pub use help_externs::HelpExterns;
pub use help_modules::HelpModules; pub use help_modules::HelpModules;
pub use help_operators::HelpOperators; pub use help_operators::HelpOperators;