forked from extern/nushell
Make ls show only the file name (#1276)
* Make ls show only the file name * Refactor and remove unwraps * Put functionality in separate flag
This commit is contained in:
parent
583f27dc41
commit
4429a75e17
@ -11,6 +11,8 @@ pub struct Ls;
|
||||
pub struct LsArgs {
|
||||
pub path: Option<Tagged<PathBuf>>,
|
||||
pub full: bool,
|
||||
#[serde(rename = "short-names")]
|
||||
pub short_names: bool,
|
||||
}
|
||||
|
||||
impl PerItemCommand for Ls {
|
||||
@ -26,6 +28,7 @@ impl PerItemCommand for Ls {
|
||||
"a path to get the directory contents from",
|
||||
)
|
||||
.switch("full", "list all available columns for each entry")
|
||||
.switch("short-names", "only print the file names and not the path")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
|
@ -88,7 +88,11 @@ impl Shell for FilesystemShell {
|
||||
|
||||
fn ls(
|
||||
&self,
|
||||
LsArgs { path, full }: LsArgs,
|
||||
LsArgs {
|
||||
path,
|
||||
full,
|
||||
short_names,
|
||||
}: LsArgs,
|
||||
context: &RunnablePerItemContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let cwd = self.path();
|
||||
@ -144,12 +148,24 @@ impl Shell for FilesystemShell {
|
||||
if let Ok(entry) = entry {
|
||||
let filepath = entry.path();
|
||||
if let Ok(metadata) = std::fs::symlink_metadata(&filepath) {
|
||||
let filename = if let Ok(fname) = filepath.strip_prefix(&cwd) {
|
||||
let mut filename = if let Ok(fname) = filepath.strip_prefix(&cwd) {
|
||||
fname
|
||||
} else {
|
||||
Path::new(&filepath)
|
||||
};
|
||||
|
||||
if short_names {
|
||||
filename = if let Some(fname) = filename.file_name() {
|
||||
Path::new(fname)
|
||||
} else {
|
||||
let fname = filename
|
||||
.file_name()
|
||||
.or_else(|| Path::new(filename).file_name())
|
||||
.unwrap_or(filename.as_os_str());
|
||||
Path::new(fname)
|
||||
}
|
||||
}
|
||||
|
||||
let value = dir_entry_dict(filename, &metadata, &name_tag, full)?;
|
||||
yield ReturnSuccess::value(value);
|
||||
}
|
||||
@ -183,12 +199,24 @@ impl Shell for FilesystemShell {
|
||||
}
|
||||
if let Ok(entry) = entry {
|
||||
if let Ok(metadata) = std::fs::symlink_metadata(&entry) {
|
||||
let filename = if let Ok(fname) = entry.strip_prefix(&cwd) {
|
||||
let mut filename = if let Ok(fname) = entry.strip_prefix(&cwd) {
|
||||
fname
|
||||
} else {
|
||||
Path::new(&entry)
|
||||
Path::new(&entry)
|
||||
};
|
||||
|
||||
if short_names {
|
||||
filename = if let Some(fname) = filename.file_name() {
|
||||
Path::new(fname)
|
||||
} else {
|
||||
let fname = filename
|
||||
.file_name()
|
||||
.or_else(|| Path::new(filename).file_name())
|
||||
.unwrap_or(filename.as_os_str());
|
||||
Path::new(fname)
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(value) = dir_entry_dict(filename, &metadata, &name_tag, full) {
|
||||
yield ReturnSuccess::value(value);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user