From 473d5d0173b80917692bb316d8c681be16355510 Mon Sep 17 00:00:00 2001 From: Strubbelkopp Date: Fri, 25 Jul 2025 12:18:36 +0200 Subject: Add clap for more advanced argument parsing --- src/main.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e4d1b6e..d6313fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,23 @@ use crossterm::terminal::size; use std::env; use unicode_width::UnicodeWidthStr; +use clap::Parser; mod mikuart; use mikuart::get_miku_art; -fn main() -> Result<(), Box> { - let args: Vec = env::args().collect(); +#[derive(Parser)] +#[command(version, about, long_about = None)] +struct Args { + /// Text to display in the speech bubble + text: String, - if args.len() == 1 { - println!("Usage: mikusays "); - println!("Example: mikusays \"Hello, World!\""); - return Ok(()); - } +} - let text = args[1..].join(" "); +fn main() -> Result<(), Box> { + let args = Args::parse(); + + let text = args.text; draw_miku_says(&text)?; Ok(()) -- cgit v1.3-2-g0d8e From 5dda54ecd49a43e79a52ded2a0fc0f1017689197 Mon Sep 17 00:00:00 2001 From: Strubbelkopp Date: Fri, 25 Jul 2025 12:20:17 +0200 Subject: Add optional argument to select a specific style --- src/main.rs | 13 ++++++++++--- src/mikuart.rs | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index d6313fa..715d34b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,20 +12,27 @@ struct Args { /// Text to display in the speech bubble text: String, + /// Style of the Miku art. Random is chosen if not specified. + #[arg(short, long)] + style: Option, } fn main() -> Result<(), Box> { let args = Args::parse(); let text = args.text; - draw_miku_says(&text)?; + let style = match args.style { + Some(s) => s, + None => -1 // Default to -1 to select a random style + }; + draw_miku_says(&text, style)?; Ok(()) } -fn draw_miku_says(text: &str) -> Result<(), Box> { +fn draw_miku_says(text: &str, style: i32) -> Result<(), Box> { let speech_bubble_lines = get_speech_bubble_lines(text); - let miku_art = get_miku_art(); + let miku_art = get_miku_art(style); let (window_width, window_height) = size()?; let available_width = window_width as usize; diff --git a/src/mikuart.rs b/src/mikuart.rs index 5ba1849..6d7b5c9 100644 --- a/src/mikuart.rs +++ b/src/mikuart.rs @@ -3,7 +3,7 @@ use rand::Rng; -pub fn get_miku_art() -> Vec<&'static str> { +pub fn get_miku_art(style: i32) -> Vec<&'static str> { let mut rng = rand::rng(); let styles = vec![ //MARK: small art @@ -504,6 +504,6 @@ pub fn get_miku_art() -> Vec<&'static str> { ], ]; - let selected_style = rng.random_range(0..styles.len()); + let selected_style = if style < 0 { rng.random_range(0..styles.len()) } else { style as usize }; styles[selected_style].clone() } -- cgit v1.3-2-g0d8e From 7a3ed2ba020cc36b8dac59a07e9925e6d1a50710 Mon Sep 17 00:00:00 2001 From: Strubbelkopp Date: Fri, 25 Jul 2025 12:52:06 +0200 Subject: Run cargo fmt --- src/main.rs | 6 +++--- src/mikuart.rs | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 715d34b..354b88c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ +use clap::Parser; use crossterm::terminal::size; use std::env; use unicode_width::UnicodeWidthStr; -use clap::Parser; mod mikuart; use mikuart::get_miku_art; @@ -19,11 +19,11 @@ struct Args { fn main() -> Result<(), Box> { let args = Args::parse(); - + let text = args.text; let style = match args.style { Some(s) => s, - None => -1 // Default to -1 to select a random style + None => -1, // Default to -1 to select a random style }; draw_miku_says(&text, style)?; diff --git a/src/mikuart.rs b/src/mikuart.rs index 4197c60..44915b6 100644 --- a/src/mikuart.rs +++ b/src/mikuart.rs @@ -505,8 +505,16 @@ pub fn get_miku_art(style: i32) -> Vec<&'static str> { ]; if style > 0 && style as usize >= styles.len() { - panic!("Style {} does not exist! Chooese one below {}", style, styles.len()); + panic!( + "Style {} does not exist! Chooese one below {}", + style, + styles.len() + ); } - let selected_style = if style < 0 { rng.random_range(0..styles.len()) } else { style as usize }; + let selected_style = if style < 0 { + rng.random_range(0..styles.len()) + } else { + style as usize + }; styles[selected_style].clone() } -- cgit v1.3-2-g0d8e From 82e2ec3fbddbb67cec79505866fb8c2308eec429 Mon Sep 17 00:00:00 2001 From: Strubbelkopp Date: Fri, 25 Jul 2025 12:57:34 +0200 Subject: Use unwrap_or() instead of match block; remove unused import --- src/main.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 354b88c..657bcc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use clap::Parser; use crossterm::terminal::size; -use std::env; use unicode_width::UnicodeWidthStr; mod mikuart; @@ -21,10 +20,7 @@ fn main() -> Result<(), Box> { let args = Args::parse(); let text = args.text; - let style = match args.style { - Some(s) => s, - None => -1, // Default to -1 to select a random style - }; + let style = args.style.unwrap_or(-1); // Default to -1 to select a random style draw_miku_says(&text, style)?; Ok(()) -- cgit v1.3-2-g0d8e From a810883f63f821fea42cc7dbcff70e5697aef6a1 Mon Sep 17 00:00:00 2001 From: Strubbelkopp Date: Fri, 25 Jul 2025 13:08:25 +0200 Subject: Adjust argument description --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 657bcc1..e0ffba7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ struct Args { /// Text to display in the speech bubble text: String, - /// Style of the Miku art. Random is chosen if not specified. + /// Style of the Miku art. A random one is chosen if not specified #[arg(short, long)] style: Option, } -- cgit v1.3-2-g0d8e