From 6f32c504c02a5ede609bd1018a01557ddde181a8 Mon Sep 17 00:00:00 2001 From: 409 Date: Wed, 22 May 2024 00:39:04 +0200 Subject: [PATCH] feat: theming through arguments --- src/arguments.rs | 16 +++++++++++++++- src/config.rs | 11 +++++++++-- src/main.rs | 14 ++++++++++++-- src/runner/mod.rs | 25 +++++++++++-------------- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/arguments.rs b/src/arguments.rs index ebf0303..806882f 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -1,8 +1,22 @@ use clap::Parser; +use crate::config::{FONT_COLOR, FONT_COLOR_ACTIVE, BACKGROUND_COLOR, BACKGROUND_COLOR_ACTIVE}; + #[derive(Parser, Debug)] #[command(version, about, long_about = None)] pub struct Arguments { - #[arg(short, long, help = "The menu prompt message", default_value_t = String::from(""))] + #[arg(short, long, help = "The menu's prompt message", default_value_t = String::from(""))] pub prompt: String, + + #[arg(long, help = "The default font color", default_value_t = String::from(FONT_COLOR))] + pub font_color: String, + + #[arg(long, help = "The font color of the selected element", default_value_t = String::from(FONT_COLOR_ACTIVE))] + pub font_color_active: String, + + #[arg(long, help = "The default font color", default_value_t = String::from(BACKGROUND_COLOR))] + pub background_color: String, + + #[arg(long, help = "The font color of the selected element", default_value_t = String::from(BACKGROUND_COLOR_ACTIVE))] + pub background_color_active: String, } diff --git a/src/config.rs b/src/config.rs index 74bea99..77d0124 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,14 @@ pub const LINE_SPACING: u16 = 2; pub const FONT_POINT_SIZE: u16 = 16; pub const FONT_COLOR: &str = "#cdd6f4"; -pub const FONT_COLOR_SELECTED: &str = "#1e1e2e"; +pub const FONT_COLOR_ACTIVE: &str = "#1e1e2e"; pub const BACKGROUND_COLOR: &str = "#1e1e2e"; -pub const BACKGROUND_COLOR_SELECTED: &str = "#89b4fa"; +pub const BACKGROUND_COLOR_ACTIVE: &str = "#89b4fa"; + +pub struct RunnerMenuColors { + pub font_color: String, + pub font_color_active: String, + pub background_color: String, + pub background_color_active: String, +} diff --git a/src/main.rs b/src/main.rs index e9f44e3..42b5a60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,13 +5,14 @@ use std::{ #[allow(unused_imports)] use clap::Parser; +use config::RunnerMenuColors; use executables::get_executables; use runner::Runner; mod arguments; +mod config; mod executables; mod runner; -mod config; mod utils; fn main() -> Result<(), Box> { @@ -19,7 +20,16 @@ fn main() -> Result<(), Box> { let executables = get_executables()?; - let mut runner = Runner::new(args.prompt, executables); + let mut runner = Runner::new( + args.prompt, + executables, + RunnerMenuColors { + font_color: args.font_color, + font_color_active: args.font_color_active, + background_color: args.background_color, + background_color_active: args.background_color_active, + }, + ); if let Some(program) = runner.run() { run_program(program); diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 4c1a547..4423e42 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -1,4 +1,4 @@ -use std::{ops::Add, time::Duration}; +use std::time::Duration; use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use sdl2::{ @@ -12,10 +12,7 @@ use sdl2::{ }; use crate::{ - config::{ - BACKGROUND_COLOR, BACKGROUND_COLOR_SELECTED, FONT_COLOR, FONT_COLOR_SELECTED, - FONT_POINT_SIZE, LINE_SPACING, MAX_ITEM_DISPLAY_COUNT, PADDING, - }, + config::{RunnerMenuColors, FONT_POINT_SIZE, LINE_SPACING, MAX_ITEM_DISPLAY_COUNT, PADDING}, utils::color_from_hex, }; @@ -27,10 +24,11 @@ pub struct Runner { ttf: ttf::Sdl2TtfContext, input: String, window_size: (u32, u32), + colors: RunnerMenuColors, } impl Runner { - pub fn new(prompt: String, executables: Vec) -> Self { + pub fn new(prompt: String, executables: Vec, colors: RunnerMenuColors) -> Self { let context = sdl2::init().expect("Error creating SDL context"); let ttf = ttf::init().expect("Error creating SDL TTF context"); @@ -75,6 +73,7 @@ impl Runner { input: String::from(""), ttf, window_size, + colors, } } @@ -84,8 +83,10 @@ impl Runner { let mut selection_index: u16 = 0; let mut filtered_executables = self.executables.clone(); - let background_color = color_from_hex(BACKGROUND_COLOR).unwrap(); - let background_color_selected = color_from_hex(BACKGROUND_COLOR_SELECTED).unwrap(); + let background_color = color_from_hex(&self.colors.background_color).unwrap(); + let background_color_active = color_from_hex(&self.colors.background_color_active).unwrap(); + let font_color = color_from_hex(&self.colors.font_color).unwrap(); + let font_color_active = color_from_hex(&self.colors.font_color_active).unwrap(); let font_path = String::from("/usr/share/fonts/OTF/GeistMonoNerdFontMono-Regular.otf"); @@ -94,10 +95,6 @@ impl Runner { .load_font(font_path.clone(), FONT_POINT_SIZE) .expect(&format!("Error loading font {}", font_path)); - let font_color = color_from_hex(FONT_COLOR).expect("Error loading FONT_COLOR"); - let font_color_selected = - color_from_hex(FONT_COLOR_SELECTED).expect("Error loading FONT_COLOR_SELECTED"); - let creator = self.canvas.texture_creator(); let mut event_pump = self.context.event_pump().unwrap(); @@ -228,7 +225,7 @@ impl Runner { .blended(if i != selection_index { font_color } else { - font_color_selected + font_color_active }) .expect("Error rendering text"); @@ -245,7 +242,7 @@ impl Runner { self.canvas.set_draw_color(if i != selection_index { background_color } else { - background_color_selected + background_color_active }); let _ = self.canvas.fill_rect(background_rect);