File size colours on a scale

This adds an option (always on at the moment) to use a colour scale of green to yellow to orange for the file size field instead of always green. See #65.
This commit is contained in:
Ben S 2016-10-30 15:50:09 +00:00
parent 91e8ef5c78
commit 86065f832d
2 changed files with 33 additions and 8 deletions

View File

@ -34,12 +34,13 @@ impl View {
}
else {
let term_colours = try!(TerminalColours::deduce(matches));
let scale = true;
let colours = match term_colours {
TerminalColours::Always => Colours::colourful(),
TerminalColours::Always => Colours::colourful(scale),
TerminalColours::Never => Colours::plain(),
TerminalColours::Automatic => {
if dimensions().is_some() {
Colours::colourful()
Colours::colourful(scale)
}
else {
Colours::plain()
@ -84,12 +85,13 @@ impl View {
let other_options_scan = || {
let term_colours = try!(TerminalColours::deduce(matches));
let term_width = try!(TerminalWidth::deduce());
let scale = true;
if let Some(&width) = term_width.as_ref() {
let colours = match term_colours {
TerminalColours::Always => Colours::colourful(),
TerminalColours::Always => Colours::colourful(scale),
TerminalColours::Never => Colours::plain(),
TerminalColours::Automatic => Colours::colourful(),
TerminalColours::Automatic => Colours::colourful(scale),
};
if matches.opt_present("oneline") {
@ -132,7 +134,7 @@ impl View {
// fallback to the lines view.
let colours = match term_colours {
TerminalColours::Always => Colours::colourful(),
TerminalColours::Always => Colours::colourful(scale),
TerminalColours::Never => Colours::plain(),
TerminalColours::Automatic => Colours::plain(),
};

View File

@ -4,6 +4,8 @@ use ansi_term::Colour::{Red, Green, Yellow, Blue, Cyan, Purple, Fixed};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Colours {
pub scale: bool,
pub filetypes: FileTypes,
pub perms: Permissions,
pub size: Size,
@ -96,8 +98,10 @@ impl Colours {
Colours::default()
}
pub fn colourful() -> Colours {
pub fn colourful(scale: bool) -> Colours {
Colours {
scale: scale,
filetypes: FileTypes {
normal: Style::default(),
directory: Blue.bold(),
@ -170,7 +174,26 @@ impl Colours {
}
}
pub fn file_size(&self, _size: u64) -> Style {
self.size.numbers
pub fn file_size(&self, size: u64) -> Style {
if self.scale {
if size < 1024 {
Fixed(118).normal()
}
else if size < 1024 * 1024 {
Fixed(190).normal()
}
else if size < 1024 * 1024 * 1024 {
Fixed(226).normal()
}
else if size < 1024 * 1024 * 1024 * 1024 {
Fixed(220).normal()
}
else {
Fixed(214).normal()
}
}
else {
self.size.numbers
}
}
}