2014-12-24 04:31:59 +00:00
|
|
|
use std::iter::repeat;
|
|
|
|
|
2015-01-24 17:26:26 +00:00
|
|
|
use ansi_term::Style;
|
|
|
|
|
2015-02-09 16:33:27 +00:00
|
|
|
use options::{SizeFormat, TimeType};
|
2015-02-05 14:39:56 +00:00
|
|
|
|
2015-01-26 00:27:06 +00:00
|
|
|
#[derive(PartialEq, Debug, Copy)]
|
2014-05-04 20:33:14 +00:00
|
|
|
pub enum Column {
|
|
|
|
Permissions,
|
2014-12-18 07:00:31 +00:00
|
|
|
FileSize(SizeFormat),
|
2015-02-09 18:14:05 +00:00
|
|
|
Timestamp(TimeType, i64),
|
2014-06-22 07:09:16 +00:00
|
|
|
Blocks,
|
2014-06-26 22:26:27 +00:00
|
|
|
User,
|
2014-05-05 10:29:50 +00:00
|
|
|
Group,
|
2014-06-22 06:40:40 +00:00
|
|
|
HardLinks,
|
2014-06-22 06:44:00 +00:00
|
|
|
Inode,
|
2015-01-27 15:01:17 +00:00
|
|
|
|
|
|
|
GitStatus,
|
2014-05-04 20:33:14 +00:00
|
|
|
}
|
2014-06-03 20:14:35 +00:00
|
|
|
|
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)]
|
2014-06-03 20:14:35 +00:00
|
|
|
pub enum Alignment {
|
|
|
|
Left, Right,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Column {
|
2015-01-24 12:38:05 +00:00
|
|
|
|
|
|
|
/// Get the alignment this column should use.
|
2014-06-03 20:14:35 +00:00
|
|
|
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,
|
2015-01-27 15:01:17 +00:00
|
|
|
Column::GitStatus => Alignment::Right,
|
2014-11-23 21:29:11 +00:00
|
|
|
_ => Alignment::Left,
|
2014-06-03 20:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2015-02-09 18:14:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-06-03 20:14:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 12:38:05 +00:00
|
|
|
/// Pad a string with the given number of spaces.
|
2015-01-12 00:31:24 +00:00
|
|
|
fn spaces(length: usize) -> String {
|
2014-12-24 04:31:59 +00:00
|
|
|
repeat(" ").take(length).collect()
|
|
|
|
}
|
|
|
|
|
2014-06-03 20:14:35 +00:00
|
|
|
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.
|
2015-01-12 00:31:24 +00:00
|
|
|
pub fn pad_string(&self, string: &String, padding: usize) -> String {
|
2014-06-03 20:14:35 +00:00
|
|
|
match *self {
|
2014-12-24 04:31:59 +00:00
|
|
|
Alignment::Left => format!("{}{}", string, spaces(padding).as_slice()),
|
|
|
|
Alignment::Right => format!("{}{}", spaces(padding), string.as_slice()),
|
2014-06-03 20:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 17:26:26 +00:00
|
|
|
|
2015-01-25 13:04:15 +00:00
|
|
|
#[derive(PartialEq, Debug)]
|
2015-01-24 17:26:26 +00:00
|
|
|
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(),
|
2015-02-22 16:21:57 +00:00
|
|
|
length: string.graphemes(true).count(),
|
2015-01-24 17:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|