exa/src/column.rs

76 lines
1.9 KiB
Rust
Raw Normal View History

use std::iter::repeat;
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 { }
pub enum SizeFormat {
DecimalBytes,
BinaryBytes,
2014-12-18 07:04:31 +00:00
JustBytes,
}
impl Copy for SizeFormat { }
// 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 {
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
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
}
}
}
fn spaces(length: uint) -> String {
repeat(" ").take(length).collect()
}
// An Alignment is used to pad a string to a certain length, letting
2014-06-28 14:40:12 +00:00
// it pick which end it puts the text on. It takes the amount of
// padding to apply, rather than the width the text should end up,
// because these strings are usually full of control characters.
impl Alignment {
2014-06-28 14:40:12 +00:00
pub fn pad_string(&self, string: &String, padding: uint) -> String {
match *self {
Alignment::Left => format!("{}{}", string, spaces(padding).as_slice()),
Alignment::Right => format!("{}{}", spaces(padding), string.as_slice()),
}
}
}