Skip to content

Commit

Permalink
🔥 icon: drop custom icon file option
Browse files Browse the repository at this point in the history
Signed-off-by: zwPapEr <zw.paper@gmail.com>
  • Loading branch information
zwpaper committed Aug 9, 2022
1 parent 5b45f89 commit 60c019a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -26,6 +26,7 @@ libc = "0.2.*"
human-sort = "0.2.2"
term_grid = "0.1.*"
terminal_size = "0.1.*"
thiserror = "1.0"
chrono = "0.4.*"
chrono-humanize = "0.1.*"
unicode-width = "0.1.*"
Expand Down
2 changes: 2 additions & 0 deletions src/app.rs
Expand Up @@ -47,6 +47,8 @@ pub fn build() -> App<'static, 'static> {
Arg::with_name("icon-theme")
.long("icon-theme")
.default_value("fancy")
.possible_value("fancy")
.possible_value("unicode")
.multiple(true)
.number_of_values(1)
.help("Whether to use fancy or unicode icons"),
Expand Down
1 change: 0 additions & 1 deletion src/flags/icons.rs
Expand Up @@ -101,7 +101,6 @@ impl Default for IconOption {
pub enum IconTheme {
Unicode,
Fancy,
Custom(String),
}

impl IconTheme {
Expand Down
12 changes: 7 additions & 5 deletions src/icon.rs
Expand Up @@ -4,7 +4,6 @@ use crate::theme::{icon::IconTheme, Theme};

pub struct Icons {
icon_separator: String,

theme: Option<IconTheme>,
}

Expand All @@ -16,11 +15,14 @@ impl Icons {
pub fn new(tty: bool, when: IconOption, theme: FlagTheme, icon_separator: String) -> Self {
let icon_theme = match (tty, when, theme) {
(_, 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::Custom(ref file)) => {
Some(Theme::from_path::<IconTheme>(file).unwrap_or_default())
}
};

Self {
Expand Down
62 changes: 34 additions & 28 deletions src/theme.rs
@@ -1,9 +1,11 @@
pub mod color;
pub mod icon;

use serde::{de::DeserializeOwned, Deserialize};
use std::fs;
use std::path::Path;
use std::{fs, io};

use serde::{de::DeserializeOwned, Deserialize};
use thiserror::Error;

use crate::config_file;
use crate::print_error;
Expand All @@ -20,49 +22,53 @@ pub struct Theme {
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 {
/// This read theme from file,
/// use the file path if it is absolute
/// 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) {
path
} else {
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() {
real
} else {
config_file::Config::config_file_path()?
.join("themes")
.join(real)
match config_file::Config::config_file_path() {
Some(p) => p.join("themes").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)) {
Ok(t) => Some(t),
Err(e) => {
print_error!("Theme file {} format error: {}.", &file, e);
None
}
},
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
}
},

// try `yml` if `yaml` extension file not found or error
let mut err: Error = Error::Unknown();
for ext in ["yaml", "yml"] {
match fs::read(&path.with_extension(ext)) {
Ok(f) => match Self::with_yaml(&String::from_utf8_lossy(&f)) {
Ok(t) => return Ok(t),
Err(e) => {
print_error!("Not a valid theme: {}, {}.", path.to_string_lossy(), e);
None
err = Error::from(e);
}
}
},
Err(e) => err = Error::from(e),
}
}

Err(err)
}

/// This constructs a Theme struct with a passed [Yaml] str.
Expand Down

0 comments on commit 60c019a

Please sign in to comment.