From 1b3492ce45552fa4335374ed6d3692992d00278b Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Sun, 20 Dec 2015 17:56:57 +1100 Subject: [PATCH] Move colours module into output This commit moves the colours module to be a sub-module of the output one. This makes sense because finding which colour a certain file should be is only done during output, and (I think) the only places that the `Colours` struct's fields are ever queried is from the output module. The only casualty was that the `file_colour` from the filetype module had to be moved, as determining colours is no longer part of that module - only determining filetype is. So it now reflects its name! --- src/filetype.rs | 26 +------------------------- src/main.rs | 1 - src/options.rs | 2 +- src/{ => output}/colours.rs | 0 src/output/details.rs | 2 +- src/output/grid.rs | 10 +++++----- src/output/lines.rs | 6 +++--- src/output/mod.rs | 29 +++++++++++++++++++++++++---- 8 files changed, 36 insertions(+), 40 deletions(-) rename src/{ => output}/colours.rs (100%) diff --git a/src/filetype.rs b/src/filetype.rs index ed2f369..b8427d4 100644 --- a/src/filetype.rs +++ b/src/filetype.rs @@ -1,31 +1,7 @@ -use ansi_term::Style; - use file::File; -use colours::Colours; -pub fn file_colour(colours: &Colours, file: &File) -> Style { - match file { - f if f.is_directory() => colours.filetypes.directory, - f if f.is_executable_file() => colours.filetypes.executable, - f if f.is_link() => colours.filetypes.symlink, - f if !f.is_file() => colours.filetypes.special, - f if f.is_immediate() => colours.filetypes.immediate, - f if f.is_image() => colours.filetypes.image, - f if f.is_video() => colours.filetypes.video, - f if f.is_music() => colours.filetypes.music, - f if f.is_lossless() => colours.filetypes.lossless, - f if f.is_crypto() => colours.filetypes.crypto, - f if f.is_document() => colours.filetypes.document, - f if f.is_compressed() => colours.filetypes.compressed, - f if f.is_temp() => colours.filetypes.temp, - f if f.is_compiled() => colours.filetypes.compiled, - _ => colours.filetypes.normal, - } -} - - -trait FileTypes { +pub trait FileTypes { fn is_immediate(&self) -> bool; fn is_image(&self) -> bool; fn is_video(&self) -> bool; diff --git a/src/main.rs b/src/main.rs index 11a2fc0..e290853 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,6 @@ use dir::Dir; use file::File; use options::{Options, View}; -mod colours; mod dir; mod feature; mod file; diff --git a/src/options.rs b/src/options.rs index adffef1..c9baa2e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -7,10 +7,10 @@ use std::os::unix::fs::MetadataExt; use getopts; use natord; -use colours::Colours; use feature::xattr; use file::File; use output::{Grid, Details, GridDetails, Lines}; +use output::Colours; use output::column::{Columns, TimeTypes, SizeFormat}; use term::dimensions; diff --git a/src/colours.rs b/src/output/colours.rs similarity index 100% rename from src/colours.rs rename to src/output/colours.rs diff --git a/src/output/details.rs b/src/output/details.rs index 0b45b7b..0a65fe6 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -118,12 +118,12 @@ use std::string::ToString; use std::ops::Add; use std::iter::repeat; -use colours::Colours; use dir::Dir; use feature::xattr::{Attribute, FileAttributes}; use file::fields as f; use file::File; use options::{FileFilter, RecurseOptions}; +use output::colours::Colours; use output::column::{Alignment, Column, Columns, SizeFormat}; use output::cell::{TextCell, DisplayWidth}; diff --git a/src/output/grid.rs b/src/output/grid.rs index 69001bf..f944fbb 100644 --- a/src/output/grid.rs +++ b/src/output/grid.rs @@ -1,10 +1,10 @@ -use colours::Colours; -use file::File; -use filetype::file_colour; -use output::DisplayWidth; - use term_grid as grid; +use file::File; +use output::DisplayWidth; +use output::colours::Colours; +use super::file_colour; + #[derive(PartialEq, Debug, Copy, Clone)] pub struct Grid { diff --git a/src/output/lines.rs b/src/output/lines.rs index 39ef2c3..07c4351 100644 --- a/src/output/lines.rs +++ b/src/output/lines.rs @@ -1,9 +1,9 @@ -use colours::Colours; -use file::File; - use ansi_term::ANSIStrings; +use file::File; + use super::filename; +use super::colours::Colours; #[derive(Clone, Copy, Debug, PartialEq)] diff --git a/src/output/mod.rs b/src/output/mod.rs index d1c7e14..f095a79 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -1,14 +1,13 @@ use ansi_term::Style; -use colours::Colours; use file::File; -use filetype::file_colour; pub use self::cell::{TextCell, TextCellContents, DisplayWidth}; +pub use self::colours::Colours; pub use self::details::Details; +pub use self::grid_details::GridDetails; pub use self::grid::Grid; pub use self::lines::Lines; -pub use self::grid_details::GridDetails; mod grid; pub mod details; @@ -16,7 +15,7 @@ mod lines; mod grid_details; pub mod column; mod cell; - +mod colours; pub fn filename(file: File, colours: &Colours, links: bool) -> TextCellContents { if links && file.is_link() { @@ -49,3 +48,25 @@ fn symlink_filename(file: File, colours: &Colours) -> TextCellContents { ].into(), } } + +pub fn file_colour(colours: &Colours, file: &File) -> Style { + use filetype::FileTypes; + + match file { + f if f.is_directory() => colours.filetypes.directory, + f if f.is_executable_file() => colours.filetypes.executable, + f if f.is_link() => colours.filetypes.symlink, + f if !f.is_file() => colours.filetypes.special, + f if f.is_immediate() => colours.filetypes.immediate, + f if f.is_image() => colours.filetypes.image, + f if f.is_video() => colours.filetypes.video, + f if f.is_music() => colours.filetypes.music, + f if f.is_lossless() => colours.filetypes.lossless, + f if f.is_crypto() => colours.filetypes.crypto, + f if f.is_document() => colours.filetypes.document, + f if f.is_compressed() => colours.filetypes.compressed, + f if f.is_temp() => colours.filetypes.temp, + f if f.is_compiled() => colours.filetypes.compiled, + _ => colours.filetypes.normal, + } +} \ No newline at end of file