have most of it working

This commit is contained in:
Abin Simon 2019-05-04 16:26:27 +05:30 committed by Pierre Peltier
parent 86d4fb8215
commit 61b8f5d7fd
4 changed files with 40 additions and 12 deletions

View file

@ -171,8 +171,8 @@ pub fn build() -> App<'static, 'static> {
.multiple(true) .multiple(true)
.number_of_values(1) .number_of_values(1)
.require_delimiter(true) .require_delimiter(true)
.possible_values(&["permission", "user", "group", "size", "date", "name"]) .possible_values(&["filetype","permission", "user", "group", "size", "date", "name", "namewithsymlink"])
.default_value("permission,user,group,size,date,name") .default_value("filetype,permission,user,group,size,date,namewithsymlink")
.help("Specify the blocks that will be displayed and in what order"), .help("Specify the blocks that will be displayed and in what order"),
) )
.arg( .arg(

View file

@ -19,6 +19,7 @@ struct PaddingRules {
size: (usize, usize), size: (usize, usize),
date: usize, date: usize,
name: usize, name: usize,
name_with_symlink: usize,
} }
pub fn one_line(metas: Vec<Meta>, flags: &Flags, colors: &Colors, icons: &Icons) -> String { pub fn one_line(metas: Vec<Meta>, flags: &Flags, colors: &Colors, icons: &Icons) -> String {
@ -57,6 +58,7 @@ fn inner_display_one_line(
size: detect_size_lengths(&metas, &flags), size: detect_size_lengths(&metas, &flags),
date: detect_date_length(&metas, &flags), date: detect_date_length(&metas, &flags),
name: detect_name_length(&metas, &icons), 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<ANSIString> = Vec::new(); let mut strings: Vec<ANSIString> = Vec::new();
for block in flags.blocks.iter() { for block in flags.blocks.iter() {
match block { match block {
Block::Permission => { Block::FileType => strings.push(meta.file_type.render(colors)),
strings.push(meta.file_type.render(colors)); Block::Permission => strings.push(meta.permissions.render(colors)),
strings.push(meta.permissions.render(colors));
}
Block::User => strings.push(meta.owner.render_user(colors, padding_rules.user)), 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::Group => strings.push(meta.owner.render_group(colors, padding_rules.group)),
Block::Size => strings.push(meta.size.render( Block::Size => strings.push(meta.size.render(
@ -294,8 +294,9 @@ fn get_long_output(
&flags, &flags,
)), )),
Block::Date => strings.push(meta.date.render(colors, padding_rules.date, &flags)), Block::Date => strings.push(meta.date.render(colors, padding_rules.date, &flags)),
Block::Name => { Block::Name => strings.push(meta.name.render(colors, icons, Some(padding_rules.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.indicator.render(&flags));
strings.push(meta.symlink.render(colors)); 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) (max_value_length, max_unit_size)
} }
fn detect_name_length(metas: &[Meta], icons: &Icons) -> usize { fn detect_name_length(metas: &[Meta], icons: &Icons) -> usize {
let mut max_value_length: usize = 0; let mut max_value_length: usize = 0;
@ -403,6 +405,22 @@ fn detect_name_length(metas: &[Meta], icons: &Icons) -> usize {
max_value_length 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -156,24 +156,26 @@ impl Default for Flags {
#[derive(Clone, Debug, Copy, PartialEq, Eq)] #[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum Block { pub enum Block {
// Type, FileType,
Permission, Permission,
User, User,
Group, Group,
Size, Size,
Date, Date,
Name, Name,
NameWithSymlink,
} }
impl<'a> From<&'a str> for Block { impl<'a> From<&'a str> for Block {
fn from(block: &'a str) -> Self { fn from(block: &'a str) -> Self {
match block { match block {
// "type" => Block::Type, "filetype" => Block::FileType,
"permission" => Block::Permission, "permission" => Block::Permission,
"user" => Block::User, "user" => Block::User,
"group" => Block::Group, "group" => Block::Group,
"size" => Block::Size, "size" => Block::Size,
"date" => Block::Date, "date" => Block::Date,
"name" => Block::Name, "name" => Block::Name,
"namewithsymlink" => Block::NameWithSymlink,
_ => panic!("invalid \"time\" flag: {}", block), _ => panic!("invalid \"time\" flag: {}", block),
} }
} }

View file

@ -43,8 +43,16 @@ impl<'a> From<&'a Path> for SymLink {
} }
impl SymLink { impl SymLink {
pub fn render(&self, colors: &Colors) -> ColoredString { pub fn symlink_string(&self) -> Option<String> {
if let Some(ref target) = self.target { 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 { let elem = if self.valid {
&Elem::SymLink &Elem::SymLink
} else { } else {
@ -53,7 +61,7 @@ impl SymLink {
let strings: &[ColoredString] = &[ let strings: &[ColoredString] = &[
ColoredString::from(" \u{21d2} "), // ⇒ ColoredString::from(" \u{21d2} "), // ⇒
colors.colorize(target.to_string(), elem), colors.colorize(target_string, elem),
]; ];
let res = ANSIStrings(strings).to_string(); let res = ANSIStrings(strings).to_string();