nushell/crates/nu-command/src/strings/char_.rs

418 lines
15 KiB
Rust
Raw Normal View History

use indexmap::{indexmap, IndexMap};
use nu_engine::command_prelude::*;
Make string related commands parse-time evaluatable (#13032) <!-- 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! --> Related meta-issue: #10239. # 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. --> This PR will modify some `str`-related commands so that they can be evaluated at parse time. See the following list for those implemented by this pr. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> Available now: - `str` subcommands - `trim` - `contains` - `distance` - `ends-with` - `expand` - `index-of` - `join` - `replace` - `reverse` - `starts-with` - `stats` - `substring` - `capitalize` - `downcase` - `upcase` - `split` subcommands - `chars` - `column` - `list` - `row` - `words` - `format` subcommands - `date` - `duration` - `filesize` - string related commands - `parse` - `detect columns` - `encode` & `decode` # 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 toolkit.nu; toolkit test stdlib"` 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 > ``` --> Unresolved questions: - [ ] Is there any routine of testing const expressions? I haven't found any yet. - [ ] Is const expressions required to behave just like there non-const version, like what rust promises? # 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. --> Unresolved questions: - [ ] Do const commands need special marks in the docs?
2024-06-05 19:21:52 +00:00
use once_cell::sync::Lazy;
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
use std::sync::{atomic::AtomicBool, Arc};
// Character used to separate directories in a Path Environment variable on windows is ";"
#[cfg(target_family = "windows")]
const ENV_PATH_SEPARATOR_CHAR: char = ';';
// Character used to separate directories in a Path Environment variable on linux/mac/unix is ":"
#[cfg(not(target_family = "windows"))]
const ENV_PATH_SEPARATOR_CHAR: char = ':';
#[derive(Clone)]
pub struct Char;
static CHAR_MAP: Lazy<IndexMap<&'static str, String>> = Lazy::new(|| {
indexmap! {
// These are some regular characters that either can't be used or
// it's just easier to use them like this.
"nul" => '\x00'.to_string(), // nul character, 0x00
"null_byte" => '\x00'.to_string(), // nul character, 0x00
"zero_byte" => '\x00'.to_string(), // nul character, 0x00
// This are the "normal" characters section
"newline" => '\n'.to_string(),
"enter" => '\n'.to_string(),
"nl" => '\n'.to_string(),
"line_feed" => '\n'.to_string(),
"lf" => '\n'.to_string(),
"carriage_return" => '\r'.to_string(),
"cr" => '\r'.to_string(),
"crlf" => "\r\n".to_string(),
"tab" => '\t'.to_string(),
"sp" => ' '.to_string(),
"space" => ' '.to_string(),
"pipe" => '|'.to_string(),
"left_brace" => '{'.to_string(),
"lbrace" => '{'.to_string(),
"right_brace" => '}'.to_string(),
"rbrace" => '}'.to_string(),
"left_paren" => '('.to_string(),
2021-12-18 18:13:10 +00:00
"lp" => '('.to_string(),
"lparen" => '('.to_string(),
"right_paren" => ')'.to_string(),
"rparen" => ')'.to_string(),
2021-12-18 18:13:10 +00:00
"rp" => ')'.to_string(),
"left_bracket" => '['.to_string(),
"lbracket" => '['.to_string(),
"right_bracket" => ']'.to_string(),
"rbracket" => ']'.to_string(),
"single_quote" => '\''.to_string(),
"squote" => '\''.to_string(),
"sq" => '\''.to_string(),
"double_quote" => '\"'.to_string(),
"dquote" => '\"'.to_string(),
"dq" => '\"'.to_string(),
"path_sep" => std::path::MAIN_SEPARATOR.to_string(),
"psep" => std::path::MAIN_SEPARATOR.to_string(),
"separator" => std::path::MAIN_SEPARATOR.to_string(),
"esep" => ENV_PATH_SEPARATOR_CHAR.to_string(),
"env_sep" => ENV_PATH_SEPARATOR_CHAR.to_string(),
"tilde" => '~'.to_string(), // ~
"twiddle" => '~'.to_string(), // ~
"squiggly" => '~'.to_string(), // ~
"home" => '~'.to_string(), // ~
"hash" => '#'.to_string(), // #
"hashtag" => '#'.to_string(), // #
"pound_sign" => '#'.to_string(), // #
"sharp" => '#'.to_string(), // #
"root" => '#'.to_string(), // #
// This is the unicode section
// Unicode names came from https://www.compart.com/en/unicode
// Private Use Area (U+E000-U+F8FF)
// Unicode can't be mixed with Ansi or it will break width calculation
2022-02-18 10:52:48 +00:00
"nf_branch" => '\u{e0a0}'.to_string(), // 
"nf_segment" => '\u{e0b0}'.to_string(), // 
"nf_left_segment" => '\u{e0b0}'.to_string(), // 
"nf_left_segment_thin" => '\u{e0b1}'.to_string(), // 
"nf_right_segment" => '\u{e0b2}'.to_string(), // 
"nf_right_segment_thin" => '\u{e0b3}'.to_string(), // 
"nf_git" => '\u{f1d3}'.to_string(), // 
"nf_git_branch" => "\u{e709}\u{e0a0}".to_string(), // 
"nf_folder1" => '\u{f07c}'.to_string(), // 
"nf_folder2" => '\u{f115}'.to_string(), // 
"nf_house1" => '\u{f015}'.to_string(), // 
"nf_house2" => '\u{f7db}'.to_string(), // 
"identical_to" => '\u{2261}'.to_string(), // ≡
"hamburger" => '\u{2261}'.to_string(), // ≡
"not_identical_to" => '\u{2262}'.to_string(), // ≢
"branch_untracked" => '\u{2262}'.to_string(), // ≢
"strictly_equivalent_to" => '\u{2263}'.to_string(), // ≣
"branch_identical" => '\u{2263}'.to_string(), // ≣
"upwards_arrow" => '\u{2191}'.to_string(), // ↑
"branch_ahead" => '\u{2191}'.to_string(), // ↑
"downwards_arrow" => '\u{2193}'.to_string(), // ↓
"branch_behind" => '\u{2193}'.to_string(), // ↓
"up_down_arrow" => '\u{2195}'.to_string(), // ↕
"branch_ahead_behind" => '\u{2195}'.to_string(), // ↕
"black_right_pointing_triangle" => '\u{25b6}'.to_string(), // ▶
"prompt" => '\u{25b6}'.to_string(), // ▶
"vector_or_cross_product" => '\u{2a2f}'.to_string(), //
"failed" => '\u{2a2f}'.to_string(), //
"high_voltage_sign" => '\u{26a1}'.to_string(), // ⚡
"elevated" => '\u{26a1}'.to_string(), // ⚡
// This is the emoji section
// Weather symbols
// https://www.babelstone.co.uk/Unicode/whatisit.html
"sun" => "☀️".to_string(), //2600 + fe0f
"sunny" => "☀️".to_string(), //2600 + fe0f
"sunrise" => "☀️".to_string(), //2600 + fe0f
"moon" => "🌛".to_string(), //1f31b
"cloudy" => "☁️".to_string(), //2601 + fe0f
"cloud" => "☁️".to_string(), //2601 + fe0f
"clouds" => "☁️".to_string(), //2601 + fe0f
"rainy" => "🌦️".to_string(), //1f326 + fe0f
"rain" => "🌦️".to_string(), //1f326 + fe0f
"foggy" => "🌫️".to_string(), //1f32b + fe0f
"fog" => "🌫️".to_string(), //1f32b + fe0f
"mist" => '\u{2591}'.to_string(), //2591
"haze" => '\u{2591}'.to_string(), //2591
"snowy" => "❄️".to_string(), //2744 + fe0f
"snow" => "❄️".to_string(), //2744 + fe0f
"thunderstorm" => "🌩️".to_string(),//1f329 + fe0f
"thunder" => "🌩️".to_string(), //1f329 + fe0f
// This is the "other" section
"bel" => '\x07'.to_string(), // Terminal Bell
"backspace" => '\x08'.to_string(), // Backspace
// separators
"file_separator" => '\x1c'.to_string(),
"file_sep" => '\x1c'.to_string(),
"fs" => '\x1c'.to_string(),
"group_separator" => '\x1d'.to_string(),
"group_sep" => '\x1d'.to_string(),
"gs" => '\x1d'.to_string(),
"record_separator" => '\x1e'.to_string(),
"record_sep" => '\x1e'.to_string(),
"rs" => '\x1e'.to_string(),
"unit_separator" => '\x1f'.to_string(),
"unit_sep" => '\x1f'.to_string(),
"us" => '\x1f'.to_string(),
}
});
impl Command for Char {
fn name(&self) -> &str {
"char"
}
fn signature(&self) -> Signature {
Signature::build("char")
.input_output_types(vec![(Type::Nothing, Type::Any)])
.optional(
"character",
SyntaxShape::Any,
"The name of the character to output.",
)
.rest("rest", SyntaxShape::Any, "Multiple Unicode bytes.")
.switch("list", "List all supported character names", Some('l'))
.switch("unicode", "Unicode string i.e. 1f378", Some('u'))
.switch("integer", "Create a codepoint from an integer", Some('i'))
update `char` signature with `Table` (#9895) # Description This PR updates the `char` command to allow `Table` output due to the `--list` parameter. ### Before ```nushell char --list | transpose Error: nu::parser::input_type_mismatch × Command does not support string input. ╭─[entry #6:1:1] 1 │ char --list | transpose · ────┬──── · ╰── command doesn't support string input ╰──── ``` ### After ```nushell ❯ char --list | transpose ╭───┬───────────┬─────────┬─────────┬─────────┬───────────┬─────────┬─────────────┬─────────┬─────────┬─────────┬──────────┬──────────┬──────────┬────────────┬──────────┬─────────────┬──────────┬────────────┬──────────┬──────────┬─────╮ │ # │ column0 │ column1 │ column2 │ column3 │ column4 │ column5 │ column6 │ column7 │ column8 │ column9 │ column10 │ column11 │ column12 │ column13 │ column14 │ column15 │ column16 │ column17 │ column18 │ column19 │ ... │ ├───┼───────────┼─────────┼─────────┼─────────┼───────────┼─────────┼─────────────┼─────────┼─────────┼─────────┼──────────┼──────────┼──────────┼────────────┼──────────┼─────────────┼──────────┼────────────┼──────────┼──────────┼─────┤ │ 0 │ name │ newline │ enter │ nl │ line_feed │ lf │ carriage_re │ cr │ crlf │ tab │ sp │ space │ pipe │ left_brace │ lbrace │ right_brace │ rbrace │ left_paren │ lp │ lparen │ ... │ │ │ │ │ │ │ │ │ turn │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ 1 │ character │ │ │ │ │ │ │ │ │ │ │ │ | │ { │ { │ } │ } │ ( │ ( │ ( │ ... │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ 2 │ unicode │ a │ a │ a │ a │ a │ d │ d │ d a │ 9 │ 20 │ 20 │ 7c │ 7b │ 7b │ 7d │ 7d │ 28 │ 28 │ 28 │ ... │ ╰───┴───────────┴─────────┴─────────┴─────────┴───────────┴─────────┴─────────────┴─────────┴─────────┴─────────┴──────────┴──────────┴──────────┴────────────┴──────────┴─────────────┴──────────┴────────────┴──────────┴──────────┴─────╯ ``` # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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 -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `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. -->
2023-08-02 13:42:13 +00:00
.allow_variants_without_examples(true)
.category(Category::Strings)
}
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
fn is_const(&self) -> bool {
true
}
fn usage(&self) -> &str {
"Output special characters (e.g., 'newline')."
}
fn search_terms(&self) -> Vec<&str> {
vec!["line break", "newline", "Unicode"]
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Output newline",
example: r#"char newline"#,
result: Some(Value::test_string("\n")),
},
Example {
description: "List available characters",
example: r#"char --list"#,
result: None,
},
Example {
2022-10-26 16:36:42 +00:00
description: "Output prompt character, newline and a hamburger menu character",
example: r#"(char prompt) + (char newline) + (char hamburger)"#,
result: Some(Value::test_string("\u{25b6}\n\u{2261}")),
},
Example {
description: "Output Unicode character",
2023-10-19 20:08:09 +00:00
example: r#"char --unicode 1f378"#,
result: Some(Value::test_string("\u{1f378}")),
},
2022-04-13 10:33:08 +00:00
Example {
description: "Create Unicode from integer codepoint values",
2023-10-19 20:08:09 +00:00
example: r#"char --integer (0x60 + 1) (0x60 + 2)"#,
result: Some(Value::test_string("ab")),
2022-04-13 10:33:08 +00:00
},
Example {
description: "Output multi-byte Unicode character",
2023-10-19 20:08:09 +00:00
example: r#"char --unicode 1F468 200D 1F466 200D 1F466"#,
result: Some(Value::test_string(
"\u{1F468}\u{200D}\u{1F466}\u{200D}\u{1F466}",
)),
},
]
}
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let call_span = call.head;
let list = call.has_flag_const(working_set, "list")?;
let integer = call.has_flag_const(working_set, "integer")?;
let unicode = call.has_flag_const(working_set, "unicode")?;
let ctrlc = working_set.permanent().ctrlc.clone();
// handle -l flag
if list {
return Ok(generate_character_list(ctrlc, call.head));
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
// handle -i flag
if integer {
let int_args = call.rest_const(working_set, 0)?;
handle_integer_flag(int_args, call_span)
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
// handle -u flag
else if unicode {
let string_args = call.rest_const(working_set, 0)?;
handle_unicode_flag(string_args, call_span)
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
// handle the rest
else {
let string_args = call.rest_const(working_set, 0)?;
handle_the_rest(string_args, call_span)
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let call_span = call.head;
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
let list = call.has_flag(engine_state, stack, "list")?;
let integer = call.has_flag(engine_state, stack, "integer")?;
let unicode = call.has_flag(engine_state, stack, "unicode")?;
let ctrlc = engine_state.ctrlc.clone();
// handle -l flag
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
if list {
return Ok(generate_character_list(ctrlc, call_span));
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
// handle -i flag
if integer {
let int_args = call.rest(engine_state, stack, 0)?;
handle_integer_flag(int_args, call_span)
}
// handle -u flag
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
else if unicode {
let string_args = call.rest(engine_state, stack, 0)?;
handle_unicode_flag(string_args, call_span)
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
// handle the rest
else {
let string_args = call.rest(engine_state, stack, 0)?;
handle_the_rest(string_args, call_span)
}
}
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
fn generate_character_list(ctrlc: Option<Arc<AtomicBool>>, call_span: Span) -> PipelineData {
CHAR_MAP
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
.iter()
.map(move |(name, s)| {
let unicode = Value::string(
s.chars()
.map(|c| format!("{:x}", c as u32))
.collect::<Vec<String>>()
.join(" "),
call_span,
);
let record = record! {
"name" => Value::string(*name, call_span),
"character" => Value::string(s, call_span),
"unicode" => unicode,
};
Value::record(record, call_span)
})
.into_pipeline_data(call_span, ctrlc)
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
fn handle_integer_flag(
int_args: Vec<Spanned<i64>>,
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
call_span: Span,
) -> Result<PipelineData, ShellError> {
if int_args.is_empty() {
return Err(ShellError::MissingParameter {
param_name: "missing at least one unicode character".into(),
span: call_span,
});
}
let str = int_args
.into_iter()
.map(integer_to_unicode_char)
.collect::<Result<String, _>>()?;
Ok(Value::string(str, call_span).into_pipeline_data())
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
fn handle_unicode_flag(
string_args: Vec<Spanned<String>>,
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
call_span: Span,
) -> Result<PipelineData, ShellError> {
if string_args.is_empty() {
return Err(ShellError::MissingParameter {
param_name: "missing at least one unicode character".into(),
span: call_span,
});
}
let str = string_args
.into_iter()
.map(string_to_unicode_char)
.collect::<Result<String, _>>()?;
Ok(Value::string(str, call_span).into_pipeline_data())
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
}
fn handle_the_rest(
string_args: Vec<Spanned<String>>,
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
call_span: Span,
) -> Result<PipelineData, ShellError> {
let Some(s) = string_args.first() else {
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
return Err(ShellError::MissingParameter {
param_name: "missing name of the character".into(),
span: call_span,
});
};
let special_character = str_to_character(&s.item);
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
if let Some(output) = special_character {
Ok(Value::string(output, call_span).into_pipeline_data())
} else {
Err(ShellError::TypeMismatch {
err_message: "error finding named character".into(),
span: s.span,
make `char` command `const` (#11771) # Description This PR makes the `char` command a `const` command. The only real changes are to get the arguments different and I extracted code into functions so they could be called via run and run_cost. No algorithms were harmed in the making of this PR. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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. -->
2024-02-07 22:29:00 +00:00
})
}
}
fn integer_to_unicode_char(value: Spanned<i64>) -> Result<char, ShellError> {
let decoded_char = value.item.try_into().ok().and_then(std::char::from_u32);
if let Some(ch) = decoded_char {
Ok(ch)
} else {
Err(ShellError::TypeMismatch {
err_message: "not a valid Unicode codepoint".into(),
span: value.span,
})
}
}
fn string_to_unicode_char(s: Spanned<String>) -> Result<char, ShellError> {
let decoded_char = u32::from_str_radix(&s.item, 16)
.ok()
.and_then(std::char::from_u32);
if let Some(ch) = decoded_char {
Ok(ch)
} else {
Err(ShellError::TypeMismatch {
err_message: "error decoding Unicode character".into(),
span: s.span,
})
}
}
fn str_to_character(s: &str) -> Option<String> {
CHAR_MAP.get(s).map(|s| s.into())
}
#[cfg(test)]
mod tests {
use super::Char;
#[test]
fn examples_work_as_expected() {
use crate::test_examples;
test_examples(Char {})
}
}