diff --git a/src/builtins/ls.rs b/src/builtins/ls.rs index 1c0e019..9a0ede0 100644 --- a/src/builtins/ls.rs +++ b/src/builtins/ls.rs @@ -36,17 +36,31 @@ impl Builtin for Ls { } else if metadata.file_type().is_symlink() { 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(()) } } -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 {