add -l flag to du

This commit is contained in:
WindSoilder 2024-11-21 22:28:08 +08:00
parent e63976df7e
commit 4f5527bd09
4 changed files with 83 additions and 24 deletions

View File

@ -15,6 +15,7 @@ pub struct DuArgs {
path: Option<Spanned<NuGlob>>, path: Option<Spanned<NuGlob>>,
all: bool, all: bool,
deref: bool, deref: bool,
long: bool,
exclude: Option<Spanned<NuGlob>>, exclude: Option<Spanned<NuGlob>>,
#[serde(rename = "max-depth")] #[serde(rename = "max-depth")]
max_depth: Option<Spanned<i64>>, max_depth: Option<Spanned<i64>>,
@ -50,6 +51,11 @@ impl Command for Du {
"Dereference symlinks to their targets for size", "Dereference symlinks to their targets for size",
Some('r'), Some('r'),
) )
.switch(
"long",
"Get underlying directories and files for each entry",
Some('l'),
)
.named( .named(
"exclude", "exclude",
SyntaxShape::GlobPattern, SyntaxShape::GlobPattern,
@ -97,6 +103,7 @@ impl Command for Du {
} }
let all = call.has_flag(engine_state, stack, "all")?; let all = call.has_flag(engine_state, stack, "all")?;
let deref = call.has_flag(engine_state, stack, "deref")?; let deref = call.has_flag(engine_state, stack, "deref")?;
let long = call.has_flag(engine_state, stack, "long")?;
let exclude = call.get_flag(engine_state, stack, "exclude")?; let exclude = call.get_flag(engine_state, stack, "exclude")?;
#[allow(deprecated)] #[allow(deprecated)]
let current_dir = current_dir(engine_state, stack)?; let current_dir = current_dir(engine_state, stack)?;
@ -114,6 +121,7 @@ impl Command for Du {
path: None, path: None,
all, all,
deref, deref,
long,
exclude, exclude,
max_depth, max_depth,
min_size, min_size,
@ -130,6 +138,7 @@ impl Command for Du {
path: Some(p), path: Some(p),
all, all,
deref, deref,
long,
exclude: exclude.clone(), exclude: exclude.clone(),
max_depth, max_depth,
min_size, min_size,
@ -200,6 +209,7 @@ fn du_for_one_pattern(
let all = args.all; let all = args.all;
let deref = args.deref; let deref = args.deref;
let long = args.long;
let max_depth = args.max_depth.map(|f| f.item as u64); let max_depth = args.max_depth.map(|f| f.item as u64);
let min_size = args.min_size.map(|f| f.item as u64); let min_size = args.min_size.map(|f| f.item as u64);
@ -209,6 +219,7 @@ fn du_for_one_pattern(
deref, deref,
exclude, exclude,
all, all,
long,
}; };
let mut output: Vec<Value> = vec![]; let mut output: Vec<Value> = vec![];
@ -217,7 +228,7 @@ fn du_for_one_pattern(
Ok(a) => { Ok(a) => {
if a.is_dir() { if a.is_dir() {
output.push(DirInfo::new(a, &params, max_depth, span, signals)?.into()); output.push(DirInfo::new(a, &params, max_depth, span, signals)?.into());
} else if let Ok(v) = FileInfo::new(a, deref, span) { } else if let Ok(v) = FileInfo::new(a, deref, span, params.long) {
output.push(v.into()); output.push(v.into());
} }
} }

View File

@ -666,7 +666,7 @@ pub(crate) fn dir_entry_dict(
if md.is_dir() { if md.is_dir() {
if du { if du {
let params = DirBuilder::new(Span::new(0, 2), None, false, None, false); let params = DirBuilder::new(Span::new(0, 2), None, false, None, false, false);
let dir_size = DirInfo::new(filename, &params, None, span, signals)?.get_size(); let dir_size = DirInfo::new(filename, &params, None, span, signals)?.get_size();
Value::filesize(dir_size as i64, span) Value::filesize(dir_size as i64, span)

View File

@ -10,6 +10,7 @@ pub struct DirBuilder {
pub deref: bool, pub deref: bool,
pub exclude: Option<Pattern>, pub exclude: Option<Pattern>,
pub all: bool, pub all: bool,
pub long: bool,
} }
impl DirBuilder { impl DirBuilder {
@ -19,6 +20,7 @@ impl DirBuilder {
deref: bool, deref: bool,
exclude: Option<Pattern>, exclude: Option<Pattern>,
all: bool, all: bool,
long: bool,
) -> DirBuilder { ) -> DirBuilder {
DirBuilder { DirBuilder {
tag, tag,
@ -26,6 +28,7 @@ impl DirBuilder {
deref, deref,
exclude, exclude,
all, all,
long,
} }
} }
} }
@ -39,6 +42,7 @@ pub struct DirInfo {
blocks: u64, blocks: u64,
path: PathBuf, path: PathBuf,
tag: Span, tag: Span,
long: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -47,10 +51,16 @@ pub struct FileInfo {
size: u64, size: u64,
blocks: Option<u64>, blocks: Option<u64>,
tag: Span, tag: Span,
long: bool,
} }
impl FileInfo { 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 path = path.into();
let m = if deref { let m = if deref {
std::fs::metadata(&path) std::fs::metadata(&path)
@ -67,6 +77,7 @@ impl FileInfo {
blocks: block_size, blocks: block_size,
size: d.len(), size: d.len(),
tag, tag,
long,
}) })
} }
Err(e) => Err(e.into()), Err(e) => Err(e.into()),
@ -92,6 +103,7 @@ impl DirInfo {
blocks: 0, blocks: 0,
tag: params.tag, tag: params.tag,
path, path,
long: params.long,
}; };
match std::fs::metadata(&s.path) { match std::fs::metadata(&s.path) {
@ -154,7 +166,7 @@ impl DirInfo {
.as_ref() .as_ref()
.map_or(true, |x| !x.matches_path(&f)); .map_or(true, |x| !x.matches_path(&f));
if include { if include {
match FileInfo::new(f, params.deref, self.tag) { match FileInfo::new(f, params.deref, self.tag, self.long) {
Ok(file) => { Ok(file) => {
let inc = params.min.map_or(true, |s| file.size >= s); let inc = params.min.map_or(true, |s| file.size >= s);
if inc { if inc {
@ -197,6 +209,7 @@ impl From<DirInfo> for Value {
// }) // })
// } // }
if d.long {
Value::record( Value::record(
record! { record! {
"path" => Value::string(d.path.display().to_string(), d.tag), "path" => Value::string(d.path.display().to_string(), d.tag),
@ -207,6 +220,16 @@ impl From<DirInfo> for Value {
}, },
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,6 +238,7 @@ impl From<FileInfo> for Value {
// cols.push("errors".into()); // cols.push("errors".into());
// vals.push(Value::nothing(Span::unknown())); // vals.push(Value::nothing(Span::unknown()));
if f.long {
Value::record( Value::record(
record! { record! {
"path" => Value::string(f.path.display().to_string(), f.tag), "path" => Value::string(f.path.display().to_string(), f.tag),
@ -225,6 +249,16 @@ impl From<FileInfo> for Value {
}, },
f.tag, 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,
)
}
} }
} }

View File

@ -100,3 +100,17 @@ fn du_with_multiple_path() {
let actual = nu!(cwd: "tests/fixtures", "du ...[] | length"); let actual = nu!(cwd: "tests/fixtures", "du ...[] | length");
assert_eq!(actual.out, "0"); assert_eq!(actual.out, "0");
} }
#[test]
fn test_du_output_columns() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"du -m 1 | columns | str join ','"
);
assert_eq!(actual.out, "path,apparent,physical");
let actual = nu!(
cwd: "tests/fixtures/formats",
"du -m 1 -l | columns | str join ','"
);
assert_eq!(actual.out, "path,apparent,physical,directories,files");
}