From 3b809b38e820eb06c6c8994568a3416a41262dda Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Thu, 4 Aug 2022 19:59:20 +0800 Subject: [PATCH] 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 --- crates/nu-command/src/filesystem/cd.rs | 14 +++++++++++++ crates/nu-command/src/filesystem/cp.rs | 9 +++++++++ crates/nu-command/src/filesystem/ls.rs | 14 +++++++++++++ crates/nu-command/src/filesystem/mv.rs | 9 +++++++++ crates/nu-command/src/filesystem/open.rs | 14 +++++++++++++ crates/nu-command/src/filesystem/rm.rs | 14 ++++++++++++- crates/nu-command/tests/commands/cp.rs | 13 ++++++++++++ crates/nu-command/tests/commands/ls.rs | 21 ++++++++++++++++++++ crates/nu-command/tests/commands/move_/mv.rs | 15 ++++++++++++++ crates/nu-command/tests/commands/open.rs | 16 +++++++++++++++ crates/nu-command/tests/commands/rm.rs | 13 ++++++++++++ 11 files changed, 151 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs index 2d147ea081..35a89ccfe6 100644 --- a/crates/nu-command/src/filesystem/cd.rs +++ b/crates/nu-command/src/filesystem/cd.rs @@ -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 == "-" { diff --git a/crates/nu-command/src/filesystem/cp.rs b/crates/nu-command/src/filesystem/cp.rs index 92b0b5c940..03a799cbf9 100644 --- a/crates/nu-command/src/filesystem/cp.rs +++ b/crates/nu-command/src/filesystem/cp.rs @@ -72,6 +72,15 @@ impl Command for Cp { _input: PipelineData, ) -> Result { let src: Spanned = 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 = call.req(engine_state, stack, 1)?; let recursive = call.has_flag("recursive"); let verbose = call.has_flag("verbose"); diff --git a/crates/nu-command/src/filesystem/ls.rs b/crates/nu-command/src/filesystem/ls.rs index 022970778f..a885da6625 100644 --- a/crates/nu-command/src/filesystem/ls.rs +++ b/crates/nu-command/src/filesystem/ls.rs @@ -83,6 +83,20 @@ impl Command for Ls { let pattern_arg: Option> = 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; diff --git a/crates/nu-command/src/filesystem/mv.rs b/crates/nu-command/src/filesystem/mv.rs index 3c0a3e1080..743d54f279 100644 --- a/crates/nu-command/src/filesystem/mv.rs +++ b/crates/nu-command/src/filesystem/mv.rs @@ -65,6 +65,15 @@ impl Command for Mv { ) -> Result { // TODO: handle invalid directory or insufficient permissions when moving let spanned_source: Spanned = 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 = call.req(engine_state, stack, 1)?; let verbose = call.has_flag("verbose"); let interactive = call.has_flag("interactive"); diff --git a/crates/nu-command/src/filesystem/open.rs b/crates/nu-command/src/filesystem/open.rs index 3bdc910873..44feeb62ec 100644 --- a/crates/nu-command/src/filesystem/open.rs +++ b/crates/nu-command/src/filesystem/open.rs @@ -50,6 +50,20 @@ impl Command for Open { let ctrlc = engine_state.ctrlc.clone(); let path = call.opt::>(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 { diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index d7dc6db84d..d023d03deb 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -139,7 +139,19 @@ fn rm( let ctrlc = engine_state.ctrlc.clone(); - let targets: Vec> = call.rest(engine_state, stack, 0)?; + let mut targets: Vec> = 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(); diff --git a/crates/nu-command/tests/commands/cp.rs b/crates/nu-command/tests/commands/cp.rs index b12ec132d5..d81a862361 100644 --- a/crates/nu-command/tests/commands/cp.rs +++ b/crates/nu-command/tests/commands/cp.rs @@ -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"); + }); +} diff --git a/crates/nu-command/tests/commands/ls.rs b/crates/nu-command/tests/commands/ls.rs index f699d85341..2cdf5078e7 100644 --- a/crates/nu-command/tests/commands/ls.rs +++ b/crates/nu-command/tests/commands/ls.rs @@ -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()); + }) +} diff --git a/crates/nu-command/tests/commands/move_/mv.rs b/crates/nu-command/tests/commands/move_/mv.rs index 84f29ff366..c08ccbd472 100644 --- a/crates/nu-command/tests/commands/move_/mv.rs +++ b/crates/nu-command/tests/commands/move_/mv.rs @@ -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"); + }) +} diff --git a/crates/nu-command/tests/commands/open.rs b/crates/nu-command/tests/commands/open.rs index 726adfe316..a6996da052 100644 --- a/crates/nu-command/tests/commands/open.rs +++ b/crates/nu-command/tests/commands/open.rs @@ -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()); + }) +} diff --git a/crates/nu-command/tests/commands/rm.rs b/crates/nu-command/tests/commands/rm.rs index d40424f70c..0a3d7d6e3e 100644 --- a/crates/nu-command/tests/commands/rm.rs +++ b/crates/nu-command/tests/commands/rm.rs @@ -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()); + }); +}