mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Style: Remove ColorState enum, revert init_color
Booleans are simpler and easier to work with. Revert init_color to its earlier state as it is easier to understand
This commit is contained in:
+6
-20
@@ -22,7 +22,7 @@ static BLOCKS: [char; 5] = ['█', '▓', '▒', '░', ' '];
|
|||||||
pub struct DisplayData {
|
pub struct DisplayData {
|
||||||
pub short_paths: bool,
|
pub short_paths: bool,
|
||||||
pub is_reversed: bool,
|
pub is_reversed: bool,
|
||||||
pub colors: ColorState,
|
pub colors_on: bool,
|
||||||
pub by_filecount: bool,
|
pub by_filecount: bool,
|
||||||
pub num_chars_needed_on_left_most: usize,
|
pub num_chars_needed_on_left_most: usize,
|
||||||
pub base_size: u64,
|
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)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn draw_it(
|
pub fn draw_it(
|
||||||
use_full_path: bool,
|
use_full_path: bool,
|
||||||
is_reversed: bool,
|
is_reversed: bool,
|
||||||
colors: ColorState,
|
no_colors: bool,
|
||||||
no_percents: bool,
|
no_percents: bool,
|
||||||
terminal_width: usize,
|
terminal_width: usize,
|
||||||
by_filecount: bool,
|
by_filecount: bool,
|
||||||
@@ -168,7 +154,7 @@ pub fn draw_it(
|
|||||||
let display_data = DisplayData {
|
let display_data = DisplayData {
|
||||||
short_paths: !use_full_path,
|
short_paths: !use_full_path,
|
||||||
is_reversed,
|
is_reversed,
|
||||||
colors,
|
colors_on: !no_colors,
|
||||||
by_filecount,
|
by_filecount,
|
||||||
num_chars_needed_on_left_most,
|
num_chars_needed_on_left_most,
|
||||||
base_size: biggest.size,
|
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 spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count();
|
||||||
let output = " ".repeat(spaces_to_add) + output.as_str();
|
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))
|
format!("{}", Red.paint(output))
|
||||||
} else {
|
} else {
|
||||||
output
|
output
|
||||||
@@ -385,7 +371,7 @@ fn get_pretty_name(
|
|||||||
name_and_padding: String,
|
name_and_padding: String,
|
||||||
display_data: &DisplayData,
|
display_data: &DisplayData,
|
||||||
) -> String {
|
) -> String {
|
||||||
if display_data.colors.enabled() {
|
if display_data.colors_on {
|
||||||
let meta_result = fs::metadata(&node.name);
|
let meta_result = fs::metadata(&node.name);
|
||||||
let directory_color = display_data
|
let directory_color = display_data
|
||||||
.ls_colors
|
.ls_colors
|
||||||
@@ -425,7 +411,7 @@ mod tests {
|
|||||||
DisplayData {
|
DisplayData {
|
||||||
short_paths: true,
|
short_paths: true,
|
||||||
is_reversed: false,
|
is_reversed: false,
|
||||||
colors: ColorState::Disabled,
|
colors_on: false,
|
||||||
by_filecount: false,
|
by_filecount: false,
|
||||||
num_chars_needed_on_left_most: 5,
|
num_chars_needed_on_left_most: 5,
|
||||||
base_size: 1,
|
base_size: 1,
|
||||||
|
|||||||
+16
-28
@@ -6,7 +6,7 @@ extern crate unicode_width;
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
use self::display::{draw_it, ColorState};
|
use self::display::draw_it;
|
||||||
use clap::{crate_version, Arg};
|
use clap::{crate_version, Arg};
|
||||||
use clap::{Command, Values};
|
use clap::{Command, Values};
|
||||||
use dir_walker::{walk_it, WalkData};
|
use dir_walker::{walk_it, WalkData};
|
||||||
@@ -29,37 +29,28 @@ mod utils;
|
|||||||
static DEFAULT_NUMBER_OF_LINES: usize = 30;
|
static DEFAULT_NUMBER_OF_LINES: usize = 30;
|
||||||
static DEFAULT_TERMINAL_WIDTH: usize = 80;
|
static DEFAULT_TERMINAL_WIDTH: usize = 80;
|
||||||
|
|
||||||
/// `ansi_term::enable_ansi_support` only exists on Windows; this wrapper
|
#[cfg(not(windows))]
|
||||||
/// function makes it available on all platforms
|
fn init_color(no_color: bool) -> bool {
|
||||||
#[inline]
|
no_color
|
||||||
fn enable_ansi_support() -> Result<(), u32> {
|
|
||||||
#[cfg(windows)]
|
|
||||||
{
|
|
||||||
ansi_term::enable_ansi_support()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(windows))]
|
|
||||||
{
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_color(color: ColorState) -> ColorState {
|
#[cfg(windows)]
|
||||||
match color {
|
fn init_color(no_color: bool) -> bool {
|
||||||
// If no color is already set do not print a warning message
|
// If no color is already set do not print a warning message
|
||||||
ColorState::Disabled => ColorState::Disabled,
|
if no_color {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
// Required for windows 10
|
// Required for windows 10
|
||||||
// Fails to resolve for windows 8 so disable color
|
// Fails to resolve for windows 8 so disable color
|
||||||
ColorState::Enabled => match enable_ansi_support() {
|
match ansi_term::enable_ansi_support() {
|
||||||
Ok(()) => ColorState::Enabled,
|
Ok(_) => no_color,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"This version of Windows does not support ANSI colors, setting no_color flag"
|
"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);
|
.unwrap_or(default_height);
|
||||||
|
|
||||||
let colors = init_color(match options.is_present("no_colors") {
|
let no_colors = init_color(options.is_present("no_colors"));
|
||||||
false => ColorState::Enabled,
|
|
||||||
true => ColorState::Disabled,
|
|
||||||
});
|
|
||||||
let use_apparent_size = options.is_present("display_apparent_size");
|
let use_apparent_size = options.is_present("display_apparent_size");
|
||||||
let ignore_directories = options
|
let ignore_directories = options
|
||||||
.values_of("ignore_directory")
|
.values_of("ignore_directory")
|
||||||
@@ -318,7 +306,7 @@ fn main() {
|
|||||||
draw_it(
|
draw_it(
|
||||||
options.is_present("display_full_paths"),
|
options.is_present("display_full_paths"),
|
||||||
!options.is_present("reverse"),
|
!options.is_present("reverse"),
|
||||||
colors,
|
no_colors,
|
||||||
options.is_present("no_bars"),
|
options.is_present("no_bars"),
|
||||||
terminal_width,
|
terminal_width,
|
||||||
by_filecount,
|
by_filecount,
|
||||||
|
|||||||
Reference in New Issue
Block a user