Browse Source

feat: add better file size formatting

main
fdai7375 2 years ago
committed by fdai7451
parent
commit
753b6ae1c6
  1. 26
      src/builtins/ls.rs

26
src/builtins/ls.rs

@ -36,17 +36,31 @@ impl Builtin for Ls {
} else if metadata.file_type().is_symlink() { } else if metadata.file_type().is_symlink() {
file_type = "link" file_type = "link"
} }
println!(
"{}",
format_line(&right_padding(&file_name, 16), file_type, metadata.len())
)
println!("{}", format_line(20, &file_name, file_type, metadata.len()));
} }
Ok(()) Ok(())
} }
} }
fn format_line(right_padding: &str, file_type: &str, file_size: u64) -> String {
format!("{:} | {:4} | {}", right_padding, file_type, file_size)
fn format_filesize(filesize: u64) -> String {
if filesize > 9_999 && filesize <= 999_999 {
return format!("{}KB", filesize / 1_000);
} else if filesize > 999_999 && filesize <= 999_999_999 {
return format!("{}MB", filesize / 1_000_000);
} else if filesize > 999_999_999 {
return format!("{}MB", filesize / 1_000_000_000);
} else {
format!("{}B", filesize)
}
}
fn format_line(max_name_len: usize, file_name: &str, file_type: &str, file_size: u64) -> String {
format!(
"{} | {:4} | {:6}",
right_padding(file_name, max_name_len),
file_type,
format_filesize(file_size),
)
} }
fn right_padding(s: &str, max: usize) -> String { fn right_padding(s: &str, max: usize) -> String {

Loading…
Cancel
Save