Skip to content

Commit

Permalink
removed some immediate reference dereferences and added general bug f…
Browse files Browse the repository at this point in the history
…ixes along with some instructions on contributing
  • Loading branch information
willdoescode committed Nov 4, 2020
1 parent 134defd commit 1397df7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 102 deletions.
9 changes: 9 additions & 0 deletions contributing.md
@@ -0,0 +1,9 @@
```bash
cargo fmt
```

```bash
rustup component add clippy

cargo clippy
```
199 changes: 97 additions & 102 deletions src/main.rs
@@ -1,3 +1,4 @@
#![allow(clippy::collapsible_if)]
extern crate libc;
extern crate pretty_bytes;

Expand Down Expand Up @@ -66,10 +67,9 @@ pub struct Cli {
time_format: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn output() -> Result<(), Box<dyn std::error::Error>> {
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;
Expand All @@ -81,114 +81,109 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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::<Result<Vec<_>, 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() {
let _ = single(&directory, 4 as usize, *wide_mode, time_format);
std::process::exit(0);
}


let entries = fs::read_dir(directory)?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, 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(
Expand Down Expand Up @@ -216,7 +211,7 @@ pub fn draw_headlines(
draw_headline("user", 0, true);
}
draw_headline("name", 0, true);
print!("\n");
println!();
}
}

Expand Down Expand Up @@ -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(())
}
Expand All @@ -254,7 +249,7 @@ pub fn time_mod(
e: &std::path::PathBuf,
time_format: &str,
) -> Result<(), Box<dyn std::error::Error>> {
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 {
Expand All @@ -277,7 +272,7 @@ pub fn show_group_name(e: &std::path::PathBuf) -> Result<(), Box<dyn std::error:
match get_group_by_gid(fs::symlink_metadata(e)?.gid()).as_ref() {
Some(n) => n.name().to_str().unwrap(),
None => "",
}
}
)
);
Ok(())
Expand All @@ -293,7 +288,7 @@ pub fn show_user_name(e: &std::path::PathBuf) -> Result<(), Box<dyn std::error::
match get_user_by_uid(fs::symlink_metadata(e)?.uid()).as_ref() {
Some(n) => n.name().to_str().unwrap(),
None => "",
}
}
)
);
Ok(())
Expand All @@ -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!(" ");
}
Expand All @@ -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) => {
Expand All @@ -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 {
Expand All @@ -346,9 +341,9 @@ pub fn show_file_name(
print!(
"{}",
fs::canonicalize(fs::read_link(e)?)
.unwrap()
.to_str()
.unwrap()
.unwrap()
.to_str()
.unwrap()
)
}
}
Expand All @@ -374,7 +369,7 @@ pub fn show_file_name(
}
}
if !wide_mode {
print!("\n");
println!();
} else {
print!(" ");
}
Expand All @@ -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!(" ");
}
Expand Down Expand Up @@ -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("")
}
Expand Down

0 comments on commit 1397df7

Please sign in to comment.