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

3 4
use ansi_term::Style;

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

B
Ben S 已提交
17 18
impl Copy for Column { }

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

impl Copy for SizeFormat { }

B
Ben S 已提交
28 29
/// Each column can pick its own **Alignment**. Usually, numbers are
/// right-aligned, and text is left-aligned.
B
Ben S 已提交
30 31 32 33
pub enum Alignment {
    Left, Right,
}

B
Ben S 已提交
34 35
impl Copy for Alignment { }

B
Ben S 已提交
36
impl Column {
B
Ben S 已提交
37 38

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

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

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

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

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