du: add -l/--long flag, remove -a/--all flag (#14407)

# Description
Closes:  #14387 
~To make it happen, just need to added `-l` flag to `du`, and pass it to
`DirBuilder`, `DirInfo`, `FileInfo`
Then tweak `impl From<DirInfo> for Value` and `impl From<FileInfo> for
Value` impl.~

---

Edit: this PR is going to:
1. Exclude directories and files columns by default
2. Added `-l/--long` flag to output directories and files columns
3. When running `du`, it will output the files as well. Previously it
doesn't output the size of file.

To make it happen, just need to added `-r` flag to `du`, and pass it to
`DirBuilder`, `DirInfo`, `FileInfo`
Then tweak `impl From<DirInfo> for Value` and `impl From<FileInfo> for
Value` impl.

And rename some variables.

# User-Facing Changes
`du` is no longer output `directories` and `file` columns by default,
added `-r` flag will show `directories` column, `-f` flag will show
`files` column.

```nushell
> du nushell
╭───┬────────────────────────────────────┬──────────┬──────────╮
│ # │                path                │ apparent │ physical │
├───┼────────────────────────────────────┼──────────┼──────────┤
│ 0 │ /home/windsoilder/projects/nushell │ 34.6 GiB │ 34.7 GiB │
├───┼────────────────────────────────────┼──────────┼──────────┤
│ # │                path                │ apparent │ physical │
╰───┴────────────────────────────────────┴──────────┴──────────╯
> du nushell --recursive --files # It outputs two more columns, `directories` and `files`, but the output is too long to paste here.
```
# Tests + Formatting
Added 1 test

# After Submitting
NaN
This commit is contained in:
Wind
2024-12-11 01:22:56 +08:00
committed by GitHub
parent 8f9aa1a250
commit dff6268d66
3 changed files with 84 additions and 42 deletions

View File

@ -9,7 +9,7 @@ pub struct DirBuilder {
pub min: Option<u64>,
pub deref: bool,
pub exclude: Option<Pattern>,
pub all: bool,
pub long: bool,
}
impl DirBuilder {
@ -18,14 +18,14 @@ impl DirBuilder {
min: Option<u64>,
deref: bool,
exclude: Option<Pattern>,
all: bool,
long: bool,
) -> DirBuilder {
DirBuilder {
tag,
min,
deref,
exclude,
all,
long,
}
}
}
@ -39,6 +39,7 @@ pub struct DirInfo {
blocks: u64,
path: PathBuf,
tag: Span,
long: bool,
}
#[derive(Debug, Clone)]
@ -47,10 +48,16 @@ pub struct FileInfo {
size: u64,
blocks: Option<u64>,
tag: Span,
long: bool,
}
impl FileInfo {
pub fn new(path: impl Into<PathBuf>, deref: bool, tag: Span) -> Result<Self, ShellError> {
pub fn new(
path: impl Into<PathBuf>,
deref: bool,
tag: Span,
long: bool,
) -> Result<Self, ShellError> {
let path = path.into();
let m = if deref {
std::fs::metadata(&path)
@ -67,6 +74,7 @@ impl FileInfo {
blocks: block_size,
size: d.len(),
tag,
long,
})
}
Err(e) => Err(e.into()),
@ -92,6 +100,7 @@ impl DirInfo {
blocks: 0,
tag: params.tag,
path,
long: params.long,
};
match std::fs::metadata(&s.path) {
@ -154,13 +163,13 @@ impl DirInfo {
.as_ref()
.map_or(true, |x| !x.matches_path(&f));
if include {
match FileInfo::new(f, params.deref, self.tag) {
match FileInfo::new(f, params.deref, self.tag, self.long) {
Ok(file) => {
let inc = params.min.map_or(true, |s| file.size >= s);
if inc {
self.size += file.size;
self.blocks += file.blocks.unwrap_or(0);
if params.all {
if params.long {
self.files.push(file);
}
}
@ -197,16 +206,27 @@ impl From<DirInfo> for Value {
// })
// }
Value::record(
record! {
"path" => Value::string(d.path.display().to_string(), d.tag),
"apparent" => Value::filesize(d.size as i64, d.tag),
"physical" => Value::filesize(d.blocks as i64, d.tag),
"directories" => value_from_vec(d.dirs, d.tag),
"files" => value_from_vec(d.files, d.tag)
},
d.tag,
)
if d.long {
Value::record(
record! {
"path" => Value::string(d.path.display().to_string(), d.tag),
"apparent" => Value::filesize(d.size as i64, d.tag),
"physical" => Value::filesize(d.blocks as i64, d.tag),
"directories" => value_from_vec(d.dirs, d.tag),
"files" => value_from_vec(d.files, d.tag)
},
d.tag,
)
} else {
Value::record(
record! {
"path" => Value::string(d.path.display().to_string(), d.tag),
"apparent" => Value::filesize(d.size as i64, d.tag),
"physical" => Value::filesize(d.blocks as i64, d.tag),
},
d.tag,
)
}
}
}
@ -215,16 +235,27 @@ impl From<FileInfo> for Value {
// cols.push("errors".into());
// vals.push(Value::nothing(Span::unknown()));
Value::record(
record! {
"path" => Value::string(f.path.display().to_string(), f.tag),
"apparent" => Value::filesize(f.size as i64, f.tag),
"physical" => Value::filesize(f.blocks.unwrap_or(0) as i64, f.tag),
"directories" => Value::nothing(Span::unknown()),
"files" => Value::nothing(Span::unknown()),
},
f.tag,
)
if f.long {
Value::record(
record! {
"path" => Value::string(f.path.display().to_string(), f.tag),
"apparent" => Value::filesize(f.size as i64, f.tag),
"physical" => Value::filesize(f.blocks.unwrap_or(0) as i64, f.tag),
"directories" => Value::nothing(Span::unknown()),
"files" => Value::nothing(Span::unknown()),
},
f.tag,
)
} else {
Value::record(
record! {
"path" => Value::string(f.path.display().to_string(), f.tag),
"apparent" => Value::filesize(f.size as i64, f.tag),
"physical" => Value::filesize(f.blocks.unwrap_or(0) as i64, f.tag),
},
f.tag,
)
}
}
}