Update theme to latest version

This commit is contained in:
Soapydev 2024-08-15 18:13:47 -04:00
parent 4b245fbf40
commit 16c6363a95
3 changed files with 115 additions and 57 deletions

View File

@ -10,6 +10,7 @@ use std::{
time::Duration, time::Duration,
}; };
use crate::theme::Theme;
use clap::Parser; use clap::Parser;
use crossterm::{ use crossterm::{
cursor::RestorePosition, cursor::RestorePosition,
@ -25,24 +26,21 @@ use ratatui::{
}; };
use state::AppState; use state::AppState;
use tempdir::TempDir; use tempdir::TempDir;
use theme::THEMES;
/// This is a binary :), Chris, change this to update the documentation on -h // Linux utility toolbox
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Args { struct Args {
/// Enable compatibility mode (disable icons and RGB colors) #[arg(short, long, value_enum)]
#[arg(short, long, default_value_t = false)] #[arg(default_value_t = Theme::Default)]
compat: bool, #[arg(help = "Set the theme to use in the application")]
theme: Theme,
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let args = Args::parse(); let args = Args::parse();
let theme = if args.compat { let theme = args.theme;
THEMES[0].clone()
} else {
THEMES[1].clone()
};
let commands_dir = include_dir!("src/commands"); let commands_dir = include_dir!("src/commands");
let temp_dir: TempDir = TempDir::new("linutil_scripts").unwrap(); let temp_dir: TempDir = TempDir::new("linutil_scripts").unwrap();
commands_dir commands_dir

View File

@ -69,7 +69,7 @@ impl AppState {
pub fn draw(&mut self, frame: &mut Frame) { pub fn draw(&mut self, frame: &mut Frame) {
let longest_tab_display_len = TABS let longest_tab_display_len = TABS
.iter() .iter()
.map(|tab| tab.name.len() + self.theme.tab_icon.len()) .map(|tab| tab.name.len() + self.theme.tab_icon().len())
.max() .max()
.unwrap_or(0); .unwrap_or(0);
@ -88,15 +88,15 @@ impl AppState {
let tabs = TABS.iter().map(|tab| tab.name).collect::<Vec<_>>(); let tabs = TABS.iter().map(|tab| tab.name).collect::<Vec<_>>();
let tab_hl_style = if let Focus::TabList = self.focus { let tab_hl_style = if let Focus::TabList = self.focus {
Style::default().reversed().fg(self.theme.tab_color) Style::default().reversed().fg(self.theme.tab_color())
} else { } else {
Style::new().fg(self.theme.tab_color) Style::new().fg(self.theme.tab_color())
}; };
let list = List::new(tabs) let list = List::new(tabs)
.block(Block::default().borders(Borders::ALL)) .block(Block::default().borders(Borders::ALL))
.highlight_style(tab_hl_style) .highlight_style(tab_hl_style)
.highlight_symbol(self.theme.tab_icon); .highlight_symbol(self.theme.tab_icon());
frame.render_stateful_widget(list, left_chunks[1], &mut self.current_tab); frame.render_stateful_widget(list, left_chunks[1], &mut self.current_tab);
let chunks = Layout::default() let chunks = Layout::default()
@ -122,7 +122,7 @@ impl AppState {
let mut items: Vec<Line> = Vec::new(); let mut items: Vec<Line> = Vec::new();
if !self.at_root() { if !self.at_root() {
items.push( items.push(
Line::from(format!("{} ..", self.theme.dir_icon)).style(self.theme.dir_color), Line::from(format!("{} ..", self.theme.dir_icon())).style(self.theme.dir_color()),
); );
} }
@ -131,11 +131,11 @@ impl AppState {
node, has_children, .. node, has_children, ..
}| { }| {
if *has_children { if *has_children {
Line::from(format!("{} {}", self.theme.dir_icon, node.name)) Line::from(format!("{} {}", self.theme.dir_icon(), node.name))
.style(self.theme.dir_color) .style(self.theme.dir_color())
} else { } else {
Line::from(format!("{} {}", self.theme.cmd_icon, node.name)) Line::from(format!("{} {}", self.theme.cmd_icon(), node.name))
.style(self.theme.cmd_color) .style(self.theme.cmd_color())
} }
}, },
)); ));

View File

@ -1,42 +1,102 @@
use clap::ValueEnum;
use ratatui::style::Color; use ratatui::style::Color;
#[derive(Clone)] // Add the Theme name here for a new theme
pub struct Theme { // This is more secure than the previous list
pub dir_color: Color, // We cannot index out of bounds, and we are giving
pub cmd_color: Color, // names to our various themes, making it very clear
pub tab_color: Color, // This will make it easy to add new themes
pub dir_icon: &'static str, #[derive(Clone, Debug, PartialEq, Default, ValueEnum, Copy)]
pub cmd_icon: &'static str, pub enum Theme {
pub tab_icon: &'static str, #[default]
pub success_color: Color, Default,
pub fail_color: Color, Compatible,
pub focused_color: Color,
pub unfocused_color: Color,
} }
pub const THEMES: [Theme; 2] = [ impl Theme {
Theme { pub fn dir_color(&self) -> Color {
dir_color: Color::Blue, match self {
cmd_color: Color::LightGreen, Theme::Default => Color::Blue,
tab_color: Color::Yellow, Theme::Compatible => Color::Blue,
dir_icon: "[DIR]", }
cmd_icon: "[CMD]", }
tab_icon: ">> ",
success_color: Color::Green, pub fn cmd_color(&self) -> Color {
fail_color: Color::Red, match self {
focused_color: Color::LightBlue, Theme::Default => Color::Rgb(204, 224, 208),
unfocused_color: Color::Gray, Theme::Compatible => Color::LightGreen,
}, }
Theme { }
dir_color: Color::Blue,
cmd_color: Color::Rgb(204, 224, 208), pub fn tab_color(&self) -> Color{
tab_color: Color::Rgb(255, 255, 85), match self {
dir_icon: "", Theme::Default => Color::Rgb(255, 255, 85),
cmd_icon: "", Theme::Compatible => Color::Yellow,
tab_icon: "", }
fail_color: Color::Rgb(199, 55, 44), }
success_color: Color::Rgb(5, 255, 55),
focused_color: Color::LightBlue, pub fn dir_icon(&self) -> &'static str {
unfocused_color: Color::Gray, match self {
}, Theme::Default => "",
]; Theme::Compatible => "[DIR]",
}
}
pub fn cmd_icon(&self) -> &'static str {
match self {
Theme::Default => "",
Theme::Compatible => "[CMD]",
}
}
pub fn tab_icon(&self) -> &'static str {
match self {
Theme::Default => "",
Theme::Compatible => ">> ",
}
}
pub fn success_color(&self) -> Color {
match self {
Theme::Default => Color::Rgb(199, 55, 44),
Theme::Compatible => Color::Green,
}
}
pub fn fail_color(&self) -> Color {
match self {
Theme::Default => Color::Rgb(5, 255, 55),
Theme::Compatible => Color::Red,
}
}
pub fn focused_color(&self) -> Color {
match self {
Theme::Default => Color::LightBlue,
Theme::Compatible => Color::LightBlue,
}
}
pub fn unfocused_color(&self) -> Color {
match self {
Theme::Default => Color::Gray,
Theme::Compatible => Color::Gray,
}
}
}
impl Theme {
#[allow(unused)]
pub fn next(self) -> Self {
let position = self as usize;
let types = Theme::value_variants();
types[(position + 1) % types.len()].into()
}
#[allow(unused)]
pub fn prev(self) -> Self {
let position = self as usize;
let types = Theme::value_variants();
types[(position + types.len() - 1) % types.len()].into()
}
}