diff --git a/src/display.rs b/src/display.rs index 23ca01a..3d81c4e 100644 --- a/src/display.rs +++ b/src/display.rs @@ -22,7 +22,7 @@ static BLOCKS: [char; 5] = ['█', '▓', '▒', '░', ' ']; pub struct DisplayData { pub short_paths: bool, pub is_reversed: bool, - pub colors: ColorState, + pub colors_on: bool, pub by_filecount: bool, pub num_chars_needed_on_left_most: usize, pub base_size: u64, @@ -106,25 +106,11 @@ impl DrawData<'_> { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum ColorState { - Disabled, - Enabled, -} - -impl ColorState { - #[inline] - #[must_use] - fn enabled(self) -> bool { - self == ColorState::Enabled - } -} - #[allow(clippy::too_many_arguments)] pub fn draw_it( use_full_path: bool, is_reversed: bool, - colors: ColorState, + no_colors: bool, no_percents: bool, terminal_width: usize, by_filecount: bool, @@ -168,7 +154,7 @@ pub fn draw_it( let display_data = DisplayData { short_paths: !use_full_path, is_reversed, - colors, + colors_on: !no_colors, by_filecount, num_chars_needed_on_left_most, base_size: biggest.size, @@ -373,7 +359,7 @@ fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayD let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count(); let output = " ".repeat(spaces_to_add) + output.as_str(); - if is_biggest && display_data.colors.enabled() { + if is_biggest && display_data.colors_on { format!("{}", Red.paint(output)) } else { output @@ -385,7 +371,7 @@ fn get_pretty_name( name_and_padding: String, display_data: &DisplayData, ) -> String { - if display_data.colors.enabled() { + if display_data.colors_on { let meta_result = fs::metadata(&node.name); let directory_color = display_data .ls_colors @@ -425,7 +411,7 @@ mod tests { DisplayData { short_paths: true, is_reversed: false, - colors: ColorState::Disabled, + colors_on: false, by_filecount: false, num_chars_needed_on_left_most: 5, base_size: 1, diff --git a/src/main.rs b/src/main.rs index b940db5..357e313 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate unicode_width; use std::collections::HashSet; use std::process; -use self::display::{draw_it, ColorState}; +use self::display::draw_it; use clap::{crate_version, Arg}; use clap::{Command, Values}; use dir_walker::{walk_it, WalkData}; @@ -29,37 +29,28 @@ mod utils; static DEFAULT_NUMBER_OF_LINES: usize = 30; static DEFAULT_TERMINAL_WIDTH: usize = 80; -/// `ansi_term::enable_ansi_support` only exists on Windows; this wrapper -/// function makes it available on all platforms -#[inline] -fn enable_ansi_support() -> Result<(), u32> { - #[cfg(windows)] - { - ansi_term::enable_ansi_support() - } - - #[cfg(not(windows))] - { - Ok(()) - } +#[cfg(not(windows))] +fn init_color(no_color: bool) -> bool { + no_color } -fn init_color(color: ColorState) -> ColorState { - match color { - // If no color is already set do not print a warning message - ColorState::Disabled => ColorState::Disabled, - +#[cfg(windows)] +fn init_color(no_color: bool) -> bool { + // If no color is already set do not print a warning message + if no_color { + true + } else { // Required for windows 10 // Fails to resolve for windows 8 so disable color - ColorState::Enabled => match enable_ansi_support() { - Ok(()) => ColorState::Enabled, + match ansi_term::enable_ansi_support() { + Ok(_) => no_color, Err(_) => { eprintln!( "This version of Windows does not support ANSI colors, setting no_color flag" ); - ColorState::Disabled + true } - }, + } } } @@ -260,10 +251,7 @@ fn main() { }) .unwrap_or(default_height); - let colors = init_color(match options.is_present("no_colors") { - false => ColorState::Enabled, - true => ColorState::Disabled, - }); + let no_colors = init_color(options.is_present("no_colors")); let use_apparent_size = options.is_present("display_apparent_size"); let ignore_directories = options .values_of("ignore_directory") @@ -318,7 +306,7 @@ fn main() { draw_it( options.is_present("display_full_paths"), !options.is_present("reverse"), - colors, + no_colors, options.is_present("no_bars"), terminal_width, by_filecount,