column.rs 1.7 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 52 53 54
        let mut str = String::new();
        match *self {
            Left => {
                str.push_str(string.as_slice());
B
Ben S 已提交
55
                for _ in range(0, padding) {
B
Ben S 已提交
56 57 58 59 60
                    str.push_char(' ');
                }
            }

            Right => {
B
Ben S 已提交
61
                for _ in range(0, padding) {
B
Ben S 已提交
62 63 64 65 66 67 68 69 70
                    str.push_char(' ');
                }
                str.push_str(string.as_slice());
            },
        }
        return str;
    }
}