mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 14:12:31 +00:00
Split the rendering of size parts
This commit is contained in:
parent
3eaf763745
commit
f3481ceef6
3 changed files with 54 additions and 30 deletions
|
@ -1,7 +1,7 @@
|
||||||
use crate::color::Colors;
|
use crate::color::Colors;
|
||||||
use crate::flags::{Block, Display, Flags, Layout};
|
use crate::flags::{Block, Display, Flags, Layout};
|
||||||
use crate::icon::Icons;
|
use crate::icon::Icons;
|
||||||
use crate::meta::{FileType, Meta, Size};
|
use crate::meta::{FileType, Meta};
|
||||||
use ansi_term::{ANSIString, ANSIStrings};
|
use ansi_term::{ANSIString, ANSIStrings};
|
||||||
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
|
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
|
||||||
use terminal_size::terminal_size;
|
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::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(
|
||||||
colors,
|
colors,
|
||||||
|
&flags,
|
||||||
padding_rules.size_val,
|
padding_rules.size_val,
|
||||||
padding_rules.size_unit,
|
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::Date => strings.push(meta.date.render(colors, padding_rules.date, &flags)),
|
||||||
Block::Name => {
|
Block::Name => {
|
||||||
if flags.no_symlink {
|
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;
|
let mut max_unit_size: usize = 0;
|
||||||
|
|
||||||
for meta in metas {
|
for meta in metas {
|
||||||
let unit = meta.size.get_unit(flags);
|
let value_len = meta.size.size_string(flags).len();
|
||||||
let value_len = meta.size.render_value(&unit).len();
|
let unit_len = meta.size.unit_string(&flags).len();
|
||||||
let unit_len = Size::render_unit(&unit, &flags).len();
|
|
||||||
|
|
||||||
if value_len > max_value_length {
|
if value_len > max_value_length {
|
||||||
max_value_length = value_len;
|
max_value_length = value_len;
|
||||||
|
|
|
@ -198,6 +198,8 @@ pub enum Block {
|
||||||
User,
|
User,
|
||||||
Group,
|
Group,
|
||||||
Size,
|
Size,
|
||||||
|
SizeValue,
|
||||||
|
SizeUnit,
|
||||||
Date,
|
Date,
|
||||||
Name,
|
Name,
|
||||||
}
|
}
|
||||||
|
@ -209,6 +211,8 @@ impl<'a> From<&'a str> for Block {
|
||||||
"user" => Block::User,
|
"user" => Block::User,
|
||||||
"group" => Block::Group,
|
"group" => Block::Group,
|
||||||
"size" => Block::Size,
|
"size" => Block::Size,
|
||||||
|
"size_value" => Block::SizeValue,
|
||||||
|
"size_unit" => Block::SizeUnit,
|
||||||
"date" => Block::Date,
|
"date" => Block::Date,
|
||||||
"name" => Block::Name,
|
"name" => Block::Name,
|
||||||
_ => panic!("invalid \"time\" flag: {}", block),
|
_ => panic!("invalid \"time\" flag: {}", block),
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::color::{ColoredString, Colors, Elem};
|
use crate::color::{ColoredString, Colors, Elem};
|
||||||
use crate::flags::{Flags, SizeFlag};
|
use crate::flags::{Flags, SizeFlag};
|
||||||
|
use ansi_term::ANSIStrings;
|
||||||
use std::fs::Metadata;
|
use std::fs::Metadata;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
@ -50,47 +51,57 @@ impl Size {
|
||||||
pub fn render(
|
pub fn render(
|
||||||
&self,
|
&self,
|
||||||
colors: &Colors,
|
colors: &Colors,
|
||||||
value_alignment: usize,
|
|
||||||
unit_alignment: usize,
|
|
||||||
flags: &Flags,
|
flags: &Flags,
|
||||||
|
val_alignment: usize,
|
||||||
|
unit_alignment: usize,
|
||||||
) -> ColoredString {
|
) -> 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 mut left_pad = String::with_capacity(val_alignment - val_content.len());
|
||||||
|
for _ in 0..left_pad.capacity() {
|
||||||
let value_str = self.render_value(&unit);
|
left_pad.push(' ');
|
||||||
let unit_str = Size::render_unit(&unit, &flags);
|
|
||||||
|
|
||||||
for _ in 0..(value_alignment - value_str.len()) {
|
|
||||||
content.push(' ');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
content += &self.render_value(&unit);
|
let mut right_pad = String::with_capacity(unit_alignment - unit_content.len());
|
||||||
if flags.size == SizeFlag::Default {
|
for _ in 0..right_pad.capacity() {
|
||||||
content.push(' ');
|
right_pad.push(' ');
|
||||||
}
|
|
||||||
content += &Size::render_unit(&unit, &flags);
|
|
||||||
|
|
||||||
for _ in 0..(unit_alignment - unit_str.len()) {
|
|
||||||
content.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 {
|
fn paint(&self, colors: &Colors, flags: &Flags, content: String) -> ColoredString {
|
||||||
if unit == &Unit::None {
|
let unit = self.get_unit(flags);
|
||||||
|
|
||||||
|
if unit == Unit::None {
|
||||||
colors.colorize(content, &Elem::NonFile)
|
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)
|
colors.colorize(content, &Elem::FileSmall)
|
||||||
} else if unit == &Unit::Mega {
|
} else if unit == Unit::Mega {
|
||||||
colors.colorize(content, &Elem::FileMedium)
|
colors.colorize(content, &Elem::FileMedium)
|
||||||
} else {
|
} else {
|
||||||
colors.colorize(content, &Elem::FileLarge)
|
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 {
|
match unit {
|
||||||
Unit::None => "".to_string(),
|
Unit::None => "".to_string(),
|
||||||
Unit::Byte => self.bytes.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 {
|
match flags.size {
|
||||||
SizeFlag::Default => match unit {
|
SizeFlag::Default => match unit {
|
||||||
Unit::None => String::from("-"),
|
Unit::None => String::from("-"),
|
||||||
|
|
Loading…
Reference in a new issue