From b41efb3ee2a9139dfee0fa24237369e50532a110 Mon Sep 17 00:00:00 2001 From: spiders Date: Thu, 28 Apr 2022 22:26:28 -0700 Subject: [PATCH] text input through invocation it now supports non std in input directly from the invocation --- src/critters.rs | 169 +++++++++++++++++++++++++----------------------- src/main.rs | 51 +++++++++------ 2 files changed, 122 insertions(+), 98 deletions(-) diff --git a/src/critters.rs b/src/critters.rs index c207639..1da6d62 100644 --- a/src/critters.rs +++ b/src/critters.rs @@ -26,107 +26,116 @@ pub struct CritterConfig { pub template: CritterTemplate, } -const KIJETESANTAKALU: CritterTemplate = CritterTemplate { - anchor: 14, - critter: r" +impl CritterConfig { + pub fn config_from_string( + eyes: Option, + tongue: Option, + line: Option, + name: Option, + ) -> CritterConfig { + let kijetesantakalu: CritterTemplate = CritterTemplate { + anchor: 14, + critter: r" $6 /__ $6 / $1$2\ $5 | |$3$4 | | (III|\||" - .to_string(), -}; + .to_string(), + }; -const KIJETESANTAKALU_LITTLE: CritterTemplate = CritterTemplate { - anchor: 13, - critter: r" + let kijetesantakalu_little: CritterTemplate = CritterTemplate { + anchor: 13, + critter: r" $6 /__ $6 / $1$2\ $5 | |$3$4 (I|\||" - .to_string(), -}; + .to_string(), + }; -const DEFAULT_CONFIG: CritterConfig = CritterConfig { - left_eye: String::from("."), - right_eye: String::from("."), + let default_config: CritterConfig = CritterConfig { + left_eye: String::from("."), + right_eye: String::from("."), - left_tongue: String::from(" "), - right_tongue: String::from(" "), + left_tongue: String::from(" "), + right_tongue: String::from(" "), - right_line: String::from("/"), - up_line: String::from("|"), - left_line: String::from("\\"), + right_line: String::from("/"), + up_line: String::from("|"), + left_line: String::from("\\"), - template: KIJETESANTAKALU, -}; + template: kijetesantakalu, + }; -pub fn config_from_string( - eyes: Option<&str>, - tongue: Option<&str>, - line: Option<&str>, -) -> CritterConfig { - let mut config = DEFAULT_CONFIG.clone(); - - match count::count_graphemes(eyes) { - 0 => break, - - 1 => (left_eye, right_eye) = (chop::grapheme_at(eyes, 0), chop::grapheme_at(eyes, 0)), - _ => (left_eye, right_eye) = (chop::grapheme_at(eyes, 0), chop::grapheme_at(eyes, 1)), - } - match count::count_graphemes(tongue) { - 0 => { - (left_tongue, right_tongue) = (DEFAULT_CONFIG.left_tongue, DEFAULT_CONFIG.right_tongue) + let mut config = default_config.clone(); + if let Some(eyes) = eyes { + match count::count_graphemes(&eyes) { + 0 => (), + 1 => { + (config.left_eye, config.right_eye) = + (chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 0)) + } + _ => { + (config.left_eye, config.right_eye) = + (chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 1)) + } + } } - 1 => (left_tongue, right_tongue) = (chop::grapheme_at(tongue, 0), " ".to_string()), - _ => { - (left_tongue, right_tongue) = - (chop::grapheme_at(tongue, 0), chop::grapheme_at(tongue, 1)) + if let Some(tongue) = tongue { + match count::count_graphemes(&tongue) { + 0 => (), + 1 => { + (config.left_tongue, config.right_tongue) = + (chop::grapheme_at(&tongue, 0), " ".to_string()) + } + _ => { + (config.left_tongue, config.right_tongue) = + (chop::grapheme_at(&tongue, 0), chop::grapheme_at(&tongue, 1)) + } + } } - } - match count::count_graphemes(line) { - 0 => { - (left_line, up_line, right_line) = ( - DEFAULT_CONFIG.left_line, - DEFAULT_CONFIG.up_line, - DEFAULT_CONFIG.right_line, - ) + if let Some(line) = line { + match count::count_graphemes(&line) { + 0 => (), + 1 => config.right_line = chop::grapheme_at(&line, 0), + 2 => { + (config.right_line, config.up_line) = + (chop::grapheme_at(&line, 0), chop::grapheme_at(&line, 1)) + } + _ => { + (config.right_line, config.up_line, config.left_line) = ( + chop::grapheme_at(&line, 0), + chop::grapheme_at(&line, 1), + chop::grapheme_at(&line, 2), + ) + } + } } - 1 => { - (left_line, up_line, right_line) = ( - chop::grapheme_at(line, 0), - DEFAULT_CONFIG.up_line, - DEFAULT_CONFIG.right_line, - ) - } - 2 => { - (left_line, up_line, right_line) = ( - chop::grapheme_at(line, 0), - chop::grapheme_at(line, 1), - DEFAULT_CONFIG.right_line, - ) - } - _ => { - (left_line, up_line, right_line) = ( - chop::grapheme_at(line, 0), - chop::grapheme_at(line, 1), - chop::grapheme_at(line, 2), - ) + + if let Some(name) = name { + match name.as_str() { + "kijetesantakalu" => (), + "lili" => config.template = kijetesantakalu_little, + _ => (), + } } + + return config; } - return config; -} - -pub fn format_critter(critter: &str, critter_config: CritterConfig) -> String { - return critter - .replace("$1", &critter_config.left_eye) - .replace("$2", &critter_config.right_eye) - .replace("$3", &critter_config.left_tongue) - .replace("$4", &critter_config.right_tongue) - .replace("$5", &critter_config.right_line) - .replace("$6", &critter_config.up_line) - .replace("$7", &critter_config.left_line); + pub fn format_critter(&self) -> String { + return self + .template + .critter + .replace("$1", &self.left_eye) + .replace("$2", &self.right_eye) + .replace("$3", &self.left_tongue) + .replace("$4", &self.right_tongue) + .replace("$5", &self.right_line) + .replace("$6", &self.up_line) + .replace("$7", &self.left_line); + } } diff --git a/src/main.rs b/src/main.rs index 5c1d389..7eead65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,30 +23,45 @@ struct Args { #[clap(short, long)] palisa: Option, + + #[clap(short, long)] + nimi: Option, + + text: Vec, } + +impl Args { + fn config_from_arguments(&self) -> critters::CritterConfig { + critters::CritterConfig::config_from_string( + self.lukin.clone(), + self.uta.clone(), + self.palisa.clone(), + self.nimi.clone(), + ) + } +} + fn main() { let cli = Args::parse(); let mut text = String::new(); - let default_config = critters::config_from_string("", "", ""); + let config = cli.config_from_arguments(); - // io::stdin() - // .read_to_string(&mut text) - // .expect("failed to read input"); - - print!( - "{}", - bubbles::bubble_from_text( - &text, - critters::KIJETESANTAKALU.anchor, - DEFAULT_MAXIMUM_LINE_LENGTH - ) - ); - println!( - "{}", - critters::format_critter(critters::KIJETESANTAKALU_LITTLE.critter, default_config) - ); + if !cli.text.is_empty() { + text = cli.text.join(" ") + } else { + io::stdin() + .read_to_string(&mut text) + .expect("failed to read input"); + } + output(&text, config) } -// functions for producing formatted text +fn output(text: &str, config: critters::CritterConfig) -> () { + print!( + "{}", + bubbles::bubble_from_text(text, config.template.anchor, DEFAULT_MAXIMUM_LINE_LENGTH) + ); + println!("{}", config.format_critter()) +} const DEFAULT_MAXIMUM_LINE_LENGTH: usize = 40;