Skip to content

Commit

Permalink
[#6] feat: update output format, add --no-header flag and use human f…
Browse files Browse the repository at this point in the history
…riendly values (#12)
  • Loading branch information
Angelmmiguel committed Feb 23, 2022
1 parent 4043803 commit a3ae08a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
use crate::FileSize;

const KBYTES:u64 = 1024;
const MBYTES:u64 = 1048576;
const GBYTES:u64 = 1073741824;

pub fn print_headers() {
println!("DISK\tBYTES\tPATH");
}

pub fn print_size(path: &std::path::PathBuf, calc: FileSize) {
println!("{:?}\tDisk: {:?}\tBytes: {:?}", path, calc.disk, calc.bytes);
let path_string = String::from(path.to_string_lossy());
println!("{}\t{}\t{}", human_size(calc.disk), human_size(calc.bytes), path_string);
}

fn human_size(size: u64) -> String {
if size > GBYTES {
return format!("{}G", size / GBYTES);
} else if size > MBYTES {
return format!("{}M", size / MBYTES);
} else if size > KBYTES {
return format!("{}K", size / KBYTES);
} else {
return format!("{}B", size);
}
}
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ use crate::size::{
entry_size,
FileSize
};
use crate::logger::print_size;
use crate::logger::{
print_size,
print_headers
};

/// Search for a pattern in a file and display the lines that contain it.
#[derive(Parser)]
struct Cli {
/// The path to the file to read
#[clap(parse(from_os_str))]
path: std::path::PathBuf,

/// Hide the headers from the output
#[clap(long)]
no_header: bool,
}

fn main() -> std::io::Result<()> {
let args = Cli::parse();

let calc = entry_size(&args.path)?;

if !args.no_header {
print_headers()
}

print_size(&args.path, calc);

Ok(())
Expand Down

0 comments on commit a3ae08a

Please sign in to comment.