diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..d022a0f --- /dev/null +++ b/contributing.md @@ -0,0 +1,9 @@ +```bash +cargo fmt +``` + +```bash +rustup component add clippy + +cargo clippy +``` diff --git a/src/main.rs b/src/main.rs index 59125e5..2f99416 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![allow(clippy::collapsible_if)] extern crate libc; extern crate pretty_bytes; @@ -66,10 +67,9 @@ pub struct Cli { time_format: String, } -fn main() -> Result<(), Box> { +fn output() -> Result<(), Box> { let args = Cli::from_args(); let directory = &args.path; - let headline_on = &args.headline_on; let hidden_files = &args.hidden_files; let wide_mode = &args.wide_mode; let time_on = &args.time_on; @@ -81,52 +81,44 @@ fn main() -> Result<(), Box> { let time_format = &args.time_format; let colors_on = &args.colors_on; - draw_headlines( - *headline_on, - *perms_on, - *size_on, - *time_on, - *group_on, - *user_on, - ); - let mut singly_found = false; if !std::path::Path::new(directory).exists() { let entries = fs::read_dir(".")? .map(|res| res.map(|e| e.path())) .collect::, io::Error>>()?; - - let mut size_count = 4; - for s in &entries { - if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count { - size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len(); - }; + + let mut size_count = 4; + for s in &entries { + if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count { + size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len(); + }; + } + for e in &entries { + if e + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_lowercase() + .contains(&args.path.display().to_string().to_lowercase()) + { + let _ = single(e, size_count, *wide_mode, time_format); + singly_found = true; } - for e in &entries { - if e - .file_name() - .unwrap() - .to_str() - .unwrap() - .to_lowercase() - .contains(&args.path.display().to_string().to_lowercase()) - { - let _ = single(e, size_count, *wide_mode, time_format); - singly_found = true; - } - } - if !singly_found { - if !*colors_on { - print!("{}", color::Fg(color::Red)); - } - println!( - "{}", - Style::new() - .bold() - .paint(format!("{} could not be found", &args.path.display().to_string())) - ); - } - std::process::exit(1); + } + if !singly_found { + if !*colors_on { + print!("{}", color::Fg(color::Red)); + } + println!( + "{}", + Style::new().bold().paint(format!( + "{} could not be found", + &args.path.display().to_string() + )) + ); + } + std::process::exit(1); } if !directory.symlink_metadata()?.is_dir() { @@ -134,61 +126,64 @@ fn main() -> Result<(), Box> { std::process::exit(0); } - let entries = fs::read_dir(directory)? .map(|res| res.map(|e| e.path())) .collect::, io::Error>>()?; - let mut size_count = 4; - let mut group_size = 8; - for s in &entries { - if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count { - size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len(); - }; - - let metadata_uid = fs::symlink_metadata(&s)?.uid(); - let user_name_len = get_user_name(metadata_uid).len(); - if user_name_len > group_size { - group_size = user_name_len; - } + let mut size_count = 4; + let mut group_size = 8; + for s in &entries { + if convert(fs::symlink_metadata(&s)?.size() as f64).len() > size_count { + size_count = convert(fs::symlink_metadata(&s)?.size() as f64).len(); + }; + + let metadata_uid = fs::symlink_metadata(&s)?.uid(); + let user_name_len = get_user_name(metadata_uid).len(); + if user_name_len > group_size { + group_size = user_name_len; } + } - let mut dirs: Vec<&std::path::PathBuf> = vec![]; - - for e in &entries { - if !&e.file_name().unwrap().to_str().unwrap().starts_with(".") || *hidden_files { - if *&e.is_file() && !*is_sorted { - dirs.push(*&e); - } else { - if !perms_on { - let _ = file_perms(&e); - } + let mut dirs: Vec<&std::path::PathBuf> = vec![]; - if !size_on { - let _ = file_size(size_count, &e); - } + for e in &entries { + if !&e.file_name().unwrap().to_str().unwrap().starts_with('.') || *hidden_files { + if e.is_file() && !*is_sorted { + dirs.push(e); + } else { + if !perms_on { + let _ = file_perms(&e); + } - if !time_on { - let _ = time_mod(e, time_format); - } + if !size_on { + let _ = file_size(size_count, &e); + } - if !group_on { - let _ = show_group_name(e); - } + if !time_on { + let _ = time_mod(e, time_format); + } - if !user_on { - let _ = show_user_name(e); - } + if !group_on { + let _ = show_group_name(e); + } - let _ = show_file_name(&e, *wide_mode); + if !user_on { + let _ = show_user_name(e); } + + let _ = show_file_name(&e, *wide_mode); } } - for e in &dirs { - let _ = single(e, size_count, *wide_mode, time_format); - } + } + for e in &dirs { + let _ = single(e, size_count, *wide_mode, time_format); + } - Ok(()) + Ok(()) +} + +fn main() { + let _ = output(); } pub fn draw_headlines( @@ -216,7 +211,7 @@ pub fn draw_headlines( draw_headline("user", 0, true); } draw_headline("name", 0, true); - print!("\n"); + println!(); } } @@ -244,8 +239,8 @@ pub fn file_size( print!( "{} ", Style::new() - .bold() - .paint(convert(fs::symlink_metadata(&e)?.size() as f64)) + .bold() + .paint(convert(fs::symlink_metadata(&e)?.size() as f64)) ); Ok(()) } @@ -254,7 +249,7 @@ pub fn time_mod( e: &std::path::PathBuf, time_format: &str, ) -> Result<(), Box> { - if let Ok(_) = e.symlink_metadata()?.modified() { + if e.symlink_metadata()?.modified().is_ok() { let mtime = FileTime::from_last_modification_time(&e.symlink_metadata().unwrap()); let d = NaiveDateTime::from_timestamp(mtime.seconds() as i64, 0); if !Cli::from_args().colors_on { @@ -277,7 +272,7 @@ pub fn show_group_name(e: &std::path::PathBuf) -> Result<(), Box n.name().to_str().unwrap(), None => "", - } + } ) ); Ok(()) @@ -293,7 +288,7 @@ pub fn show_user_name(e: &std::path::PathBuf) -> Result<(), Box n.name().to_str().unwrap(), None => "", - } + } ) ); Ok(()) @@ -313,7 +308,7 @@ pub fn show_file_name( } print!("{}/", &e.file_name().unwrap().to_str().unwrap()); if !wide_mode { - print!("\n"); + println!(); } else { print!(" "); } @@ -324,8 +319,8 @@ pub fn show_file_name( print!( "{} -> ", Style::new() - .bold() - .paint(e.file_name().unwrap().to_str().unwrap()) + .bold() + .paint(e.file_name().unwrap().to_str().unwrap()) ); match fs::canonicalize(fs::read_link(e)?) { Ok(_n) => { @@ -335,9 +330,9 @@ pub fn show_file_name( print!( "{}/", fs::canonicalize(fs::read_link(e)?) - .unwrap() - .to_str() - .unwrap() + .unwrap() + .to_str() + .unwrap() ) } } else { @@ -346,9 +341,9 @@ pub fn show_file_name( print!( "{}", fs::canonicalize(fs::read_link(e)?) - .unwrap() - .to_str() - .unwrap() + .unwrap() + .to_str() + .unwrap() ) } } @@ -374,7 +369,7 @@ pub fn show_file_name( } } if !wide_mode { - print!("\n"); + println!(); } else { print!(" "); } @@ -385,11 +380,11 @@ pub fn show_file_name( print!( "{}", Style::new() - .bold() - .paint(e.file_name().unwrap().to_str().unwrap()) + .bold() + .paint(e.file_name().unwrap().to_str().unwrap()) ); if !wide_mode { - print!("\n"); + println!(); } else { print!(" "); } @@ -461,7 +456,7 @@ pub fn perms(mode: u16) -> String { format!("{}{}", color::Fg(color::LightRed), group), format!("{}{}", color::Fg(color::Yellow), other), ] - .join("") + .join("") } else { [user, group, other].join("") }