From 84b186451beef47004cdb955e699ce47f9f22b4d Mon Sep 17 00:00:00 2001 From: 409 Date: Wed, 22 May 2024 00:48:48 +0200 Subject: [PATCH] feat: row argument --- src/arguments.rs | 8 +++++++- src/config.rs | 3 ++- src/main.rs | 5 +++-- src/runner/mod.rs | 23 ++++++++++++----------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/arguments.rs b/src/arguments.rs index d407fb7..368a7b4 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -1,6 +1,9 @@ use clap::Parser; -use crate::config::{FONT_COLOR, FONT_COLOR_ACTIVE, BACKGROUND_COLOR, BACKGROUND_COLOR_ACTIVE}; +use crate::config::{ + BACKGROUND_COLOR, BACKGROUND_COLOR_ACTIVE, FONT_COLOR, FONT_COLOR_ACTIVE, + MAX_ITEM_DISPLAY_COUNT, +}; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -19,4 +22,7 @@ pub struct Arguments { #[arg(long, help = "The background color of the active item", default_value_t = String::from(BACKGROUND_COLOR_ACTIVE))] pub background_color_active: String, + + #[arg(short, long, help = "The amount of items to display at once", default_value_t = MAX_ITEM_DISPLAY_COUNT)] + pub rows: u16, } diff --git a/src/config.rs b/src/config.rs index 77d0124..a69efd9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,9 +9,10 @@ pub const FONT_COLOR_ACTIVE: &str = "#1e1e2e"; pub const BACKGROUND_COLOR: &str = "#1e1e2e"; pub const BACKGROUND_COLOR_ACTIVE: &str = "#89b4fa"; -pub struct RunnerMenuColors { +pub struct RunnerMenuSettings { pub font_color: String, pub font_color_active: String, pub background_color: String, pub background_color_active: String, + pub rows: u16, } diff --git a/src/main.rs b/src/main.rs index 42b5a60..baaeb9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::{ #[allow(unused_imports)] use clap::Parser; -use config::RunnerMenuColors; +use config::RunnerMenuSettings; use executables::get_executables; use runner::Runner; @@ -23,11 +23,12 @@ fn main() -> Result<(), Box> { let mut runner = Runner::new( args.prompt, executables, - RunnerMenuColors { + RunnerMenuSettings { font_color: args.font_color, font_color_active: args.font_color_active, background_color: args.background_color, background_color_active: args.background_color_active, + rows: args.rows, }, ); diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 4423e42..735c3cb 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -12,7 +12,7 @@ use sdl2::{ }; use crate::{ - config::{RunnerMenuColors, FONT_POINT_SIZE, LINE_SPACING, MAX_ITEM_DISPLAY_COUNT, PADDING}, + config::{RunnerMenuSettings, FONT_POINT_SIZE, LINE_SPACING, MAX_ITEM_DISPLAY_COUNT, PADDING}, utils::color_from_hex, }; @@ -24,11 +24,11 @@ pub struct Runner { ttf: ttf::Sdl2TtfContext, input: String, window_size: (u32, u32), - colors: RunnerMenuColors, + settings: RunnerMenuSettings, } impl Runner { - pub fn new(prompt: String, executables: Vec, colors: RunnerMenuColors) -> Self { + pub fn new(prompt: String, executables: Vec, colors: RunnerMenuSettings) -> Self { let context = sdl2::init().expect("Error creating SDL context"); let ttf = ttf::init().expect("Error creating SDL TTF context"); @@ -73,7 +73,7 @@ impl Runner { input: String::from(""), ttf, window_size, - colors, + settings: colors, } } @@ -83,10 +83,11 @@ impl Runner { let mut selection_index: u16 = 0; let mut filtered_executables = self.executables.clone(); - 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 background_color = color_from_hex(&self.settings.background_color).unwrap(); + let background_color_active = + color_from_hex(&self.settings.background_color_active).unwrap(); + let font_color = color_from_hex(&self.settings.font_color).unwrap(); + let font_color_active = color_from_hex(&self.settings.font_color_active).unwrap(); let font_path = String::from("/usr/share/fonts/OTF/GeistMonoNerdFontMono-Regular.otf"); @@ -210,9 +211,9 @@ impl Runner { // try to keep the selection centered let executables_len: u16 = filtered_executables.len() as u16; let start: u16 = selection_index - .saturating_sub(MAX_ITEM_DISPLAY_COUNT.div_euclid(2)) - .min(executables_len.saturating_sub(MAX_ITEM_DISPLAY_COUNT)); - let end: u16 = (start + MAX_ITEM_DISPLAY_COUNT).min(executables_len); + .saturating_sub(self.settings.rows.div_euclid(2)) + .min(executables_len.saturating_sub(self.settings.rows)); + let end: u16 = (start + self.settings.rows).min(executables_len); // --- let mut display_count: u16 = 0;