column.rs 2.5 KB
Newer Older
B
Ben S 已提交
1 2
use std::iter::repeat;

3 4
use ansi_term::Style;

B
Ben S 已提交
5
#[derive(PartialEq, Debug, Copy)]
B
Ben S 已提交
6 7
pub enum Column {
    Permissions,
8
    FileSize(SizeFormat),
B
Ben S 已提交
9
    Blocks,
B
Ben S 已提交
10
    User,
B
Ben S 已提交
11
    Group,
B
Ben S 已提交
12
    HardLinks,
B
Ben S 已提交
13
    Inode,
B
Ben S 已提交
14 15

    GitStatus,
B
Ben S 已提交
16
}
B
Ben S 已提交
17

B
Ben S 已提交
18
#[derive(PartialEq, Debug, Copy)]
19 20 21
pub enum SizeFormat {
    DecimalBytes,
    BinaryBytes,
B
Ben S 已提交
22
    JustBytes,
23 24
}

B
Ben S 已提交
25 26
/// Each column can pick its own **Alignment**. Usually, numbers are
/// right-aligned, and text is left-aligned.
B
Ben S 已提交
27
#[derive(Copy)]
B
Ben S 已提交
28 29 30 31 32
pub enum Alignment {
    Left, Right,
}

impl Column {
B
Ben S 已提交
33 34

    /// Get the alignment this column should use.
B
Ben S 已提交
35 36
    pub fn alignment(&self) -> Alignment {
        match *self {
B
Ben S 已提交
37 38 39 40
            Column::FileSize(_) => Alignment::Right,
            Column::HardLinks   => Alignment::Right,
            Column::Inode       => Alignment::Right,
            Column::Blocks      => Alignment::Right,
B
Ben S 已提交
41
            Column::GitStatus   => Alignment::Right,
B
Ben S 已提交
42
            _                   => Alignment::Left,
B
Ben S 已提交
43 44
        }
    }
B
Ben S 已提交
45

B
Ben S 已提交
46 47
    /// Get the text that should be printed at the top, when the user elects
    /// to have a header row printed.
B
Ben S 已提交
48 49
    pub fn header(&self) -> &'static str {
        match *self {
B
Ben S 已提交
50 51
            Column::Permissions => "Permissions",
            Column::FileSize(_) => "Size",
B
Ben S 已提交
52 53 54 55 56
            Column::Blocks      => "Blocks",
            Column::User        => "User",
            Column::Group       => "Group",
            Column::HardLinks   => "Links",
            Column::Inode       => "inode",
B
Ben S 已提交
57
            Column::GitStatus   => "Git",
B
Ben S 已提交
58 59
        }
    }
B
Ben S 已提交
60 61
}

B
Ben S 已提交
62
/// Pad a string with the given number of spaces.
B
Benjamin Sago 已提交
63
fn spaces(length: usize) -> String {
B
Ben S 已提交
64 65 66
    repeat(" ").take(length).collect()
}

B
Ben S 已提交
67
impl Alignment {
B
Ben S 已提交
68 69 70 71 72 73
    /// 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.
B
Benjamin Sago 已提交
74
    pub fn pad_string(&self, string: &String, padding: usize) -> String {
B
Ben S 已提交
75
        match *self {
B
Ben S 已提交
76 77
            Alignment::Left  => format!("{}{}", string, spaces(padding).as_slice()),
            Alignment::Right => format!("{}{}", spaces(padding), string.as_slice()),
B
Ben S 已提交
78 79 80
        }
    }
}
81

B
Benjamin Sago 已提交
82
#[derive(PartialEq, Debug)]
83 84 85 86 87 88 89 90 91 92 93 94 95
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(),
        }
    }
}