exa/src/column.rs

98 lines
2.5 KiB
Rust
Raw Normal View History

use std::iter::repeat;
use ansi_term::Style;
2015-01-12 23:31:30 +00:00
#[derive(PartialEq, Show)]
pub enum Column {
Permissions,
FileName,
FileSize(SizeFormat),
2014-06-22 07:09:16 +00:00
Blocks,
User,
Group,
HardLinks,
2014-06-22 06:44:00 +00:00
Inode,
}
2014-12-14 18:22:56 +00:00
impl Copy for Column { }
2015-01-12 23:31:30 +00:00
#[derive(PartialEq, Show)]
pub enum SizeFormat {
DecimalBytes,
BinaryBytes,
2014-12-18 07:04:31 +00:00
JustBytes,
}
impl Copy for SizeFormat { }
2015-01-24 12:38:05 +00:00
/// Each column can pick its own **Alignment**. Usually, numbers are
/// right-aligned, and text is left-aligned.
pub enum Alignment {
Left, Right,
}
2014-12-14 18:22:56 +00:00
impl Copy for Alignment { }
impl Column {
2015-01-24 12:38:05 +00:00
/// Get the alignment this column should use.
pub fn alignment(&self) -> Alignment {
match *self {
2014-11-23 21:29:11 +00:00
Column::FileSize(_) => Alignment::Right,
Column::HardLinks => Alignment::Right,
Column::Inode => Alignment::Right,
Column::Blocks => Alignment::Right,
_ => Alignment::Left,
}
}
2014-06-23 17:26:35 +00:00
2015-01-24 12:38:05 +00:00
/// Get the text that should be printed at the top, when the user elects
/// to have a header row printed.
2014-06-23 17:26:35 +00:00
pub fn header(&self) -> &'static str {
match *self {
2014-11-23 21:29:11 +00:00
Column::Permissions => "Permissions",
2014-11-24 02:12:52 +00:00
Column::FileName => "Name",
2014-11-23 21:29:11 +00:00
Column::FileSize(_) => "Size",
2014-11-24 02:12:52 +00:00
Column::Blocks => "Blocks",
Column::User => "User",
Column::Group => "Group",
Column::HardLinks => "Links",
Column::Inode => "inode",
2014-06-23 17:26:35 +00:00
}
}
}
2015-01-24 12:38:05 +00:00
/// Pad a string with the given number of spaces.
fn spaces(length: usize) -> String {
repeat(" ").take(length).collect()
}
impl Alignment {
2015-01-24 12:38:05 +00:00
/// Pad a string with the given alignment and number of spaces.
///
/// This doesn't take the width the string *should* be, rather the number
/// of spaces to add: this is because the strings are usually full of
/// invisible control characters, so getting the displayed width of the
/// string is not as simple as just getting its length.
pub fn pad_string(&self, string: &String, padding: usize) -> String {
match *self {
Alignment::Left => format!("{}{}", string, spaces(padding).as_slice()),
Alignment::Right => format!("{}{}", spaces(padding), string.as_slice()),
}
}
}
pub struct Cell {
pub length: usize,
pub text: String,
}
impl Cell {
pub fn paint(style: Style, string: &str) -> Cell {
Cell {
text: style.paint(string).to_string(),
length: string.len(),
}
}
}