🔥 icon: drop custom icon file option

Signed-off-by: zwPapEr <zw.paper@gmail.com>
This commit is contained in:
zwPapEr 2022-08-05 14:53:11 +08:00 committed by Abin Simon
parent a60ab3d3e4
commit 9de3090c12
6 changed files with 45 additions and 34 deletions

1
Cargo.lock generated
View file

@ -435,6 +435,7 @@ dependencies = [
"tempfile", "tempfile",
"term_grid", "term_grid",
"terminal_size", "terminal_size",
"thiserror",
"unicode-width", "unicode-width",
"url", "url",
"users", "users",

View file

@ -27,6 +27,7 @@ libc = "0.2.*"
human-sort = "0.2.2" human-sort = "0.2.2"
term_grid = "0.1.*" term_grid = "0.1.*"
terminal_size = "0.1.*" terminal_size = "0.1.*"
thiserror = "1.0"
chrono = "0.4.*" chrono = "0.4.*"
chrono-humanize = "0.1.*" chrono-humanize = "0.1.*"
unicode-width = "0.1.*" unicode-width = "0.1.*"

View file

@ -54,6 +54,8 @@ pub fn build() -> App<'static> {
Arg::with_name("icon-theme") Arg::with_name("icon-theme")
.long("icon-theme") .long("icon-theme")
.default_value("fancy") .default_value("fancy")
.possible_value("fancy")
.possible_value("unicode")
.multiple_occurrences(true) .multiple_occurrences(true)
.takes_value(true) .takes_value(true)
.number_of_values(1) .number_of_values(1)

View file

@ -96,7 +96,6 @@ pub enum IconTheme {
Unicode, Unicode,
#[default] #[default]
Fancy, Fancy,
Custom(String),
} }
impl IconTheme { impl IconTheme {

View file

@ -4,7 +4,6 @@ use crate::theme::{icon::IconTheme, Theme};
pub struct Icons { pub struct Icons {
icon_separator: String, icon_separator: String,
theme: Option<IconTheme>, theme: Option<IconTheme>,
} }
@ -16,11 +15,14 @@ impl Icons {
pub fn new(tty: bool, when: IconOption, theme: FlagTheme, icon_separator: String) -> Self { pub fn new(tty: bool, when: IconOption, theme: FlagTheme, icon_separator: String) -> Self {
let icon_theme = match (tty, when, theme) { let icon_theme = match (tty, when, theme) {
(_, IconOption::Never, _) | (false, IconOption::Auto, _) => None, (_, IconOption::Never, _) | (false, IconOption::Auto, _) => None,
(_, _, FlagTheme::Fancy) => Some(IconTheme::default()), (_, _, FlagTheme::Fancy) => {
if let Ok(t) = Theme::from_path::<IconTheme>("icons") {
Some(t)
} else {
Some(IconTheme::default())
}
},
(_, _, FlagTheme::Unicode) => Some(IconTheme::unicode()), (_, _, FlagTheme::Unicode) => Some(IconTheme::unicode()),
(_, _, FlagTheme::Custom(ref file)) => {
Some(Theme::from_path::<IconTheme>(file).unwrap_or_default())
}
}; };
Self { Self {

View file

@ -1,9 +1,11 @@
pub mod color; pub mod color;
pub mod icon; pub mod icon;
use serde::{de::DeserializeOwned, Deserialize};
use std::fs;
use std::path::Path; use std::path::Path;
use std::{fs, io};
use serde::{de::DeserializeOwned, Deserialize};
use thiserror::Error;
use crate::config_file; use crate::config_file;
use crate::print_error; use crate::print_error;
@ -20,49 +22,53 @@ pub struct Theme {
pub icon: IconTheme, pub icon: IconTheme,
} }
#[derive(Error, Debug)]
pub enum Error {
#[error("Theme file not existed")]
NotExisted(#[from] io::Error),
#[error("Theme file format invalid")]
InvalidFormat(#[from] serde_yaml::Error),
#[error("Theme file path invalid {0}")]
InvalidPath(String),
#[error("Unknown Theme error")]
Unknown(),
}
impl Theme { impl Theme {
/// This read theme from file, /// This read theme from file,
/// use the file path if it is absolute /// use the file path if it is absolute
/// prefix the config_file dir to it if it is not /// prefix the config_file dir to it if it is not
pub fn from_path<D: DeserializeOwned>(file: &str) -> Option<D> { pub fn from_path<D: DeserializeOwned>(file: &str) -> Result<D, Error> {
let real = if let Some(path) = config_file::Config::expand_home(file) { let real = if let Some(path) = config_file::Config::expand_home(file) {
path path
} else { } else {
print_error!("Not a valid theme file path: {}.", &file); print_error!("Not a valid theme file path: {}.", &file);
return None; return Err(Error::InvalidPath(file.to_string()));
}; };
let path = if Path::new(&real).is_absolute() { let path = if Path::new(&real).is_absolute() {
real real
} else { } else {
config_file::Config::config_file_path()? match config_file::Config::config_file_path() {
.join("themes") Some(p) => p.join("themes").join(real),
.join(real) None => return Err(Error::InvalidPath("config home not existed".into())),
}
}; };
match fs::read(&path.with_extension("yaml")) {
Ok(f) => match Self::with_yaml(&String::from_utf8_lossy(&f)) { // try `yml` if `yaml` extension file not found or error
Ok(t) => Some(t), let mut err: Error = Error::Unknown();
Err(e) => { for ext in ["yaml", "yml"] {
print_error!("Theme file {} format error: {}.", &file, e); match fs::read(&path.with_extension(ext)) {
None Ok(f) => match Self::with_yaml(&String::from_utf8_lossy(&f)) {
} Ok(t) => return Ok(t),
},
Err(_) => {
// try `yml` if `yaml` extension file not found
match fs::read(&path.with_extension("yml")) {
Ok(f) => match Self::with_yaml(&String::from_utf8_lossy(&f)) {
Ok(t) => Some(t),
Err(e) => {
print_error!("Theme file {} format error: {}.", &file, e);
None
}
},
Err(e) => { Err(e) => {
print_error!("Not a valid theme: {}, {}.", path.to_string_lossy(), e); err = Error::from(e);
None
} }
} },
Err(e) => err = Error::from(e),
} }
} }
Err(err)
} }
/// This constructs a Theme struct with a passed [Yaml] str. /// This constructs a Theme struct with a passed [Yaml] str.