forked from extern/nushell
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:
parent
7c49a42b68
commit
3b809b38e8
@ -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 == "-" {
|
||||
|
@ -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");
|
||||
|
@ -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;
|
||||
|
@ -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");
|
||||
|
@ -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 {
|
||||
|
@ -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();
|
||||
|
@ -331,3 +331,16 @@ fn copy_identical_file() {
|
||||
assert!(actual.err.contains("Copy aborted"));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_ignores_ansi() {
|
||||
Playground::setup("cp_test_16", |_dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("test.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: sandbox.cwd(),
|
||||
"ls | find test | get name | cp $in.0 success.txt; ls | find success | get name | ansi strip | get 0",
|
||||
);
|
||||
assert_eq!(actual.out, "success.txt");
|
||||
});
|
||||
}
|
||||
|
@ -538,3 +538,24 @@ fn list_directory_contains_invalid_utf8() {
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_ignores_ansi() {
|
||||
Playground::setup("ls_test_ansi", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("los.txt"),
|
||||
EmptyFile("tres.txt"),
|
||||
EmptyFile("amigos.txt"),
|
||||
EmptyFile("arepas.clu"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls | find .txt | each { ls $in.name }
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
})
|
||||
}
|
||||
|
@ -360,3 +360,18 @@ fn does_not_error_when_some_file_is_moving_into_itself() {
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mv_ignores_ansi() {
|
||||
Playground::setup("mv_test_ansi", |_dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("test.txt")]);
|
||||
let actual = nu!(
|
||||
cwd: sandbox.cwd(),
|
||||
r#"
|
||||
ls | find test | mv $in.0.name success.txt; ls | $in.0.name
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(actual.out, "success.txt");
|
||||
})
|
||||
}
|
||||
|
@ -267,3 +267,19 @@ fn test_open_block_command() {
|
||||
|
||||
assert_eq!(actual.out, "abcd")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_ignore_ansi() {
|
||||
Playground::setup("open_test_ansi", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("nu.zion.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls | find nu.zion | get 0 | get name | open $in
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
})
|
||||
}
|
||||
|
@ -324,3 +324,16 @@ fn removes_files_with_case_sensitive_glob_matches_by_default() {
|
||||
assert!(skipped_path.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_ignores_ansi() {
|
||||
Playground::setup("rm_test_ansi", |_dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("test.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: sandbox.cwd(),
|
||||
"ls | find test | get name | rm $in.0; ls",
|
||||
);
|
||||
assert!(actual.out.is_empty());
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user