feat: row argument

This commit is contained in:
2024-05-22 00:48:48 +02:00
parent 2c8773ff5c
commit 84b186451b
4 changed files with 24 additions and 15 deletions

View File

@@ -1,6 +1,9 @@
use clap::Parser; 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)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[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))] #[arg(long, help = "The background color of the active item", default_value_t = String::from(BACKGROUND_COLOR_ACTIVE))]
pub background_color_active: String, 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,
} }

View File

@@ -9,9 +9,10 @@ pub const FONT_COLOR_ACTIVE: &str = "#1e1e2e";
pub const BACKGROUND_COLOR: &str = "#1e1e2e"; pub const BACKGROUND_COLOR: &str = "#1e1e2e";
pub const BACKGROUND_COLOR_ACTIVE: &str = "#89b4fa"; pub const BACKGROUND_COLOR_ACTIVE: &str = "#89b4fa";
pub struct RunnerMenuColors { pub struct RunnerMenuSettings {
pub font_color: String, pub font_color: String,
pub font_color_active: String, pub font_color_active: String,
pub background_color: String, pub background_color: String,
pub background_color_active: String, pub background_color_active: String,
pub rows: u16,
} }

View File

@@ -5,7 +5,7 @@ use std::{
#[allow(unused_imports)] #[allow(unused_imports)]
use clap::Parser; use clap::Parser;
use config::RunnerMenuColors; use config::RunnerMenuSettings;
use executables::get_executables; use executables::get_executables;
use runner::Runner; use runner::Runner;
@@ -23,11 +23,12 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut runner = Runner::new( let mut runner = Runner::new(
args.prompt, args.prompt,
executables, executables,
RunnerMenuColors { RunnerMenuSettings {
font_color: args.font_color, font_color: args.font_color,
font_color_active: args.font_color_active, font_color_active: args.font_color_active,
background_color: args.background_color, background_color: args.background_color,
background_color_active: args.background_color_active, background_color_active: args.background_color_active,
rows: args.rows,
}, },
); );

View File

@@ -12,7 +12,7 @@ use sdl2::{
}; };
use crate::{ 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, utils::color_from_hex,
}; };
@@ -24,11 +24,11 @@ pub struct Runner {
ttf: ttf::Sdl2TtfContext, ttf: ttf::Sdl2TtfContext,
input: String, input: String,
window_size: (u32, u32), window_size: (u32, u32),
colors: RunnerMenuColors, settings: RunnerMenuSettings,
} }
impl Runner { impl Runner {
pub fn new(prompt: String, executables: Vec<String>, colors: RunnerMenuColors) -> Self { pub fn new(prompt: String, executables: Vec<String>, colors: RunnerMenuSettings) -> Self {
let context = sdl2::init().expect("Error creating SDL context"); let context = sdl2::init().expect("Error creating SDL context");
let ttf = ttf::init().expect("Error creating SDL TTF context"); let ttf = ttf::init().expect("Error creating SDL TTF context");
@@ -73,7 +73,7 @@ impl Runner {
input: String::from(""), input: String::from(""),
ttf, ttf,
window_size, window_size,
colors, settings: colors,
} }
} }
@@ -83,10 +83,11 @@ impl Runner {
let mut selection_index: u16 = 0; let mut selection_index: u16 = 0;
let mut filtered_executables = self.executables.clone(); let mut filtered_executables = self.executables.clone();
let background_color = color_from_hex(&self.colors.background_color).unwrap(); let background_color = color_from_hex(&self.settings.background_color).unwrap();
let background_color_active = color_from_hex(&self.colors.background_color_active).unwrap(); let background_color_active =
let font_color = color_from_hex(&self.colors.font_color).unwrap(); color_from_hex(&self.settings.background_color_active).unwrap();
let font_color_active = color_from_hex(&self.colors.font_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"); let font_path = String::from("/usr/share/fonts/OTF/GeistMonoNerdFontMono-Regular.otf");
@@ -210,9 +211,9 @@ impl Runner {
// try to keep the selection centered // try to keep the selection centered
let executables_len: u16 = filtered_executables.len() as u16; let executables_len: u16 = filtered_executables.len() as u16;
let start: u16 = selection_index let start: u16 = selection_index
.saturating_sub(MAX_ITEM_DISPLAY_COUNT.div_euclid(2)) .saturating_sub(self.settings.rows.div_euclid(2))
.min(executables_len.saturating_sub(MAX_ITEM_DISPLAY_COUNT)); .min(executables_len.saturating_sub(self.settings.rows));
let end: u16 = (start + MAX_ITEM_DISPLAY_COUNT).min(executables_len); let end: u16 = (start + self.settings.rows).min(executables_len);
// --- // ---
let mut display_count: u16 = 0; let mut display_count: u16 = 0;