# Description This PR renames the conversion functions on `Value` to be more consistent. It follows the Rust [API guidelines](https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv) for ad-hoc conversions. The conversion functions on `Value` now come in a few forms: - `coerce_{type}` takes a `&Value` and attempts to convert the value to `type` (e.g., `i64` are converted to `f64`). This is the old behavior of some of the `as_{type}` functions -- these functions have simply been renamed to better reflect what they do. - The new `as_{type}` functions take a `&Value` and returns an `Ok` result only if the value is of `type` (no conversion is attempted). The returned value will be borrowed if `type` is non-`Copy`, otherwise an owned value is returned. - `into_{type}` exists for non-`Copy` types, but otherwise does not attempt conversion just like `as_type`. It takes an owned `Value` and always returns an owned result. - `coerce_into_{type}` has the same relationship with `coerce_{type}` as `into_{type}` does with `as_{type}`. - `to_{kind}_string`: conversion to different string formats (debug, abbreviated, etc.). Only two of the old string conversion functions were removed, the rest have been renamed only. - `to_{type}`: other conversion functions. Currently, only `to_path` exists. (And `to_string` through `Display`.) This table summaries the above: | Form | Cost | Input Ownership | Output Ownership | Converts `Value` case/`type` | | ---------------------------- | ----- | --------------- | ---------------- | -------- | | `as_{type}` | Cheap | Borrowed | Borrowed/Owned | No | | `into_{type}` | Cheap | Owned | Owned | No | | `coerce_{type}` | Cheap | Borrowed | Borrowed/Owned | Yes | | `coerce_into_{type}` | Cheap | Owned | Owned | Yes | | `to_{kind}_string` | Expensive | Borrowed | Owned | Yes | | `to_{type}` | Expensive | Borrowed | Owned | Yes | # User-Facing Changes Breaking API change for `Value` in `nu-protocol` which is exposed as part of the plugin API.
116 lines
3.4 KiB
Rust
116 lines
3.4 KiB
Rust
use nu_cmd_base::input_handler::{operate, CmdArgument};
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::{Call, CellPath},
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Config, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
pub struct Arguments {
|
|
cell_paths: Option<Vec<CellPath>>,
|
|
config: Config,
|
|
}
|
|
|
|
impl CmdArgument for Arguments {
|
|
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
|
|
self.cell_paths.take()
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct SubCommand;
|
|
|
|
impl Command for SubCommand {
|
|
fn name(&self) -> &str {
|
|
"ansi strip"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("ansi strip")
|
|
.input_output_types(vec![
|
|
(Type::String, Type::String),
|
|
(Type::List(Box::new(Type::String)), Type::List(Box::new(Type::String))),
|
|
(Type::Table(vec![]), Type::Table(vec![])),
|
|
(Type::Record(vec![]), Type::Record(vec![])),
|
|
])
|
|
.rest(
|
|
"cell path",
|
|
SyntaxShape::CellPath,
|
|
"For a data structure input, remove ANSI sequences from strings at the given cell paths.",
|
|
)
|
|
.allow_variants_without_examples(true)
|
|
.category(Category::Platform)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Strip ANSI escape sequences from a string."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
|
|
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
|
|
let config = engine_state.get_config();
|
|
let args = Arguments {
|
|
cell_paths,
|
|
config: config.clone(),
|
|
};
|
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Strip ANSI escape sequences from a string",
|
|
example: r#"$'(ansi green)(ansi cursor_on)hello' | ansi strip"#,
|
|
result: Some(Value::test_string("hello")),
|
|
}]
|
|
}
|
|
}
|
|
|
|
fn action(input: &Value, args: &Arguments, _span: Span) -> Value {
|
|
let span = input.span();
|
|
match input {
|
|
Value::String { val, .. } => {
|
|
Value::string(nu_utils::strip_ansi_likely(val).to_string(), span)
|
|
}
|
|
other => {
|
|
// Fake stripping ansi for other types and just show the abbreviated string
|
|
// instead of showing an error message
|
|
Value::string(other.to_abbreviated_string(&args.config), span)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{action, Arguments, SubCommand};
|
|
use nu_protocol::{engine::EngineState, Span, Value};
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(SubCommand {})
|
|
}
|
|
|
|
#[test]
|
|
fn test_stripping() {
|
|
let input_string =
|
|
Value::test_string("\u{1b}[3;93;41mHello\u{1b}[0m \u{1b}[1;32mNu \u{1b}[1;35mWorld");
|
|
let expected = Value::test_string("Hello Nu World");
|
|
|
|
let args = Arguments {
|
|
cell_paths: vec![].into(),
|
|
config: EngineState::new().get_config().clone(),
|
|
};
|
|
|
|
let actual = action(&input_string, &args, Span::test_data());
|
|
assert_eq!(actual, expected);
|
|
}
|
|
}
|