completed say function

This commit is contained in:
Vomitblood 2024-04-22 23:48:43 +08:00
parent 976cc75483
commit d7d7205d38
4 changed files with 118 additions and 4 deletions

View file

@ -3,7 +3,7 @@
pkgname=rustmon-git pkgname=rustmon-git
pkgdesc="Pokemon Colorscripts written in Rust" pkgdesc="Pokemon Colorscripts written in Rust"
_gitname=rustmon _gitname=rustmon
pkgver=r47.31f64ae pkgver=r49.976cc75
pkgrel=1 pkgrel=1
arch=('x86_64') arch=('x86_64')
url="https://github.com/Vomitblood/$_gitname" url="https://github.com/Vomitblood/$_gitname"

View file

@ -2,5 +2,6 @@ pub mod constants;
pub mod fetch; pub mod fetch;
pub mod list; pub mod list;
pub mod print; pub mod print;
pub mod say;
pub mod structs; pub mod structs;
pub mod validation; pub mod validation;

View file

@ -15,6 +15,9 @@
- `pokedex` - Print Pokemon by list of space-separated Pokedex numbers. Use `0` to print a random Pokemon. - `pokedex` - Print Pokemon by list of space-separated Pokedex numbers. Use `0` to print a random Pokemon.
- `shiny` - Rate of printing the shiny version of the colorscript - `shiny` - Rate of printing the shiny version of the colorscript
- `spacing` - Number of spaces between colorscripts - `spacing` - Number of spaces between colorscripts
## `say` - Print a speaking Pokemon
- `text` - Input text for Pokemon to say
*/ */
/// Pokemon Colorscripts written in Rust /// Pokemon Colorscripts written in Rust
@ -33,7 +36,7 @@ fn main() {
println!("Verbose: {verbose}"); println!("Verbose: {verbose}");
// invoke bigchungus fetch function // invoke bigchungus fetch function
rustmon::fetch::fetch(extract_destination, verbose) rustmon::fetch::fetch(extract_destination, verbose);
} else if let Some(list_args) = args.subcommand_matches("list") { } else if let Some(list_args) = args.subcommand_matches("list") {
// list // list
@ -49,7 +52,7 @@ fn main() {
eprintln!("Error: {e}"); eprintln!("Error: {e}");
std::process::exit(1); std::process::exit(1);
} }
} };
} else { } else {
// list/forms // list/forms
match rustmon::list::print_pokemon_forms(pokemon_name) { match rustmon::list::print_pokemon_forms(pokemon_name) {
@ -58,7 +61,7 @@ fn main() {
eprintln!("Error: {e}"); eprintln!("Error: {e}");
std::process::exit(1); std::process::exit(1);
} }
} };
} }
} else if let Some(print_args) = args.subcommand_matches("print") { } else if let Some(print_args) = args.subcommand_matches("print") {
// print // print
@ -77,6 +80,15 @@ fn main() {
// print // print
rustmon::print::print(big, forms, hide_name, names, pokedexes, shiny_rate, spacing); rustmon::print::print(big, forms, hide_name, names, pokedexes, shiny_rate, spacing);
} else if let Some(say_args) = args.subcommand_matches("say") {
// say
// validate files first
rustmon::validation::validate_files();
let text: &String = say_args.get_one::<String>("text").unwrap();
rustmon::say::say(text);
} }
} }
@ -207,6 +219,25 @@ For more advanced usage, use `less` or `more` to scroll through the list!",
.required(false), .required(false),
) )
) )
// say subcommand
.subcommand(
clap::Command::new("say")
.about("Print a speaking Pokemon")
.after_help(
"Tip: Pipe the output to `rustmon say` to see the Pokemon speak!
Example: `echo \"Never gonna give you up\" | rustmon say`"
)
// say/text
.arg(
clap::Arg::new("text")
.help("Input text for Pokemon to say")
.short('t')
.long("text")
.default_value("")
.hide_default_value(true)
.required(false)
)
)
.subcommand_required(true) .subcommand_required(true)
// finalize // finalize
.get_matches(); .get_matches();

82
src/say.rs Normal file
View file

@ -0,0 +1,82 @@
pub fn say(text: &str) {
// first prioritise input text
if !text.is_empty() {
// if input text was provided
let content = split_into_lines(text);
print_speech_bubble(&content);
crate::print::print(
false,
vec![&"regular".to_string()],
true,
vec![&"random".to_string()],
vec![0],
0.0,
0,
)
} else {
// if no input text was provided
let buffer = read_from_stdin();
let content = split_into_lines(buffer.as_str());
print_speech_bubble(&content);
crate::print::print(
false,
vec![&"regular".to_string()],
true,
vec![&"random".to_string()],
vec![0],
0.0,
0,
)
}
}
fn read_from_stdin() -> String {
use std::io::Read;
let mut buffer = String::new();
std::io::stdin()
.read_to_string(&mut buffer)
.expect("Failed to read from stdin");
// trim newline character from end of buffer
buffer.trim_end().to_string()
}
fn split_into_lines(input: &str) -> Vec<String> {
input
// splits the input string on newlines
.lines()
// converts each line to a string
.map(|line| line.to_string())
// collects all lines into a Vec<String>
.collect()
}
fn print_speech_bubble(content: &[String]) {
// determine the maximum length of the content lines, with a minimum of 24
let mut max_length = content.iter().map(|line| line.len()).max().unwrap_or(0);
max_length = max_length.max(24); // Ensure minimum width of 20
// print the top border
println!("+{}+", "-".repeat(max_length + 2));
// print each line centered within the bubble
for line in content.iter() {
let padding = max_length - line.len();
// padding to the left
let left_padding = padding / 2;
// padding to the right to balance out any odd number of padding spaces
let right_padding = padding - left_padding;
println!(
"| {}{}{} |",
" ".repeat(left_padding),
line,
" ".repeat(right_padding)
);
}
// print the bottom border
println!("+{}+", "-".repeat(max_length + 2));
println!(" //");
println!(" //");
}