text input through invocation

it now supports non std in input directly from the invocation
This commit is contained in:
spiders 2022-04-28 22:26:28 -07:00
parent 1fd5b98c7f
commit b41efb3ee2
2 changed files with 122 additions and 98 deletions

View file

@ -26,107 +26,116 @@ pub struct CritterConfig {
pub template: CritterTemplate, pub template: CritterTemplate,
} }
const KIJETESANTAKALU: CritterTemplate = CritterTemplate { impl CritterConfig {
anchor: 14, pub fn config_from_string(
critter: r" eyes: Option<String>,
tongue: Option<String>,
line: Option<String>,
name: Option<String>,
) -> CritterConfig {
let kijetesantakalu: CritterTemplate = CritterTemplate {
anchor: 14,
critter: r"
$6 $6
/__ $6 /__ $6
/ $1$2\ $5 / $1$2\ $5
| |$3$4 | |$3$4
| | | |
(III|\||" (III|\||"
.to_string(), .to_string(),
}; };
const KIJETESANTAKALU_LITTLE: CritterTemplate = CritterTemplate { let kijetesantakalu_little: CritterTemplate = CritterTemplate {
anchor: 13, anchor: 13,
critter: r" critter: r"
$6 $6
/__ $6 /__ $6
/ $1$2\ $5 / $1$2\ $5
| |$3$4 | |$3$4
(I|\||" (I|\||"
.to_string(), .to_string(),
}; };
const DEFAULT_CONFIG: CritterConfig = CritterConfig { let default_config: CritterConfig = CritterConfig {
left_eye: String::from("."), left_eye: String::from("."),
right_eye: String::from("."), right_eye: String::from("."),
left_tongue: String::from(" "), left_tongue: String::from(" "),
right_tongue: String::from(" "), right_tongue: String::from(" "),
right_line: String::from("/"), right_line: String::from("/"),
up_line: String::from("|"), up_line: String::from("|"),
left_line: String::from("\\"), left_line: String::from("\\"),
template: KIJETESANTAKALU, template: kijetesantakalu,
}; };
pub fn config_from_string( let mut config = default_config.clone();
eyes: Option<&str>, if let Some(eyes) = eyes {
tongue: Option<&str>, match count::count_graphemes(&eyes) {
line: Option<&str>, 0 => (),
) -> CritterConfig { 1 => {
let mut config = DEFAULT_CONFIG.clone(); (config.left_eye, config.right_eye) =
(chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 0))
match count::count_graphemes(eyes) { }
0 => break, _ => {
(config.left_eye, config.right_eye) =
1 => (left_eye, right_eye) = (chop::grapheme_at(eyes, 0), chop::grapheme_at(eyes, 0)), (chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 1))
_ => (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)
} }
1 => (left_tongue, right_tongue) = (chop::grapheme_at(tongue, 0), " ".to_string()), if let Some(tongue) = tongue {
_ => { match count::count_graphemes(&tongue) {
(left_tongue, right_tongue) = 0 => (),
(chop::grapheme_at(tongue, 0), chop::grapheme_at(tongue, 1)) 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))
}
}
} }
} if let Some(line) = line {
match count::count_graphemes(line) { match count::count_graphemes(&line) {
0 => { 0 => (),
(left_line, up_line, right_line) = ( 1 => config.right_line = chop::grapheme_at(&line, 0),
DEFAULT_CONFIG.left_line, 2 => {
DEFAULT_CONFIG.up_line, (config.right_line, config.up_line) =
DEFAULT_CONFIG.right_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) = ( if let Some(name) = name {
chop::grapheme_at(line, 0), match name.as_str() {
DEFAULT_CONFIG.up_line, "kijetesantakalu" => (),
DEFAULT_CONFIG.right_line, "lili" => config.template = kijetesantakalu_little,
) _ => (),
} }
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),
)
} }
return config;
} }
return config; pub fn format_critter(&self) -> String {
} return self
.template
pub fn format_critter(critter: &str, critter_config: CritterConfig) -> String { .critter
return critter .replace("$1", &self.left_eye)
.replace("$1", &critter_config.left_eye) .replace("$2", &self.right_eye)
.replace("$2", &critter_config.right_eye) .replace("$3", &self.left_tongue)
.replace("$3", &critter_config.left_tongue) .replace("$4", &self.right_tongue)
.replace("$4", &critter_config.right_tongue) .replace("$5", &self.right_line)
.replace("$5", &critter_config.right_line) .replace("$6", &self.up_line)
.replace("$6", &critter_config.up_line) .replace("$7", &self.left_line);
.replace("$7", &critter_config.left_line); }
} }

View file

@ -23,30 +23,45 @@ struct Args {
#[clap(short, long)] #[clap(short, long)]
palisa: Option<String>, palisa: Option<String>,
#[clap(short, long)]
nimi: Option<String>,
text: Vec<String>,
} }
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() { fn main() {
let cli = Args::parse(); let cli = Args::parse();
let mut text = String::new(); let mut text = String::new();
let default_config = critters::config_from_string("", "", ""); let config = cli.config_from_arguments();
// io::stdin() if !cli.text.is_empty() {
// .read_to_string(&mut text) text = cli.text.join(" ")
// .expect("failed to read input"); } else {
io::stdin()
print!( .read_to_string(&mut text)
"{}", .expect("failed to read input");
bubbles::bubble_from_text( }
&text, output(&text, config)
critters::KIJETESANTAKALU.anchor,
DEFAULT_MAXIMUM_LINE_LENGTH
)
);
println!(
"{}",
critters::format_critter(critters::KIJETESANTAKALU_LITTLE.critter, default_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; const DEFAULT_MAXIMUM_LINE_LENGTH: usize = 40;