Split the rendering of size parts

This commit is contained in:
Pierre Peltier 2019-10-23 16:47:10 +02:00 committed by Abin Simon
parent 3eaf763745
commit f3481ceef6
3 changed files with 54 additions and 30 deletions

View file

@ -1,7 +1,7 @@
use crate::color::Colors;
use crate::flags::{Block, Display, Flags, Layout};
use crate::icon::Icons;
use crate::meta::{FileType, Meta, Size};
use crate::meta::{FileType, Meta};
use ansi_term::{ANSIString, ANSIStrings};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::terminal_size;
@ -306,10 +306,12 @@ fn get_long_output(
Block::Group => strings.push(meta.owner.render_group(colors, padding_rules.group)),
Block::Size => strings.push(meta.size.render(
colors,
&flags,
padding_rules.size_val,
padding_rules.size_unit,
&flags,
)),
Block::SizeValue => strings.push(meta.size.render_value(colors, flags)),
Block::SizeUnit => strings.push(meta.size.render_unit(colors, flags)),
Block::Date => strings.push(meta.date.render(colors, padding_rules.date, &flags)),
Block::Name => {
if flags.no_symlink {
@ -413,9 +415,8 @@ fn detect_size_lengths(metas: &[Meta], flags: &Flags) -> (usize, usize) {
let mut max_unit_size: usize = 0;
for meta in metas {
let unit = meta.size.get_unit(flags);
let value_len = meta.size.render_value(&unit).len();
let unit_len = Size::render_unit(&unit, &flags).len();
let value_len = meta.size.size_string(flags).len();
let unit_len = meta.size.unit_string(&flags).len();
if value_len > max_value_length {
max_value_length = value_len;

View file

@ -198,6 +198,8 @@ pub enum Block {
User,
Group,
Size,
SizeValue,
SizeUnit,
Date,
Name,
}
@ -209,6 +211,8 @@ impl<'a> From<&'a str> for Block {
"user" => Block::User,
"group" => Block::Group,
"size" => Block::Size,
"size_value" => Block::SizeValue,
"size_unit" => Block::SizeUnit,
"date" => Block::Date,
"name" => Block::Name,
_ => panic!("invalid \"time\" flag: {}", block),

View file

@ -1,5 +1,6 @@
use crate::color::{ColoredString, Colors, Elem};
use crate::flags::{Flags, SizeFlag};
use ansi_term::ANSIStrings;
use std::fs::Metadata;
#[derive(Clone, Debug, PartialEq, Eq)]
@ -50,47 +51,57 @@ impl Size {
pub fn render(
&self,
colors: &Colors,
value_alignment: usize,
unit_alignment: usize,
flags: &Flags,
val_alignment: usize,
unit_alignment: usize,
) -> ColoredString {
let mut content = String::with_capacity(value_alignment + unit_alignment + 1);
let val_content = self.render_value(colors, flags);
let unit_content = self.render_unit(colors, flags);
let unit = self.get_unit(flags);
let value_str = self.render_value(&unit);
let unit_str = Size::render_unit(&unit, &flags);
for _ in 0..(value_alignment - value_str.len()) {
content.push(' ');
let mut left_pad = String::with_capacity(val_alignment - val_content.len());
for _ in 0..left_pad.capacity() {
left_pad.push(' ');
}
content += &self.render_value(&unit);
if flags.size == SizeFlag::Default {
content.push(' ');
}
content += &Size::render_unit(&unit, &flags);
for _ in 0..(unit_alignment - unit_str.len()) {
content.push(' ');
let mut right_pad = String::with_capacity(unit_alignment - unit_content.len());
for _ in 0..right_pad.capacity() {
right_pad.push(' ');
}
self.paint(&unit, colors, content)
let strings: &[ColoredString] = &[
ColoredString::from(left_pad),
val_content,
unit_content,
ColoredString::from(right_pad),
];
let res = ANSIStrings(strings).to_string();
ColoredString::from(res)
}
fn paint(&self, unit: &Unit, colors: &Colors, content: String) -> ColoredString {
if unit == &Unit::None {
fn paint(&self, colors: &Colors, flags: &Flags, content: String) -> ColoredString {
let unit = self.get_unit(flags);
if unit == Unit::None {
colors.colorize(content, &Elem::NonFile)
} else if unit == &Unit::Byte || unit == &Unit::Kilo {
} else if unit == Unit::Byte || unit == Unit::Kilo {
colors.colorize(content, &Elem::FileSmall)
} else if unit == &Unit::Mega {
} else if unit == Unit::Mega {
colors.colorize(content, &Elem::FileMedium)
} else {
colors.colorize(content, &Elem::FileLarge)
}
}
pub fn render_value(&self, unit: &Unit) -> String {
pub fn render_value(&self, colors: &Colors, flags: &Flags) -> ColoredString {
let content = self.size_string(flags);
self.paint(colors, flags, content)
}
pub fn size_string(&self, flags: &Flags) -> String {
let unit = self.get_unit(flags);
match unit {
Unit::None => "".to_string(),
Unit::Byte => self.bytes.to_string(),
@ -108,7 +119,15 @@ impl Size {
}
}
pub fn render_unit(unit: &Unit, flags: &Flags) -> String {
pub fn render_unit(&self, colors: &Colors, flags: &Flags) -> ColoredString {
let content = self.unit_string(flags);
self.paint(colors, flags, content)
}
pub fn unit_string(&self, flags: &Flags) -> String {
let unit = self.get_unit(flags);
match flags.size {
SizeFlag::Default => match unit {
Unit::None => String::from("-"),