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,7 +26,14 @@ pub struct CritterConfig {
pub template: CritterTemplate,
}
const KIJETESANTAKALU: CritterTemplate = CritterTemplate {
impl CritterConfig {
pub fn config_from_string(
eyes: Option<String>,
tongue: Option<String>,
line: Option<String>,
name: Option<String>,
) -> CritterConfig {
let kijetesantakalu: CritterTemplate = CritterTemplate {
anchor: 14,
critter: r"
$6
@ -38,7 +45,7 @@ const KIJETESANTAKALU: CritterTemplate = CritterTemplate {
.to_string(),
};
const KIJETESANTAKALU_LITTLE: CritterTemplate = CritterTemplate {
let kijetesantakalu_little: CritterTemplate = CritterTemplate {
anchor: 13,
critter: r"
$6
@ -49,7 +56,7 @@ const KIJETESANTAKALU_LITTLE: CritterTemplate = CritterTemplate {
.to_string(),
};
const DEFAULT_CONFIG: CritterConfig = CritterConfig {
let default_config: CritterConfig = CritterConfig {
left_eye: String::from("."),
right_eye: String::from("."),
@ -60,73 +67,75 @@ const DEFAULT_CONFIG: CritterConfig = CritterConfig {
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)
}
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))
}
}
match count::count_graphemes(line) {
0 => {
(left_line, up_line, right_line) = (
DEFAULT_CONFIG.left_line,
DEFAULT_CONFIG.up_line,
DEFAULT_CONFIG.right_line,
)
}
let mut config = default_config.clone();
if let Some(eyes) = eyes {
match count::count_graphemes(&eyes) {
0 => (),
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,
)
(config.left_eye, config.right_eye) =
(chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 0))
}
_ => {
(left_line, up_line, right_line) = (
chop::grapheme_at(line, 0),
chop::grapheme_at(line, 1),
chop::grapheme_at(line, 2),
(config.left_eye, config.right_eye) =
(chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 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))
}
}
}
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),
)
}
}
}
if let Some(name) = name {
match name.as_str() {
"kijetesantakalu" => (),
"lili" => config.template = kijetesantakalu_little,
_ => (),
}
}
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);
}
}

View file

@ -23,30 +23,45 @@ struct Args {
#[clap(short, long)]
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() {
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;