make cd, cp, ls, mv, open and rm automatically strip ansi codes (#6220)

* make `cd`, `cp`, `ls`, `mv`, `open` and `rm` automatically strip ansi escape code

* fix nu-cli test

* fix nu-cli test 2

* fix nu-cli test 3

* remove `include-ansi` arg

* fix test
This commit is contained in:
pwygab
2022-08-04 19:59:20 +08:00
committed by GitHub
parent 7c49a42b68
commit 3b809b38e8
11 changed files with 151 additions and 1 deletions

View File

@ -40,6 +40,20 @@ impl Command for Cd {
let config = engine_state.get_config();
let use_abbrev = config.cd_with_abbreviations;
let path_val = {
if let Some(path) = path_val {
Some(Spanned {
item: match strip_ansi_escapes::strip(&path.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path.item),
Err(_) => path.item,
},
span: path.span,
})
} else {
path_val
}
};
let (path, span) = match path_val {
Some(v) => {
if v.item == "-" {

View File

@ -72,6 +72,15 @@ impl Command for Cp {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let src: Spanned<String> = call.req(engine_state, stack, 0)?;
let src = {
Spanned {
item: match strip_ansi_escapes::strip(&src.item) {
Ok(item) => String::from_utf8(item).unwrap_or(src.item),
Err(_) => src.item,
},
span: src.span,
}
};
let dst: Spanned<String> = call.req(engine_state, stack, 1)?;
let recursive = call.has_flag("recursive");
let verbose = call.has_flag("verbose");

View File

@ -83,6 +83,20 @@ impl Command for Ls {
let pattern_arg: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
let pattern_arg = {
if let Some(path) = pattern_arg {
Some(Spanned {
item: match strip_ansi_escapes::strip(&path.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path.item),
Err(_) => path.item,
},
span: path.span,
})
} else {
pattern_arg
}
};
let (path, p_tag, absolute_path) = match pattern_arg {
Some(p) => {
let p_tag = p.span;

View File

@ -65,6 +65,15 @@ impl Command for Mv {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
// TODO: handle invalid directory or insufficient permissions when moving
let spanned_source: Spanned<String> = call.req(engine_state, stack, 0)?;
let spanned_source = {
Spanned {
item: match strip_ansi_escapes::strip(&spanned_source.item) {
Ok(item) => String::from_utf8(item).unwrap_or(spanned_source.item),
Err(_) => spanned_source.item,
},
span: spanned_source.span,
}
};
let spanned_destination: Spanned<String> = call.req(engine_state, stack, 1)?;
let verbose = call.has_flag("verbose");
let interactive = call.has_flag("interactive");

View File

@ -50,6 +50,20 @@ impl Command for Open {
let ctrlc = engine_state.ctrlc.clone();
let path = call.opt::<Spanned<String>>(engine_state, stack, 0)?;
let path = {
if let Some(path_val) = path {
Some(Spanned {
item: match strip_ansi_escapes::strip(&path_val.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path_val.item),
Err(_) => path_val.item,
},
span: path_val.span,
})
} else {
path
}
};
let path = if let Some(path) = path {
path
} else {

View File

@ -139,7 +139,19 @@ fn rm(
let ctrlc = engine_state.ctrlc.clone();
let targets: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
let mut targets: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
for (idx, path) in targets.clone().into_iter().enumerate() {
let corrected_path = Spanned {
item: match strip_ansi_escapes::strip(&path.item) {
Ok(item) => String::from_utf8(item).unwrap_or(path.item),
Err(_) => path.item,
},
span: path.span,
};
let _ = std::mem::replace(&mut targets[idx], corrected_path);
}
let span = call.head;
let config = engine_state.get_config();