diff options
author | Ben S <ogham@bsago.me> | 2015-05-09 23:57:18 +0100 |
---|---|---|
committer | Ben S <ogham@bsago.me> | 2015-05-09 23:57:18 +0100 |
commit | 36116a142095d61ab7f601cb6efda03d2cb8d749 (patch) | |
tree | 8b4057bc23c5a92565610d9067077c87a6e1211b /src/output | |
parent | da49b80c3525db7f0a0aae0fe2ac3e0a9a574cd3 (diff) |
Add colours module, and disable them sometimes
Colours are now disabled when output is not to a terminal. Fixes #53!
This required some internal restructuring - colours are now in their own object that gets passed around everywhere it's needed.
Diffstat (limited to 'src/output')
-rw-r--r-- | src/output/details.rs | 9 | ||||
-rw-r--r-- | src/output/grid.rs | 9 | ||||
-rw-r--r-- | src/output/lines.rs | 14 | ||||
-rw-r--r-- | src/output/mod.rs | 2 |
4 files changed, 24 insertions, 10 deletions
diff --git a/src/output/details.rs b/src/output/details.rs index 5e69081..3870ace 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -1,3 +1,4 @@ +use colours::Colours; use column::{Alignment, Column, Cell}; use feature::Attribute; use dir::Dir; @@ -37,6 +38,8 @@ pub struct Details { /// Whether to show each file's extended attributes. pub xattr: bool, + + pub colours: Colours, } impl Details { @@ -55,7 +58,7 @@ impl Details { /// is present. fn add_files_to_table(&self, table: &mut Table, src: &[File], depth: usize) { for (index, file) in src.iter().enumerate() { - table.add_file(file, depth, index == src.len() - 1); + table.add_file(file, depth, index == src.len() - 1, &self.colours); // There are two types of recursion that exa supports: a tree // view, which is dealt with here, and multiple listings, which is @@ -150,11 +153,11 @@ impl Table { } /// Get the cells for the given file, and add the result to the table. - fn add_file(&mut self, file: &File, depth: usize, last: bool) { + fn add_file(&mut self, file: &File, depth: usize, last: bool, colours: &Colours) { let row = Row { depth: depth, cells: self.cells_for_file(file), - name: file.file_name_view(), + name: file.file_name_view(colours), last: last, attrs: file.xattrs.clone(), children: file.this.is_some(), diff --git a/src/output/grid.rs b/src/output/grid.rs index f87e916..0c2e950 100644 --- a/src/output/grid.rs +++ b/src/output/grid.rs @@ -1,6 +1,8 @@ +use colours::Colours; use column::Alignment::Left; use file::File; -use super::lines::lines_view; +use filetype::file_colour; +use super::lines::Lines; use std::cmp::max; use std::iter::repeat; @@ -9,6 +11,7 @@ use std::iter::repeat; pub struct Grid { pub across: bool, pub console_width: usize, + pub colours: Colours, } impl Grid { @@ -81,7 +84,7 @@ impl Grid { } let ref file = files[num]; - let styled_name = file.file_colour().paint(&file.name).to_string(); + let styled_name = file_colour(&self.colours, file).paint(&file.name).to_string(); if x == widths.len() - 1 { // The final column doesn't need to have trailing spaces print!("{}", styled_name); @@ -96,7 +99,7 @@ impl Grid { } else { // Drop down to lines view if the file names are too big for a grid - lines_view(files); + Lines { colours: self.colours }.view(files); } } } diff --git a/src/output/lines.rs b/src/output/lines.rs index 6649c29..b61494f 100644 --- a/src/output/lines.rs +++ b/src/output/lines.rs @@ -1,8 +1,16 @@ +use colours::Colours; use file::File; +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Lines { + pub colours: Colours, +} + /// The lines view literally just displays each file, line-by-line. -pub fn lines_view(files: &[File]) { - for file in files { - println!("{}", file.file_name_view()); +impl Lines { + pub fn view(&self, files: &[File]) { + for file in files { + println!("{}", file.file_name_view(&self.colours)); + } } } diff --git a/src/output/mod.rs b/src/output/mod.rs index 22b8726..8606d23 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -4,4 +4,4 @@ mod lines; pub use self::grid::Grid; pub use self::details::Details; -pub use self::lines::lines_view; +pub use self::lines::Lines; |