Files
nushell/crates/nu-command/src/help/help_escapes.rs
Ian Manske 9996e4a1f8 Shrink the size of Expr (#12610)
# Description
Continuing from #12568, this PR further reduces the size of `Expr` from
64 to 40 bytes. It also reduces `Expression` from 128 to 96 bytes and
`Type` from 32 to 24 bytes.

This was accomplished by:
- for `Expr` with multiple fields (e.g., `Expr::Thing(A, B, C)`),
merging the fields into new AST struct types and then boxing this struct
(e.g. `Expr::Thing(Box<ABC>)`).
- replacing `Vec<T>` with `Box<[T]>` in multiple places. `Expr`s and
`Expression`s should rarely be mutated, if at all, so this optimization
makes sense.

By reducing the size of these types, I didn't notice a large performance
improvement (at least compared to #12568). But this PR does reduce the
memory usage of nushell. My config is somewhat light so I only noticed a
difference of 1.4MiB (38.9MiB vs 37.5MiB).

---------

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
2024-04-24 15:46:35 +00:00

152 lines
3.7 KiB
Rust

use nu_engine::command_prelude::*;
#[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())])
.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 {})
}
}