Skip to content

Commit

Permalink
fixed .unwrap() none value error as well as refactored some of the so…
Browse files Browse the repository at this point in the history
…rting methods into one method with a generic type that returns ordering
  • Loading branch information
willdoescode committed Feb 17, 2021
1 parent f9ff5a8 commit 80d74aa
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 146 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "natls"
version = "2.1.10"
version = "2.1.11"
authors = ["Will Lane <[email protected]>"]
description = "nat - the 'ls' replacement you never knew you needed"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: natls
version: '2.1.10'
version: '2.1.11'
summary: the 'ls' replacement you never knew you needed
description: |
Nat is a complete replacement for the 'ls' command
Expand Down
228 changes: 131 additions & 97 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod text_effects;
mod utils;
use std::os::unix::fs::{FileTypeExt, MetadataExt};
use structopt::StructOpt;
use std::cmp::Ordering;

struct Directory {
paths: Vec<File>,
Expand Down Expand Up @@ -44,59 +45,69 @@ enum PathType {
impl PathType {
fn new(file: &std::path::PathBuf) -> Result<Vec<Self>, Box<dyn std::error::Error>> {
let mut return_val = Vec::new();
if file.symlink_metadata()?.is_dir() { return_val.push(Self::Dir) }
if file.symlink_metadata()?.file_type().is_symlink() { return_val.push(Self::Symlink) }
if file.symlink_metadata()?.file_type().is_fifo() { return_val.push(Self::Pipe) }
if file.symlink_metadata()?.file_type().is_char_device() { return_val.push(Self::CharD) }
if file.symlink_metadata()?.file_type().is_block_device() { return_val.push(Self::BlockD) }
if file.symlink_metadata()?.file_type().is_socket() { return_val.push(Self::Socket) }
if return_val.is_empty() { return_val.push(Self::Path) }
if file.symlink_metadata()?.is_dir() {return_val.push(Self::Dir) }
if file.symlink_metadata()?.file_type().is_symlink() {return_val.push(Self::Symlink)}
if file.symlink_metadata()?.file_type().is_fifo() {return_val.push(Self::Pipe)}
if file.symlink_metadata()?.file_type().is_char_device() {return_val.push(Self::CharD)}
if file.symlink_metadata()?.file_type().is_block_device() {return_val.push(Self::BlockD)}
if file.symlink_metadata()?.file_type().is_socket() {return_val.push(Self::Socket)}
if return_val.is_empty() {return_val.push(Self::Path)}

Ok(return_val)
}

fn create_letter(&self, letter: &str) -> String {
format!(
"{}{}{}{}",
self.get_color_for_type(),
letter,
termion::color::Fg(termion::color::Reset),
termion::color::Bg(termion::color::Reset)
)
}

fn get_letter_for_type(&self) -> String {
match self {
Self::Dir => format!("{}d{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
Self::Symlink => format!("{}l{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
Self::Pipe => format!("{}|{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
Self::CharD => format!("{}c{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
Self::BlockD => format!("{}b{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
Self::Socket => format!("{}s{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
_ => format!("{}.{}{}", self.get_color_for_type(), termion::color::Fg(termion::color::Reset), termion::color::Bg(termion::color::Reset)),
Self::Dir => self.create_letter("d"),
Self::Symlink => self.create_letter("l"),
Self::Pipe => self.create_letter("|"),
Self::CharD => self.create_letter("c"),
Self::BlockD => self.create_letter("b"),
Self::Socket => self.create_letter("s"),
_ => self.create_letter("."),
}
}

fn get_color_for_type(&self) -> String {
match self {
Self::Dir => format!("{}" , termion::color::Fg(termion::color::LightBlue)),
Self::Symlink => format!("{}" , termion::color::Fg(termion::color::LightMagenta)),
Self::Path => format!("{}" , termion::color::Fg(termion::color::White)),
Self::Pipe => format!("{}" , termion::color::Fg(termion::color::Yellow)),
Self::CharD => format!("{}{}", termion::color::Bg(termion::color::Yellow), termion::color::Fg(termion::color::LightBlue) ),
Self::BlockD => format!("{}" , termion::color::Fg(termion::color::LightGreen)),
Self::Socket => format!("{}" , termion::color::Fg(termion::color::LightRed)),
Self::Dir => format!("{}", termion::color::Fg(termion::color::LightBlue)),
Self::Symlink => format!("{}", termion::color::Fg(termion::color::LightMagenta)),
Self::Path => format!("{}", termion::color::Fg(termion::color::White)),
Self::Pipe => format!("{}", termion::color::Fg(termion::color::Yellow)),
Self::CharD => format!("{}{}", termion::color::Bg(termion::color::Yellow), termion::color::Fg(termion::color::LightBlue)),
Self::BlockD => format!("{}", termion::color::Fg(termion::color::LightGreen)),
Self::Socket => format!("{}", termion::color::Fg(termion::color::LightRed)),
}
}

fn get_text_traits_for_type(&self, name: &str, file: &std::path::PathBuf) -> String {
match self {
Self::Dir => text_effects::bold(&format!( "{}{}/" , name, termion::color::Fg(termion::color::White) )),
Self::Symlink => text_effects::italic(&format!( "{} -> {}", name, std::fs::read_link(file).unwrap().display().to_string() )),
Self::Path => text_effects::bold(name) ,
Self::Pipe => text_effects::bold(&format!( "{}{}" , name, termion::color::Fg(termion::color::White) )),
Self::CharD => text_effects::bold(name) ,
Self::BlockD => text_effects::bold(name) ,
Self::Socket => text_effects::bold(&format!( "{}{}" , name, termion::color::Fg(termion::color::White) )),
Self::Dir => text_effects::bold(&format!( "{}{}/", name, termion::color::Fg(termion::color::White))),
Self::Symlink => text_effects::italic(&format!( "{} -> {}", name, std::fs::read_link(file).unwrap().display().to_string())),
Self::Path => text_effects::bold(name),
Self::Pipe => text_effects::bold(&format!( "{}{}", name, termion::color::Fg(termion::color::White))),
Self::CharD => text_effects::bold(name),
Self::BlockD => text_effects::bold(name),
Self::Socket => text_effects::bold(&format!( "{}{}", name, termion::color::Fg(termion::color::White))),
}
}
}

impl File {
fn new(file: std::path::PathBuf) -> Self {
Self {
group: utils::get_group::group(file.to_path_buf()),
user: utils::get_user::user(file.to_path_buf()),
group: utils::group(file.to_path_buf()),
user: utils::user(file.to_path_buf()),
modified: utils::file_times::modified(file.to_path_buf(), input::Cli::from_args().time_format),
created: utils::file_times::created(file.to_path_buf(), input::Cli::from_args().time_format),
size: utils::size::size(file.to_path_buf()),
Expand Down Expand Up @@ -143,49 +154,6 @@ impl Directory {
Ok(Self { paths })
}

fn self_name_sort(&mut self) {
self.paths.sort_by(|a, b| {
a.path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_lowercase()
.cmp(&b.path.file_name().unwrap().to_str().unwrap().to_lowercase())
})
}

fn self_create_sort(&mut self) {
self.paths.sort_by(|a, b| {
a.path
.symlink_metadata()
.unwrap()
.created()
.unwrap()
.cmp(&b.path.symlink_metadata().unwrap().created().unwrap())
})
}

fn self_modified_sort(&mut self) {
self.paths.sort_by(|a, b| {
a.path
.symlink_metadata()
.unwrap()
.modified()
.unwrap()
.cmp(&b.path.symlink_metadata().unwrap().modified().unwrap())
})
}

fn self_size_sort(&mut self) {
self.paths.sort_by(|a, b| {
a.path
.symlink_metadata()
.unwrap()
.size()
.cmp(&b.path.symlink_metadata().unwrap().size())
})
}

fn sort_directory_then_path(&mut self) {
let new = &self.paths;
Expand Down Expand Up @@ -235,14 +203,63 @@ impl Directory {
input::Cli::from_args().modified,
input::Cli::from_args().size,
]) {
DirSortType::Name => self.self_name_sort(),
DirSortType::Created => self.self_create_sort(),
DirSortType::Modified => self.self_modified_sort(),
DirSortType::Size => self.self_size_sort(),
DirSortType::Name => sort_as(&mut self.paths, |a, b| {
a.path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_lowercase()
.cmp(&b.path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_lowercase()
)
}),
DirSortType::Created => sort_as(&mut self.paths ,|a, b| {
a.path
.symlink_metadata()
.unwrap()
.created()
.unwrap()
.cmp(&b.path
.symlink_metadata()
.unwrap()
.created()
.unwrap()
)
}),
DirSortType::Modified => sort_as(&mut self.paths, |a, b| {
a.path
.symlink_metadata()
.unwrap()
.modified()
.unwrap()
.cmp(&b.path
.symlink_metadata()
.unwrap()
.modified()
.unwrap()
)
}),
DirSortType::Size => sort_as(&mut self.paths, |a, b| {
a.path
.symlink_metadata()
.unwrap()
.size()
.cmp(&b.path.
symlink_metadata()
.unwrap()
.size()
)
}),
DirSortType::Not => (),
}
}


fn sort(&mut self) {
match input::Cli::from_args().gdf {
true => self.sort_directory_then_path(),
Expand Down Expand Up @@ -288,12 +305,18 @@ impl Directory {
}
}

fn setup(&mut self) {
fn setup(&mut self) -> &mut Directory {
self.sort();
self.add_space();
self
}
}

fn sort_as<T>(files: &mut Vec<File>, sort_method: T)
where T: Fn(&File, &File) -> Ordering {
files.sort_by(sort_method)
}

fn name_sort(dir: &mut Vec<File>) {
dir.sort_by(|a, b| {
a.path
Expand Down Expand Up @@ -346,7 +369,16 @@ impl std::fmt::Display for File {
res = format!(
"{}{}",
v.get_color_for_type(),
v.get_text_traits_for_type(&self.path.components().next_back().unwrap().as_os_str().to_string_lossy().to_string(), &self.path)
v.get_text_traits_for_type(
&self.path.
components()
.next_back()
.unwrap()
.as_os_str()
.to_string_lossy()
.to_string(),
&self.path
)
);
} else {
res = format!(
Expand All @@ -365,8 +397,16 @@ impl std::fmt::Debug for File {
let mut res = String::new();
for (i, v) in self.file_type.iter().enumerate() {
if i == 0 {
res =
v.get_text_traits_for_type(&self.path.components().next_back().unwrap().as_os_str().to_string_lossy().to_string(), &self.path);
res = v.get_text_traits_for_type(
&self.path
.components()
.next_back()
.unwrap()
.as_os_str()
.to_string_lossy()
.to_string(),
&self.path
);
res = format!("{}{}", v.get_color_for_type(), res);
} else {
res = v.get_text_traits_for_type(&res, &self.path);
Expand All @@ -377,18 +417,11 @@ impl std::fmt::Debug for File {
if input::Cli::from_args().created_time {
time = &self.created;
}
writeln!(
f,
"{} {}{} {}{} {} {}{} {}",
self.perms,
termion::color::Fg(termion::color::LightGreen),
self.size,
termion::color::Fg(termion::color::Yellow),
self.user,
self.group,
termion::color::Fg(termion::color::Blue),
time,
res
writeln!(f, "{} {green}{} {yellow}{} {blue} {}{} {}",
self.perms, self.size, self.user, self.group, time, res,
green = termion::color::Fg(termion::color::LightGreen),
yellow = termion::color::Fg(termion::color::Yellow),
blue = termion::color::Fg(termion::color::Blue),
)
}
}
Expand All @@ -405,10 +438,11 @@ impl std::fmt::Display for Directory {
}

fn main() {
let mut dir = Directory::new(input::Cli::from_args().dir).expect("Failed to run natls");
// let mut dir = Directory::new(std::path::PathBuf::from("wrong path")).expect("Failed to run natls");
dir.setup();
println!("{}", dir);
println!("{}",
Directory::new(input::Cli::from_args().dir)
.expect("Failed to run natls")
.setup()
);
}

#[cfg(test)]
Expand Down
32 changes: 8 additions & 24 deletions src/text_effects.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
pub fn bold(i: &str) -> String {
format!("\x1B[1m{}\x1B[0m", i)
}
pub fn bold(i: &str) -> String { format!("\x1B[1m{}\x1B[0m", i) }

pub fn dimmed(i: &str) -> String {
format!("\x1B[2m{}\x1B[0m", i)
}
pub fn dimmed(i: &str) -> String { format!("\x1B[2m{}\x1B[0m", i) }

pub fn italic(i: &str) -> String {
format!("\x1B[3m{}\x1B[0m", i)
}
pub fn italic(i: &str) -> String { format!("\x1B[3m{}\x1B[0m", i) }

pub fn underline(i: &str) -> String {
format!("\x1B[4m{}\x1B[0m", i)
}
pub fn underline(i: &str) -> String { format!("\x1B[4m{}\x1B[0m", i) }

pub fn blink(i: &str) -> String {
format!("\x1B[5m{}\x1B[0m", i)
}
pub fn blink(i: &str) -> String { format!("\x1B[5m{}\x1B[0m", i) }

pub fn reverse(i: &str) -> String {
format!("\x1B[7m{}\x1B[0m", i)
}
pub fn reverse(i: &str) -> String { format!("\x1B[7m{}\x1B[0m", i) }

pub fn hidden(i: &str) -> String {
format!("\x1B[8m{}\x1B[0m", i)
}
pub fn hidden(i: &str) -> String { format!("\x1B[8m{}\x1B[0m", i) }

pub fn stricken(i: &str) -> String {
format!("\x1B[9m{}\x1B[0m", i)
}
pub fn stricken(i: &str) -> String { format!("\x1B[9m{}\x1B[0m", i) }
Loading

0 comments on commit 80d74aa

Please sign in to comment.