Rename the Path and Pattern primitives (#2889)

* Rename the Path primitive to FilePath

* Rename glob pattern also

* more fun

* Fix the Windows path methods
This commit is contained in:
Jonathan Turner
2021-01-08 20:30:41 +13:00
committed by GitHub
parent 2dcb16870b
commit 0e13d9fbaa
48 changed files with 146 additions and 131 deletions

View File

@ -134,7 +134,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
out!("{}", s);
}
Value {
value: UntaggedValue::Primitive(Primitive::Path(s)),
value: UntaggedValue::Primitive(Primitive::FilePath(s)),
..
} => {
out!("{}", s.display());

View File

@ -23,7 +23,7 @@ impl WholeStreamCommand for Cd {
fn signature(&self) -> Signature {
Signature::build("cd").optional(
"directory",
SyntaxShape::Path,
SyntaxShape::FilePath,
"the directory to change to",
)
}

View File

@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand {
fn signature(&self) -> Signature {
Signature::build("config load").required(
"load",
SyntaxShape::Path,
SyntaxShape::FilePath,
"Path to load the config from",
)
}

View File

@ -36,6 +36,6 @@ pub async fn path(args: CommandArgs) -> Result<OutputStream, ShellError> {
let path = config::default_path()?;
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::Primitive(Primitive::Path(path)).into_value(args.call_info.name_tag),
UntaggedValue::Primitive(Primitive::FilePath(path)).into_value(args.call_info.name_tag),
)))
}

View File

@ -22,8 +22,8 @@ impl WholeStreamCommand for Cpy {
fn signature(&self) -> Signature {
Signature::build("cp")
.required("src", SyntaxShape::Pattern, "the place to copy from")
.required("dst", SyntaxShape::Path, "the place to copy to")
.required("src", SyntaxShape::GlobPattern, "the place to copy from")
.required("dst", SyntaxShape::FilePath, "the place to copy to")
.switch(
"recursive",
"copy recursively through subdirectories",

View File

@ -38,7 +38,7 @@ impl WholeStreamCommand for Du {
fn signature(&self) -> Signature {
Signature::build(NAME)
.optional("path", SyntaxShape::Pattern, "starting directory")
.optional("path", SyntaxShape::GlobPattern, "starting directory")
.switch(
"all",
"Output file sizes as well as directory sizes",
@ -51,7 +51,7 @@ impl WholeStreamCommand for Du {
)
.named(
"exclude",
SyntaxShape::Pattern,
SyntaxShape::GlobPattern,
"Exclude these file names",
Some('x'),
)
@ -347,7 +347,7 @@ impl From<DirInfo> for Value {
r.insert(
"path".to_string(),
UntaggedValue::path(d.path).into_value(&d.tag),
UntaggedValue::filepath(d.path).into_value(&d.tag),
);
r.insert(
@ -389,7 +389,7 @@ impl From<FileInfo> for Value {
r.insert(
"path".to_string(),
UntaggedValue::path(f.path).into_value(&f.tag),
UntaggedValue::filepath(f.path).into_value(&f.tag),
);
r.insert(

View File

@ -27,7 +27,7 @@ impl WholeStreamCommand for Enter {
Signature::build("enter")
.required(
"location",
SyntaxShape::Path,
SyntaxShape::FilePath,
"the location to create a new shell from",
)
.named(

View File

@ -21,8 +21,11 @@ impl WholeStreamCommand for Exec {
fn signature(&self) -> Signature {
Signature::build("exec")
.required("command", SyntaxShape::Path, "the command to execute")
.rest(SyntaxShape::Pattern, "any additional arguments for command")
.required("command", SyntaxShape::FilePath, "the command to execute")
.rest(
SyntaxShape::GlobPattern,
"any additional arguments for command",
)
}
fn usage(&self) -> &str {

View File

@ -28,7 +28,7 @@ impl WholeStreamCommand for Ls {
Signature::build("ls")
.optional(
"path",
SyntaxShape::Pattern,
SyntaxShape::GlobPattern,
"a path to get the directory contents from",
)
.switch("all", "Show hidden files", Some('a'))

View File

@ -22,7 +22,10 @@ impl WholeStreamCommand for Mkdir {
fn signature(&self) -> Signature {
Signature::build("mkdir")
.rest(SyntaxShape::Path, "the name(s) of the path(s) to create")
.rest(
SyntaxShape::FilePath,
"the name(s) of the path(s) to create",
)
.switch("show-created-paths", "show the path(s) created.", Some('s'))
}

View File

@ -23,12 +23,12 @@ impl WholeStreamCommand for Mv {
Signature::build("mv")
.required(
"source",
SyntaxShape::Pattern,
SyntaxShape::GlobPattern,
"the location to move files/directories from",
)
.required(
"destination",
SyntaxShape::Path,
SyntaxShape::FilePath,
"the location to move files/directories to",
)
}

View File

@ -28,7 +28,7 @@ impl WholeStreamCommand for SubCommand {
fn signature(&self) -> Signature {
Signature::build("nu plugin").named(
"load",
SyntaxShape::Path,
SyntaxShape::FilePath,
"a path to load the plugins from",
Some('l'),
)

View File

@ -29,7 +29,7 @@ impl WholeStreamCommand for Open {
Signature::build(self.name())
.required(
"path",
SyntaxShape::Path,
SyntaxShape::FilePath,
"the file path to load values from",
)
.switch(

View File

@ -59,7 +59,7 @@ impl WholeStreamCommand for PathBasename {
Example {
description: "Replace basename of a path",
example: "echo 'C:\\Users\\joe\\test.txt' | path basename -r 'spam.png'",
result: Some(vec![Value::from(UntaggedValue::path(
result: Some(vec![Value::from(UntaggedValue::filepath(
"C:\\Users\\joe\\spam.png",
))]),
},
@ -77,7 +77,9 @@ impl WholeStreamCommand for PathBasename {
Example {
description: "Replace basename of a path",
example: "echo '/home/joe/test.txt' | path basename -r 'spam.png'",
result: Some(vec![Value::from(UntaggedValue::path("/home/joe/spam.png"))]),
result: Some(vec![Value::from(UntaggedValue::filepath(
"/home/joe/spam.png",
))]),
},
]
}
@ -85,7 +87,7 @@ impl WholeStreamCommand for PathBasename {
fn action(path: &Path, args: Arc<DefaultArguments>) -> UntaggedValue {
match args.replace {
Some(ref basename) => UntaggedValue::path(path.with_file_name(basename)),
Some(ref basename) => UntaggedValue::filepath(path.with_file_name(basename)),
None => UntaggedValue::string(match path.file_name() {
Some(filename) => filename.to_string_lossy(),
None => "".into(),

View File

@ -69,20 +69,20 @@ impl WholeStreamCommand for PathDirname {
Example {
description: "Get dirname of a path",
example: "echo 'C:\\Users\\joe\\code\\test.txt' | path dirname",
result: Some(vec![Value::from(UntaggedValue::path(
result: Some(vec![Value::from(UntaggedValue::filepath(
"C:\\Users\\joe\\code",
))]),
},
Example {
description: "Set how many levels up to skip",
example: "echo 'C:\\Users\\joe\\code\\test.txt' | path dirname -n 2",
result: Some(vec![Value::from(UntaggedValue::path("C:\\Users\\joe"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("C:\\Users\\joe"))]),
},
Example {
description: "Replace the part that would be returned with custom string",
example:
"echo 'C:\\Users\\joe\\code\\test.txt' | path dirname -n 2 -r C:\\Users\\viking",
result: Some(vec![Value::from(UntaggedValue::path(
result: Some(vec![Value::from(UntaggedValue::filepath(
"C:\\Users\\viking\\code\\test.txt",
))]),
},
@ -95,17 +95,17 @@ impl WholeStreamCommand for PathDirname {
Example {
description: "Get dirname of a path",
example: "echo '/home/joe/code/test.txt' | path dirname",
result: Some(vec![Value::from(UntaggedValue::path("/home/joe/code"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("/home/joe/code"))]),
},
Example {
description: "Set how many levels up to skip",
example: "echo '/home/joe/code/test.txt' | path dirname -n 2",
result: Some(vec![Value::from(UntaggedValue::path("/home/joe"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("/home/joe"))]),
},
Example {
description: "Replace the part that would be returned with custom string",
example: "echo '/home/joe/code/test.txt' | path dirname -n 2 -r /home/viking",
result: Some(vec![Value::from(UntaggedValue::path(
result: Some(vec![Value::from(UntaggedValue::filepath(
"/home/viking/code/test.txt",
))]),
},
@ -132,12 +132,12 @@ fn action(path: &Path, args: Arc<DefaultArguments>) -> UntaggedValue {
Some(ref newdir) => {
let remainder = path.strip_prefix(dirname).unwrap_or(dirname);
if !remainder.as_os_str().is_empty() {
UntaggedValue::path(Path::new(newdir).join(remainder))
UntaggedValue::filepath(Path::new(newdir).join(remainder))
} else {
UntaggedValue::path(Path::new(newdir))
UntaggedValue::filepath(Path::new(newdir))
}
}
None => UntaggedValue::path(dirname),
None => UntaggedValue::filepath(dirname),
}
}

View File

@ -65,7 +65,7 @@ fn action(path: &Path, _args: Arc<DefaultArguments>) -> UntaggedValue {
let ps = path.to_string_lossy();
let expanded = shellexpand::tilde(&ps);
let path: &Path = expanded.as_ref().as_ref();
UntaggedValue::path(dunce::canonicalize(path).unwrap_or_else(|_| PathBuf::from(path)))
UntaggedValue::filepath(dunce::canonicalize(path).unwrap_or_else(|_| PathBuf::from(path)))
}
#[cfg(test)]

View File

@ -63,12 +63,12 @@ impl WholeStreamCommand for PathExtension {
Example {
description: "Replace an extension with a custom string",
example: "echo 'test.txt' | path extension -r md",
result: Some(vec![Value::from(UntaggedValue::path("test.md"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("test.md"))]),
},
Example {
description: "To replace more complex extensions:",
example: "echo 'test.tar.gz' | path extension -r '' | path extension -r txt",
result: Some(vec![Value::from(UntaggedValue::path("test.txt"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("test.txt"))]),
},
]
}
@ -76,7 +76,7 @@ impl WholeStreamCommand for PathExtension {
fn action(path: &Path, args: Arc<DefaultArguments>) -> UntaggedValue {
match args.replace {
Some(ref extension) => UntaggedValue::path(path.with_extension(extension)),
Some(ref extension) => UntaggedValue::filepath(path.with_extension(extension)),
None => UntaggedValue::string(match path.extension() {
Some(extension) => extension.to_string_lossy(),
None => "".into(),

View File

@ -86,7 +86,7 @@ impl WholeStreamCommand for PathFilestem {
Example {
description: "Replace the filestem that would be returned",
example: "echo 'C:\\Users\\joe\\bacon_lettuce.egg.gz' | path filestem -p bacon_ -s .egg.gz -r spam",
result: Some(vec![Value::from(UntaggedValue::path("C:\\Users\\joe\\bacon_spam.egg.gz"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("C:\\Users\\joe\\bacon_spam.egg.gz"))]),
},
]
}
@ -107,7 +107,7 @@ impl WholeStreamCommand for PathFilestem {
Example {
description: "Replace the filestem that would be returned",
example: "echo '/home/joe/bacon_lettuce.egg.gz' | path filestem -p bacon_ -s .egg.gz -r spam",
result: Some(vec![Value::from(UntaggedValue::path("/home/joe/bacon_spam.egg.gz"))]),
result: Some(vec![Value::from(UntaggedValue::filepath("/home/joe/bacon_spam.egg.gz"))]),
},
]
}
@ -152,7 +152,7 @@ fn action(path: &Path, args: Arc<DefaultArguments>) -> UntaggedValue {
match args.replace {
Some(ref replace) => {
let new_name = prefix + replace + &suffix;
UntaggedValue::path(path.with_file_name(&new_name))
UntaggedValue::filepath(path.with_file_name(&new_name))
}
None => UntaggedValue::string(stem),
}

View File

@ -46,7 +46,7 @@ where
F: Fn(&Path, Arc<DefaultArguments>) -> UntaggedValue + Send + 'static,
{
let v = match &v.value {
UntaggedValue::Primitive(Primitive::Path(buf)) => action(buf, args).into_value(v.tag()),
UntaggedValue::Primitive(Primitive::FilePath(buf)) => action(buf, args).into_value(v.tag()),
UntaggedValue::Primitive(Primitive::String(s)) => {
action(s.as_ref(), args).into_value(v.tag())
}

View File

@ -38,7 +38,7 @@ impl WholeStreamCommand for Remove {
)
.switch("recursive", "delete subdirectories recursively", Some('r'))
.switch("force", "suppress error when no file", Some('f'))
.rest(SyntaxShape::Pattern, "the file path(s) to remove")
.rest(SyntaxShape::GlobPattern, "the file path(s) to remove")
}
fn usage(&self) -> &str {

View File

@ -140,7 +140,11 @@ impl WholeStreamCommand for Save {
fn signature(&self) -> Signature {
Signature::build("save")
.optional("path", SyntaxShape::Path, "the path to save contents to")
.optional(
"path",
SyntaxShape::FilePath,
"the path to save contents to",
)
.switch(
"raw",
"treat values as-is rather than auto-converting based on file extension",

View File

@ -112,8 +112,8 @@ pub fn clone_tagged_value(v: &Value) -> Value {
UntaggedValue::Primitive(Primitive::Int(i)) => {
UntaggedValue::Primitive(Primitive::Int(i.clone()))
}
UntaggedValue::Primitive(Primitive::Path(x)) => {
UntaggedValue::Primitive(Primitive::Path(x.clone()))
UntaggedValue::Primitive(Primitive::FilePath(x)) => {
UntaggedValue::Primitive(Primitive::FilePath(x.clone()))
}
UntaggedValue::Primitive(Primitive::Filesize(b)) => {
UntaggedValue::Primitive(Primitive::Filesize(*b))
@ -136,7 +136,7 @@ fn to_string_tagged_value(v: &Value) -> Result<String, ShellError> {
| UntaggedValue::Primitive(Primitive::Filesize(_))
| UntaggedValue::Primitive(Primitive::Boolean(_))
| UntaggedValue::Primitive(Primitive::Decimal(_))
| UntaggedValue::Primitive(Primitive::Path(_))
| UntaggedValue::Primitive(Primitive::FilePath(_))
| UntaggedValue::Primitive(Primitive::Int(_)) => as_string(v),
UntaggedValue::Primitive(Primitive::Date(d)) => Ok(d.to_string()),
UntaggedValue::Primitive(Primitive::Nothing) => Ok(String::new()),

View File

@ -96,7 +96,7 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
)?))
}
UntaggedValue::Primitive(Primitive::Nothing) => serde_json::Value::Null,
UntaggedValue::Primitive(Primitive::Pattern(s)) => serde_json::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::GlobPattern(s)) => serde_json::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::String(s)) => serde_json::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => serde_json::Value::Array(
path.iter()
@ -113,7 +113,7 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
})
.collect::<Result<Vec<serde_json::Value>, ShellError>>()?,
),
UntaggedValue::Primitive(Primitive::Path(s)) => {
UntaggedValue::Primitive(Primitive::FilePath(s)) => {
serde_json::Value::String(s.display().to_string())
}

View File

@ -58,9 +58,9 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
UntaggedValue::Primitive(Primitive::Nothing) => {
toml::Value::String("<Nothing>".to_string())
}
UntaggedValue::Primitive(Primitive::Pattern(s)) => toml::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::GlobPattern(s)) => toml::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::String(s)) => toml::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::Path(s)) => {
UntaggedValue::Primitive(Primitive::FilePath(s)) => {
toml::Value::String(s.display().to_string())
}
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => toml::Value::Array(

View File

@ -58,7 +58,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
)?))
}
UntaggedValue::Primitive(Primitive::Nothing) => serde_yaml::Value::Null,
UntaggedValue::Primitive(Primitive::Pattern(s)) => serde_yaml::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::GlobPattern(s)) => serde_yaml::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::String(s)) => serde_yaml::Value::String(s.clone()),
UntaggedValue::Primitive(Primitive::ColumnPath(path)) => {
let mut out = vec![];
@ -79,7 +79,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
serde_yaml::Value::Sequence(out)
}
UntaggedValue::Primitive(Primitive::Path(s)) => {
UntaggedValue::Primitive(Primitive::FilePath(s)) => {
serde_yaml::Value::String(s.display().to_string())
}

View File

@ -23,10 +23,10 @@ impl WholeStreamCommand for Touch {
Signature::build("touch")
.required(
"filename",
SyntaxShape::Path,
SyntaxShape::FilePath,
"the path of the file you want to create",
)
.rest(SyntaxShape::Path, "additional files to create")
.rest(SyntaxShape::FilePath, "additional files to create")
}
fn usage(&self) -> &str {
"creates one or more files"

View File

@ -60,7 +60,7 @@ macro_rules! entry_path {
($arg:expr, $path:expr, $tag:expr) => {
entry(
$arg.clone(),
UntaggedValue::Primitive(Primitive::Path($path)).into_value($tag.clone()),
UntaggedValue::Primitive(Primitive::FilePath($path)).into_value($tag.clone()),
false,
$tag,
)