exa/src/column.rs

93 lines
2.6 KiB
Rust
Raw Normal View History

use std::iter::repeat;
use ansi_term::Style;
use options::{SizeFormat, TimeType};
2015-01-26 00:27:06 +00:00
#[derive(PartialEq, Debug, Copy)]
pub enum Column {
Permissions,
FileSize(SizeFormat),
Timestamp(TimeType, i64),
2014-06-22 07:09:16 +00:00
Blocks,
User,
Group,
HardLinks,
2014-06-22 06:44:00 +00:00
Inode,
GitStatus,
}
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.
2015-01-26 00:27:06 +00:00
#[derive(Copy)]
pub enum Alignment {
Left, Right,
}
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,
Column::GitStatus => Alignment::Right,
2014-11-23 21:29:11 +00:00
_ => 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 {
Column::Permissions => "Permissions",
Column::FileSize(_) => "Size",
Column::Timestamp(t, _) => t.header(),
Column::Blocks => "Blocks",
Column::User => "User",
Column::Group => "Group",
Column::HardLinks => "Links",
Column::Inode => "inode",
Column::GitStatus => "Git",
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()),
}
}
}
2015-01-25 13:04:15 +00:00
#[derive(PartialEq, Debug)]
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.graphemes(true).count(),
}
}
}