mod.rs 2.9 KB
Newer Older
1 2 3 4
mod groups;
mod permissions;
mod size;
mod users;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

use output::cell::{TextCell, DisplayWidth};
use output::colours::Colours;
use fs::fields as f;

use datetime::{LocalDateTime, TimeZone, DatePiece};
use datetime::fmt::DateFormat;
use locale;


impl f::Links {
    pub fn render(&self, colours: &Colours, numeric: &locale::Numeric) -> TextCell {
        let style = if self.multiple { colours.links.multi_link_file }
                                else { colours.links.normal };

        TextCell::paint(style, numeric.format_int(self.count))
    }
}


impl f::Blocks {
    pub fn render(&self, colours: &Colours) -> TextCell {
        match *self {
            f::Blocks::Some(ref blk)  => TextCell::paint(colours.blocks, blk.to_string()),
            f::Blocks::None           => TextCell::blank(colours.punctuation),
        }
    }
}


impl f::Inode {
    pub fn render(&self, colours: &Colours) -> TextCell {
        TextCell::paint(colours.inode, self.0.to_string())
    }
}


#[allow(trivial_numeric_casts)]
impl f::Time {
    pub fn render(&self, colours: &Colours, tz: &Option<TimeZone>,
                          date_and_time: &DateFormat<'static>, date_and_year: &DateFormat<'static>,
                          time: &locale::Time, current_year: i64) -> TextCell {

        // TODO(ogham): This method needs some serious de-duping!
        // zoned and local times have different types at the moment,
        // so it's tricky.

        if let Some(ref tz) = *tz {
            let date = tz.to_zoned(LocalDateTime::at(self.0 as i64));

            let datestamp = if date.year() == current_year {
                date_and_time.format(&date, time)
            }
            else {
                date_and_year.format(&date, time)
            };

            TextCell::paint(colours.date, datestamp)
        }
        else {
            let date = LocalDateTime::at(self.0 as i64);

            let datestamp = if date.year() == current_year {
                date_and_time.format(&date, time)
            }
            else {
                date_and_year.format(&date, time)
            };

            TextCell::paint(colours.date, datestamp)
        }
    }
}


impl f::Git {
    pub fn render(&self, colours: &Colours) -> TextCell {
        let git_char = |status| match status {
            &f::GitStatus::NotModified  => colours.punctuation.paint("-"),
            &f::GitStatus::New          => colours.git.new.paint("N"),
            &f::GitStatus::Modified     => colours.git.modified.paint("M"),
            &f::GitStatus::Deleted      => colours.git.deleted.paint("D"),
            &f::GitStatus::Renamed      => colours.git.renamed.paint("R"),
            &f::GitStatus::TypeChange   => colours.git.typechange.paint("T"),
        };

        TextCell {
            width: DisplayWidth::from(2),
            contents: vec![
                git_char(&self.staged),
                git_char(&self.unstaged)
            ].into(),
        }
    }
}