use std::{collections::HashMap, env, fs}; use number_prefix::NumberPrefix; use table::{Row, Table}; fn main() -> std::io::Result<()> { let ret = clparse::Arguments::parse_from_args().unwrap(); let args = ret.1; let mut table = Table { table: HashMap::new(), }; let current_dir = env::current_dir()?; let size_shown = args.is_truthy("s"); let last_modified_shown = args.is_truthy("lm"); let row = Row(vec![], 20); table.table.insert("File Name".to_string(), row); if size_shown { let row = Row(vec![], 20); table.table.insert("Size".to_string(), row); } if last_modified_shown { let row = Row(vec![], 20); table.table.insert("Last Modified".to_string(), row); } for entry in fs::read_dir(current_dir)? { let entry = entry?; let path = entry.path(); let metadata = fs::metadata(&path)?; let abc = table.table.get_mut(&"File Name".to_string()); let mut path = format!( "{}", path.file_name() .ok_or("No filename") .unwrap() .to_str() .unwrap(), ); if metadata.is_dir() { path.push('/') } match abc { Some(row) => row.0.push(path), None => todo!(), } if size_shown { let size = metadata.len(); let abc = table.table.get_mut(&"Size".to_string()); match abc { Some(row) => row.0.push(maybe_bytes_suffix(size)), None => todo!(), } } if last_modified_shown { let last_modified = metadata.modified()?.elapsed().unwrap().as_secs(); let abc = table.table.get_mut(&"Last Modified".to_string()); match abc { Some(row) => row.0.push(format!("{} seconds ago", last_modified)), None => todo!(), } // print!(" {} seconds ago |", last_modified); } print!("\n"); } println!("{}", table); Ok(()) } fn maybe_bytes_suffix(amount: u64) -> String { return match NumberPrefix::binary(amount as f64) { NumberPrefix::Standalone(bytes) => { format!("{}B", bytes) } NumberPrefix::Prefixed(prefix, n) => { format!("{:.1} {}B", n, prefix.symbol()) } }; }