Skip to content

Commit

Permalink
File size colours on a scale
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ogham committed Oct 30, 2016
1 parent 91e8ef5 commit 86065f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/options/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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(),
};
Expand Down
29 changes: 26 additions & 3 deletions src/output/colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 86065f8

Please sign in to comment.