diff --git a/crates/nu-command/src/filesystem/save.rs b/crates/nu-command/src/filesystem/save.rs index 48d107487..8f7dfcfa3 100644 --- a/crates/nu-command/src/filesystem/save.rs +++ b/crates/nu-command/src/filesystem/save.rs @@ -105,6 +105,20 @@ impl Command for Save { Ok(PipelineData::new(span)) } + Value::List { vals, .. } => { + let val = vals + .into_iter() + .map(|it| it.as_string()) + .collect::, ShellError>>()? + .join("\n") + + "\n"; + + if let Err(err) = file.write_all(val.as_bytes()) { + return Err(ShellError::IOError(err.to_string())); + } + + Ok(PipelineData::new(span)) + } v => Err(ShellError::UnsupportedInput( format!("{:?} not supported", v.get_type()), span, @@ -126,6 +140,20 @@ impl Command for Save { Ok(PipelineData::new(span)) } + Value::List { vals, .. } => { + let val = vals + .into_iter() + .map(|it| it.as_string()) + .collect::, ShellError>>()? + .join("\n") + + "\n"; + + if let Err(err) = file.write_all(val.as_bytes()) { + return Err(ShellError::IOError(err.to_string())); + } + + Ok(PipelineData::new(span)) + } v => Err(ShellError::UnsupportedInput( format!("{:?} not supported", v.get_type()), span, diff --git a/crates/nu-command/tests/commands/save.rs b/crates/nu-command/tests/commands/save.rs index 95f82c31c..2d3f8790e 100644 --- a/crates/nu-command/tests/commands/save.rs +++ b/crates/nu-command/tests/commands/save.rs @@ -49,6 +49,24 @@ fn writes_out_csv() { }) } +#[test] +fn writes_out_list() { + Playground::setup("save_test_3", |dirs, sandbox| { + sandbox.with_files(vec![]); + + let expected_file = dirs.test().join("list_sample.txt"); + + nu!( + cwd: dirs.root(), + r#"echo [a b c d] | save save_test_3/list_sample.txt"#, + ); + + let actual = file_contents(expected_file); + println!("{actual}"); + assert_eq!(actual, "a\nb\nc\nd\n") + }) +} + #[test] fn save_append_will_create_file_if_not_exists() { Playground::setup("save_test_3", |dirs, sandbox| {