Skip to content

Commit

Permalink
🎨 Add ability to list custom and built-in themes (#90)
Browse files Browse the repository at this point in the history
* Add ability to list custom and built-in themes

* Refactor and add warning message when no custom themes are present
  • Loading branch information
grtcdr authored May 17, 2021
1 parent b540b5a commit 9a8547b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ pub struct Opt {
)]
#[serde(skip_serializing, skip_deserializing)]
pub export_config: bool,

#[structopt(
long = "list-themes",
help = "Lists all available themes: built-in and custom"
)]
pub list_themes: bool,
}

impl Default for Opt {
Expand Down Expand Up @@ -270,6 +276,7 @@ impl Default for Opt {
box_inner_margin_x: 1,
box_inner_margin_y: 0,
export_config: false,
list_themes: false,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ impl Opt {
if args.theme.is_some() {
self.theme = args.theme;
}
if args.list_themes {
self.list_themes = true;
}
}

// Checks for conflicts between the different flags.
Expand Down
47 changes: 45 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ mod format;
mod theme;

use cli::{MacchinaColor, Opt};
use colored::Colorize;
use std::io;
use structopt::StructOpt;
use theme::Themes;

#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -185,6 +187,42 @@ fn select_ascii(small: bool) -> Option<Text<'static>> {
}
}

fn list_themes() {
let themes = Themes::variants();
themes
.iter()
.for_each(|x| println!("• {} (Built-in)", x.bright_green()));

if let Some(dir) = dirs::data_local_dir() {
let entries = libmacchina::extra::list_dir_entries(&dir.join("macchina/themes"));
if !entries.is_empty() {
let custom_themes = entries.iter().filter(|&x| {
if let Some(ext) = libmacchina::extra::path_extension(&x) {
ext == "json"
} else {
false
}
});

if custom_themes.clone().count() == 0 {
println!(
"\nNo custom themes were found in {}",
dir.join("macchina/themes")
.to_string_lossy()
.bright_yellow()
)
}

custom_themes.for_each(|x| {
if let Some(theme) = x.file_name() {
let name = theme.to_string_lossy().replace(".json", "");
println!("• {}", name.bright_blue());
}
});
}
}
}

fn main() -> Result<(), io::Error> {
let mut opt: Opt;
let config_opt = Opt::from_config();
Expand All @@ -201,8 +239,8 @@ fn main() -> Result<(), io::Error> {
let conflicts = opt.check_conflicts();
if !conflicts.is_empty() {
println!("\x1b[33mWarning:\x1b[0m Conflicting keys in config file:");
for i in 0..conflicts.len() {
println!("• {}", conflicts[i]);
for conflict in conflicts {
println!("• {}", conflict);
}
opt = arg_opt;
}
Expand All @@ -220,6 +258,11 @@ fn main() -> Result<(), io::Error> {
return Ok(());
}

if opt.list_themes {
list_themes();
return Ok(());
}

let mut backend = create_backend();
let mut tmp_buffer = Buffer::empty(Rect::new(0, 0, 500, 50));

Expand Down
21 changes: 4 additions & 17 deletions src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::data::ReadoutKey;
use clap::arg_enum;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;
use tui::style::Color;

/// Defines the different ways a key can be named, let's take the _OperatingSystem variant_ for example: \
Expand Down Expand Up @@ -162,27 +162,14 @@ impl BarStyle {
}
}

/// This stores the pre-defined themes
#[derive(Debug)]
pub enum Themes {
arg_enum! {
#[derive(Debug)]
pub enum Themes {
Hydrogen,
Helium,
Lithium,
Beryllium,
Boron,
}

impl FromStr for Themes {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.to_ascii_lowercase().as_str() {
"hydrogen" => Self::Hydrogen,
"helium" => Self::Helium,
"lithium" => Self::Lithium,
"beryllium" => Self::Beryllium,
"boron" => Self::Boron,
&_ => return Err(s.to_ascii_lowercase()),
})
}
}

Expand Down

0 comments on commit 9a8547b

Please sign in to comment.