From daec3fc3d322233ba4369ab9b2cac57352130bee Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Mon, 5 Sep 2022 14:32:09 +0800 Subject: [PATCH] let path split keeps 'C:\' together (#6485) * `path split` keeps 'C:\' together * fmt * fix clippt * fix match arm --- crates/nu-command/src/path/split.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/crates/nu-command/src/path/split.rs b/crates/nu-command/src/path/split.rs index dc98a3f8ba..dc223f12a2 100644 --- a/crates/nu-command/src/path/split.rs +++ b/crates/nu-command/src/path/split.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Component, Path}; use nu_engine::CallExt; use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value}; @@ -62,8 +62,7 @@ impl Command for SubCommand { example: r"'C:\Users\viking\spam.txt' | path split", result: Some(Value::List { vals: vec![ - Value::test_string("C:"), - Value::test_string(r"\"), + Value::test_string(r"C:\"), Value::test_string("Users"), Value::test_string("viking"), Value::test_string("spam.txt"), @@ -108,15 +107,33 @@ fn split(path: &Path, span: Span, _: &Arguments) -> Value { Value::List { vals: path .components() - .map(|comp| { - let s = comp.as_os_str().to_string_lossy(); - Value::string(s, span) + .filter_map(|comp| { + let comp = process_component(comp); + comp.map(|s| Value::string(s, span)) }) .collect(), span, } } +#[cfg(windows)] +fn process_component(comp: Component) -> Option { + match comp { + Component::RootDir => None, + Component::Prefix(_) => { + let mut s = comp.as_os_str().to_string_lossy().to_string(); + s.push('\\'); + Some(s) + } + comp => Some(comp.as_os_str().to_string_lossy().to_string()), + } +} + +#[cfg(not(windows))] +fn process_component(comp: Component) -> Option { + Some(comp.as_os_str().to_string_lossy().to_string()) +} + #[cfg(test)] mod tests { use super::*;