Added --theme option to select icon theme

Possible values are `default` and `unicode`.
This commit is contained in:
Sebastian Zivota 2019-01-14 20:58:14 +01:00 committed by Pierre Peltier
parent 662f3ddf38
commit bc3e47c6d7
5 changed files with 47 additions and 14 deletions

View file

@ -32,6 +32,15 @@ pub fn build() -> App<'static, 'static> {
.multiple(true) .multiple(true)
.help("When to print the icons"), .help("When to print the icons"),
) )
.arg(
Arg::with_name("theme")
.long("theme")
.possible_value("default")
.possible_value("unicode")
.default_value("default")
.multiple(true)
.help("Whether to use extended or unicode icons"),
)
.arg( .arg(
Arg::with_name("indicators") Arg::with_name("indicators")
.short("F") .short("F")

View file

@ -1,7 +1,7 @@
use batch::Batch; use batch::Batch;
use color::{self, Colors}; use color::{self, Colors};
use display::Display; use display::Display;
use flags::{Flags, WhenFlag}; use flags::{Flags, ThemeFlag, WhenFlag};
use icon::{self, Icons}; use icon::{self, Icons};
use meta::{FileType, Meta}; use meta::{FileType, Meta};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -28,12 +28,10 @@ impl Core {
_ => color::Theme::Default, _ => color::Theme::Default,
}; };
let icon_theme = match (tty_available, flags.icon) { let icon_theme = match (tty_available, flags.icon, flags.icon_theme) {
(true, WhenFlag::Never) => icon::Theme::NoIcon, (_, WhenFlag::Never, _) | (false, WhenFlag::Auto, _) => icon::Theme::NoIcon,
(false, WhenFlag::Never) => icon::Theme::NoIcon, (_, _, ThemeFlag::Default) => icon::Theme::Default,
(false, WhenFlag::Auto) => icon::Theme::NoIcon, (_, _, ThemeFlag::Unicode) => icon::Theme::Unicode,
(false, WhenFlag::Always) => icon::Theme::Default,
_ => icon::Theme::Default,
}; };
if !tty_available { if !tty_available {

View file

@ -14,6 +14,7 @@ pub struct Flags {
pub date: DateFlag, pub date: DateFlag,
pub color: WhenFlag, pub color: WhenFlag,
pub icon: WhenFlag, pub icon: WhenFlag,
pub icon_theme: ThemeFlag,
pub recursion_depth: usize, pub recursion_depth: usize,
} }
@ -21,6 +22,7 @@ impl Flags {
pub fn from_matches(matches: &ArgMatches) -> Result<Self, Error> { pub fn from_matches(matches: &ArgMatches) -> Result<Self, Error> {
let color_inputs: Vec<&str> = matches.values_of("color").unwrap().collect(); let color_inputs: Vec<&str> = matches.values_of("color").unwrap().collect();
let icon_inputs: Vec<&str> = matches.values_of("icon").unwrap().collect(); let icon_inputs: Vec<&str> = matches.values_of("icon").unwrap().collect();
let icon_theme_inputs: Vec<&str> = matches.values_of("theme").unwrap().collect();
let date_inputs: Vec<&str> = matches.values_of("date").unwrap().collect(); let date_inputs: Vec<&str> = matches.values_of("date").unwrap().collect();
let dir_order_inputs: Vec<&str> = matches.values_of("group-dirs").unwrap().collect(); let dir_order_inputs: Vec<&str> = matches.values_of("group-dirs").unwrap().collect();
@ -70,6 +72,7 @@ impl Flags {
date: DateFlag::from(date_inputs[date_inputs.len() - 1]), date: DateFlag::from(date_inputs[date_inputs.len() - 1]),
color: WhenFlag::from(color_inputs[color_inputs.len() - 1]), color: WhenFlag::from(color_inputs[color_inputs.len() - 1]),
icon: WhenFlag::from(icon_inputs[icon_inputs.len() - 1]), icon: WhenFlag::from(icon_inputs[icon_inputs.len() - 1]),
icon_theme: ThemeFlag::from(icon_theme_inputs[icon_inputs.len() - 1]),
directory_order: DirOrderFlag::from(dir_order_inputs[dir_order_inputs.len() - 1]), directory_order: DirOrderFlag::from(dir_order_inputs[dir_order_inputs.len() - 1]),
}) })
} }
@ -91,6 +94,7 @@ impl Default for Flags {
date: DateFlag::Date, date: DateFlag::Date,
color: WhenFlag::Auto, color: WhenFlag::Auto,
icon: WhenFlag::Auto, icon: WhenFlag::Auto,
icon_theme: ThemeFlag::Default,
} }
} }
} }
@ -159,6 +163,22 @@ impl<'a> From<&'a str> for DirOrderFlag {
} }
} }
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum ThemeFlag {
Unicode,
Default,
}
impl<'a> From<&'a str> for ThemeFlag {
fn from(theme: &'a str) -> Self {
match theme {
"default" => ThemeFlag::Default,
"unicode" => ThemeFlag::Unicode,
_ => panic!("invalid \"theme\" flag: {}", theme),
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::Flags; use super::Flags;

View file

@ -24,7 +24,10 @@ impl Icons {
pub fn new(theme: Theme) -> Self { pub fn new(theme: Theme) -> Self {
let display_icons = theme == Theme::Default || theme == Theme::Unicode; let display_icons = theme == Theme::Default || theme == Theme::Unicode;
let (icons_by_name, icons_by_extension) = if theme == Theme::Default { let (icons_by_name, icons_by_extension) = if theme == Theme::Default {
(Self::get_default_icons_by_name(), Self::get_default_icons_by_extension()) (
Self::get_default_icons_by_name(),
Self::get_default_icons_by_extension(),
)
} else { } else {
(HashMap::new(), HashMap::new()) (HashMap::new(), HashMap::new())
}; };

View file

@ -1,9 +1,12 @@
#![cfg_attr(feature = "cargo-clippy", allow( #![cfg_attr(
clippy::cast_precision_loss, feature = "cargo-clippy",
clippy::cast_sign_loss, allow(
clippy::match_same_arms, clippy::cast_precision_loss,
clippy::cast_possible_wrap clippy::cast_sign_loss,
))] clippy::match_same_arms,
clippy::cast_possible_wrap
)
)]
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;