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

57
Cargo.lock generated
View File

@@ -109,6 +109,34 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]]
name = "cstr"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68523903c8ae5aacfa32a0d9ae60cadeb764e1da14ee0d26b1f3089f13a54636"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "dlib"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412"
dependencies = [
"libloading",
]
[[package]]
name = "fontconfig"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9b79b619a4ae048ea79e927376b1d10294979bda195b0c052fc958be96c62d9"
dependencies = [
"yeslogic-fontconfig-sys",
]
[[package]] [[package]]
name = "fuzzy-matcher" name = "fuzzy-matcher"
version = "0.3.7" version = "0.3.7"
@@ -151,17 +179,34 @@ version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "libloading"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if",
"windows-targets",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.19.0" version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]] [[package]]
name = "practicalrunner" name = "practicalrunner"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"fontconfig",
"fuzzy-matcher", "fuzzy-matcher",
"home", "home",
"sdl2", "sdl2",
@@ -325,3 +370,15 @@ name = "windows_x86_64_msvc"
version = "0.52.5" version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
[[package]]
name = "yeslogic-fontconfig-sys"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb6b23999a8b1a997bf47c7bb4d19ad4029c3327bb3386ebe0a5ff584b33c7a"
dependencies = [
"cstr",
"dlib",
"once_cell",
"pkg-config",
]

View File

@@ -13,6 +13,7 @@ strip = true
[dependencies] [dependencies]
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
fontconfig = "0.8.0"
fuzzy-matcher = "0.3.7" fuzzy-matcher = "0.3.7"
home = "0.5.9" home = "0.5.9"
sdl2 = { version = "0.36.0", features = ["ttf"] } sdl2 = { version = "0.36.0", features = ["ttf"] }

View File

@@ -11,6 +11,9 @@ pub struct Arguments {
#[arg(short, long, help = "The menu's prompt message", default_value_t = String::from(""))] #[arg(short, long, help = "The menu's prompt message", default_value_t = String::from(""))]
pub prompt: String, 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))] #[arg(long, help = "The default font color", default_value_t = String::from(FONT_COLOR))]
pub font_color: String, pub font_color: String,

View File

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

View File

@@ -24,6 +24,7 @@ fn main() -> Result<(), Box<dyn Error>> {
args.prompt, args.prompt,
executables, executables,
RunnerMenuSettings { RunnerMenuSettings {
font: args.font,
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,

View File

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

View File

@@ -1,8 +1,12 @@
use fontconfig::Fontconfig;
use sdl2::pixels::Color; use sdl2::pixels::Color;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ColorParseError; pub struct ColorParseError;
#[derive(Debug, Clone)]
pub struct FontFetchError;
pub fn color_from_hex(hex: &str) -> Result<Color, ColorParseError> { pub fn color_from_hex(hex: &str) -> Result<Color, ColorParseError> {
if hex.len() != 7 || hex.chars().nth(0).unwrap() != '#' { if hex.len() != 7 || hex.chars().nth(0).unwrap() != '#' {
return Err(ColorParseError); return Err(ColorParseError);
@@ -14,3 +18,17 @@ pub fn color_from_hex(hex: &str) -> Result<Color, ColorParseError> {
Ok(Color::RGB(r, g, b)) 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)
}