mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 06:02:36 +00:00
Add the Size.render method
This commit is contained in:
parent
8585369bf1
commit
25c615002e
3 changed files with 36 additions and 33 deletions
|
@ -109,8 +109,8 @@ impl<'a> Core<'a> {
|
|||
self.formatter.format_permissions(&meta),
|
||||
self.formatter.format_user(&meta.user, max_user_length),
|
||||
self.formatter.format_group(&meta.group, max_group_length),
|
||||
self.formatter
|
||||
.format_size(&meta, max_size_value_length, max_size_unit_length),
|
||||
meta.size
|
||||
.render(max_size_value_length, max_size_unit_length),
|
||||
self.formatter.format_date(&meta),
|
||||
self.formatter.format_name(&meta),
|
||||
self.formatter.format_symlink(&meta),
|
||||
|
|
|
@ -117,33 +117,4 @@ impl Formatter {
|
|||
|
||||
content
|
||||
}
|
||||
|
||||
pub fn format_size(
|
||||
&self,
|
||||
meta: &Meta,
|
||||
max_value_length: usize,
|
||||
max_unit_size: usize,
|
||||
) -> String {
|
||||
let mut content = String::with_capacity(max_value_length + max_unit_size + 1);
|
||||
|
||||
for _ in 0..(max_value_length - meta.size.render_value().len()) {
|
||||
content.push(' ');
|
||||
}
|
||||
|
||||
content += meta.size.render_value().as_str();
|
||||
content.push(' ');
|
||||
content += meta.size.render_unit().as_str();
|
||||
|
||||
for _ in 0..(max_unit_size - meta.size.render_unit().len()) {
|
||||
content.push(' ');
|
||||
}
|
||||
|
||||
if meta.metadata.len() < 10 * 1044 * 1024 {
|
||||
Colors[&Elem::FileSmall].paint(content).to_string()
|
||||
} else if meta.metadata.len() < 100 * 1044 * 1024 {
|
||||
Colors[&Elem::FileMedium].paint(content).to_string()
|
||||
} else {
|
||||
Colors[&Elem::FileLarge].paint(content).to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use color::{Colors, Elem};
|
||||
use std::fs::Metadata;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Unit {
|
||||
Byte,
|
||||
Kilo,
|
||||
|
@ -9,7 +10,7 @@ pub enum Unit {
|
|||
Tera,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Size {
|
||||
value: usize,
|
||||
unit: Unit,
|
||||
|
@ -49,6 +50,37 @@ impl<'a> From<&'a Metadata> for Size {
|
|||
}
|
||||
|
||||
impl Size {
|
||||
pub fn render(&self, value_alignment: usize, unit_alignment: usize) -> String {
|
||||
let mut content = String::with_capacity(value_alignment + unit_alignment + 1);
|
||||
|
||||
let value = self.render_value();
|
||||
let unit = self.render_unit();
|
||||
|
||||
for _ in 0..(value_alignment - value.len()) {
|
||||
content.push(' ');
|
||||
}
|
||||
|
||||
content += &self.render_value();
|
||||
content.push(' ');
|
||||
content += &self.render_unit();
|
||||
|
||||
for _ in 0..(unit_alignment - unit.len()) {
|
||||
content.push(' ');
|
||||
}
|
||||
|
||||
self.paint(content)
|
||||
}
|
||||
|
||||
fn paint(&self, content: String) -> String {
|
||||
if self.unit == Unit::Byte || self.unit == Unit::Kilo {
|
||||
Colors[&Elem::FileSmall].paint(content).to_string()
|
||||
} else if self.unit == Unit::Mega {
|
||||
Colors[&Elem::FileMedium].paint(content).to_string()
|
||||
} else {
|
||||
Colors[&Elem::FileLarge].paint(content).to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_value(&self) -> String {
|
||||
let size_str = (self.value as f32 / 1024.0).to_string();
|
||||
|
||||
|
|
Loading…
Reference in a new issue