From 61b8f5d7fde4fc81e97c2239ccb22407400a85c2 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Sat, 4 May 2019 16:26:27 +0530 Subject: [PATCH] have most of it working --- src/app.rs | 4 ++-- src/display.rs | 30 ++++++++++++++++++++++++------ src/flags.rs | 6 ++++-- src/meta/symlink.rs | 12 ++++++++++-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8af17b4..82b94b9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -171,8 +171,8 @@ pub fn build() -> App<'static, 'static> { .multiple(true) .number_of_values(1) .require_delimiter(true) - .possible_values(&["permission", "user", "group", "size", "date", "name"]) - .default_value("permission,user,group,size,date,name") + .possible_values(&["filetype","permission", "user", "group", "size", "date", "name", "namewithsymlink"]) + .default_value("filetype,permission,user,group,size,date,namewithsymlink") .help("Specify the blocks that will be displayed and in what order"), ) .arg( diff --git a/src/display.rs b/src/display.rs index ff74b8a..f5b56e7 100644 --- a/src/display.rs +++ b/src/display.rs @@ -19,6 +19,7 @@ struct PaddingRules { size: (usize, usize), date: usize, name: usize, + name_with_symlink: usize, } pub fn one_line(metas: Vec, flags: &Flags, colors: &Colors, icons: &Icons) -> String { @@ -57,6 +58,7 @@ fn inner_display_one_line( size: detect_size_lengths(&metas, &flags), date: detect_date_length(&metas, &flags), name: detect_name_length(&metas, &icons), + name_with_symlink: detect_name_with_symlink_length(&metas, &icons) }) } @@ -281,10 +283,8 @@ fn get_long_output( let mut strings: Vec = Vec::new(); for block in flags.blocks.iter() { match block { - Block::Permission => { - strings.push(meta.file_type.render(colors)); - strings.push(meta.permissions.render(colors)); - } + Block::FileType => strings.push(meta.file_type.render(colors)), + Block::Permission => strings.push(meta.permissions.render(colors)), Block::User => strings.push(meta.owner.render_user(colors, padding_rules.user)), Block::Group => strings.push(meta.owner.render_group(colors, padding_rules.group)), Block::Size => strings.push(meta.size.render( @@ -294,8 +294,9 @@ fn get_long_output( &flags, )), Block::Date => strings.push(meta.date.render(colors, padding_rules.date, &flags)), - Block::Name => { - strings.push(meta.name.render(colors, icons, Some(padding_rules.name))); + Block::Name => strings.push(meta.name.render(colors, icons, Some(padding_rules.name))), + Block::NameWithSymlink => { + strings.push(meta.name.render(colors, icons, None)); strings.push(meta.indicator.render(&flags)); strings.push(meta.symlink.render(colors)); } @@ -391,6 +392,7 @@ fn detect_size_lengths(metas: &[Meta], flags: &Flags) -> (usize, usize) { (max_value_length, max_unit_size) } + fn detect_name_length(metas: &[Meta], icons: &Icons) -> usize { let mut max_value_length: usize = 0; @@ -403,6 +405,22 @@ fn detect_name_length(metas: &[Meta], icons: &Icons) -> usize { max_value_length } +fn detect_name_with_symlink_length(metas: &[Meta], icons: &Icons) -> usize { + let mut max_value_length: usize = 0; + + for meta in metas { + let mut len = meta.name.name_string(&icons).len(); + if let Some(syml) = meta.symlink.symlink_string() { + len += syml.len(); + } + if len > max_value_length { + max_value_length = len; + } + } + + max_value_length +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/flags.rs b/src/flags.rs index 7c431d7..12bc84a 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -156,24 +156,26 @@ impl Default for Flags { #[derive(Clone, Debug, Copy, PartialEq, Eq)] pub enum Block { - // Type, + FileType, Permission, User, Group, Size, Date, Name, + NameWithSymlink, } impl<'a> From<&'a str> for Block { fn from(block: &'a str) -> Self { match block { - // "type" => Block::Type, + "filetype" => Block::FileType, "permission" => Block::Permission, "user" => Block::User, "group" => Block::Group, "size" => Block::Size, "date" => Block::Date, "name" => Block::Name, + "namewithsymlink" => Block::NameWithSymlink, _ => panic!("invalid \"time\" flag: {}", block), } } diff --git a/src/meta/symlink.rs b/src/meta/symlink.rs index 9767f26..31db9c1 100644 --- a/src/meta/symlink.rs +++ b/src/meta/symlink.rs @@ -43,8 +43,16 @@ impl<'a> From<&'a Path> for SymLink { } impl SymLink { - pub fn render(&self, colors: &Colors) -> ColoredString { + pub fn symlink_string(&self) -> Option { if let Some(ref target) = self.target { + Some(target.to_string()) + } else { + None + } + } + + pub fn render(&self, colors: &Colors) -> ColoredString { + if let Some(target_string) = self.symlink_string() { let elem = if self.valid { &Elem::SymLink } else { @@ -53,7 +61,7 @@ impl SymLink { let strings: &[ColoredString] = &[ ColoredString::from(" \u{21d2} "), // ⇒ - colors.colorize(target.to_string(), elem), + colors.colorize(target_string, elem), ]; let res = ANSIStrings(strings).to_string();