feat: custom font argument

This commit is contained in:
2024-05-22 04:08:36 +02:00
parent 8a5ca4a74b
commit 47a1ac8235
7 changed files with 94 additions and 8 deletions

View File

@@ -11,6 +11,9 @@ pub struct Arguments {
#[arg(short, long, help = "The menu's prompt message", default_value_t = String::from(""))]
pub prompt: String,
#[arg(long, help = "The font to use for the menu.")]
pub font: Option<String>,
#[arg(long, help = "The default font color", default_value_t = String::from(FONT_COLOR))]
pub font_color: String,

View File

@@ -18,4 +18,5 @@ pub struct RunnerMenuSettings {
pub font_size: u16,
pub line_spacing: u16,
pub display_index: Option<u8>,
pub font: Option<String>,
}

View File

@@ -24,6 +24,7 @@ fn main() -> Result<(), Box<dyn Error>> {
args.prompt,
executables,
RunnerMenuSettings {
font: args.font,
font_color: args.font_color,
font_color_active: args.font_color_active,
background_color: args.background_color,

View File

@@ -13,7 +13,7 @@ use sdl2::{
use crate::{
config::{RunnerMenuSettings, PADDING},
utils::color_from_hex,
utils::{color_from_hex, get_font_path},
};
pub struct Runner {
@@ -22,6 +22,7 @@ pub struct Runner {
context: Sdl,
canvas: Canvas<Window>,
ttf: ttf::Sdl2TtfContext,
font_path: String,
input: String,
window_size: (u32, u32),
settings: RunnerMenuSettings,
@@ -33,15 +34,20 @@ impl Runner {
let ttf = ttf::init().expect("Error creating SDL TTF context");
let font_path: String;
let (window_width, window_height): (u32, u32);
window_width = 480;
{
let font_path = String::from("/usr/share/fonts/OTF/GeistMonoNerdFontMono-Regular.otf");
font_path = get_font_path(match settings.font {
Some(ref font_p) => font_p.clone(),
None => String::from("Monospace"),
})
.expect("Could not load fonts");
let font = ttf
.load_font(font_path.clone(), settings.font_size)
.expect(&format!("Error loading font {}", font_path));
.load_font(&font_path, settings.font_size)
.expect(&format!("Error loading font {}", &font_path));
window_height = (PADDING
+ ((font.height() as u16 + settings.line_spacing) * (1 + settings.rows))
@@ -103,6 +109,7 @@ impl Runner {
ttf,
window_size: (window_width, window_height),
settings,
font_path,
}
}
@@ -118,12 +125,10 @@ impl Runner {
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 = self
.ttf
.load_font(font_path.clone(), self.settings.font_size)
.expect(&format!("Error loading font {}", font_path));
.load_font(&self.font_path, self.settings.font_size)
.expect(&format!("Error loading font {}", self.font_path));
let creator = self.canvas.texture_creator();

View File

@@ -1,8 +1,12 @@
use fontconfig::Fontconfig;
use sdl2::pixels::Color;
#[derive(Debug, Clone)]
pub struct ColorParseError;
#[derive(Debug, Clone)]
pub struct FontFetchError;
pub fn color_from_hex(hex: &str) -> Result<Color, ColorParseError> {
if hex.len() != 7 || hex.chars().nth(0).unwrap() != '#' {
return Err(ColorParseError);
@@ -14,3 +18,17 @@ pub fn color_from_hex(hex: &str) -> Result<Color, ColorParseError> {
Ok(Color::RGB(r, g, b))
}
pub fn get_font_path(font_name: String) -> Result<String, FontFetchError> {
let fc = Fontconfig::new().ok_or(FontFetchError)?;
let font = fc.find(&font_name, None).ok_or(FontFetchError)?;
let font_path = font
.path
.into_os_string()
.into_string()
.or(Err(FontFetchError))?;
Ok(font_path)
}