column.rs 1.4 KB
Newer Older
B
Ben S 已提交
1 2 3 4
pub enum Column {
    Permissions,
    FileName,
    FileSize(bool),
B
Ben S 已提交
5
    Blocks,
B
Ben S 已提交
6
    User,
B
Ben S 已提交
7
    Group,
B
Ben S 已提交
8
    HardLinks,
B
Ben S 已提交
9
    Inode,
B
Ben S 已提交
10
}
B
Ben S 已提交
11 12 13 14 15 16 17 18 19 20 21 22

// Each column can pick its own alignment. Usually, numbers are
// right-aligned, and text is left-aligned.

pub enum Alignment {
    Left, Right,
}

impl Column {
    pub fn alignment(&self) -> Alignment {
        match *self {
            FileSize(_) => Right,
B
Ben S 已提交
23
            HardLinks   => Right,
B
Ben S 已提交
24
            Inode       => Right,
B
Ben S 已提交
25
            Blocks      => Right,
B
Ben S 已提交
26 27 28
            _           => Left,
        }
    }
B
Ben S 已提交
29 30 31 32 33 34 35

    pub fn header(&self) -> &'static str {
        match *self {
            Permissions => "Permissions",
            FileName => "Name",
            FileSize(_) => "Size",
            Blocks => "Blocks",
B
Ben S 已提交
36
            User => "User",
B
Ben S 已提交
37 38 39 40 41
            Group => "Group",
            HardLinks => "Links",
            Inode => "inode",
        }
    }
B
Ben S 已提交
42 43 44
}

// An Alignment is used to pad a string to a certain length, letting
B
Ben S 已提交
45 46 47
// 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.
B
Ben S 已提交
48 49

impl Alignment {
B
Ben S 已提交
50
    pub fn pad_string(&self, string: &String, padding: uint) -> String {
B
Ben S 已提交
51
        match *self {
B
Ben S 已提交
52 53
            Left => string.clone().append(" ".to_string().repeat(padding).as_slice()),
            Right => " ".to_string().repeat(padding).append(string.as_slice()),
B
Ben S 已提交
54 55 56 57
        }
    }
}