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, 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, anchor: 14,
critter: r" critter: r"
$6 $6
@ -38,7 +45,7 @@ const KIJETESANTAKALU: CritterTemplate = CritterTemplate {
.to_string(), .to_string(),
}; };
const KIJETESANTAKALU_LITTLE: CritterTemplate = CritterTemplate { let kijetesantakalu_little: CritterTemplate = CritterTemplate {
anchor: 13, anchor: 13,
critter: r" critter: r"
$6 $6
@ -49,7 +56,7 @@ const KIJETESANTAKALU_LITTLE: CritterTemplate = CritterTemplate {
.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("."),
@ -60,73 +67,75 @@ const DEFAULT_CONFIG: CritterConfig = CritterConfig {
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 {
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,
)
}
1 => { 1 => {
(left_line, up_line, right_line) = ( (config.left_eye, config.right_eye) =
chop::grapheme_at(line, 0), (chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 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) = ( (config.left_eye, config.right_eye) =
chop::grapheme_at(line, 0), (chop::grapheme_at(&eyes, 0), chop::grapheme_at(&eyes, 1))
chop::grapheme_at(line, 1), }
chop::grapheme_at(line, 2), }
}
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; return config;
} }
pub fn format_critter(critter: &str, critter_config: CritterConfig) -> String { pub fn format_critter(&self) -> String {
return critter return self
.replace("$1", &critter_config.left_eye) .template
.replace("$2", &critter_config.right_eye) .critter
.replace("$3", &critter_config.left_tongue) .replace("$1", &self.left_eye)
.replace("$4", &critter_config.right_tongue) .replace("$2", &self.right_eye)
.replace("$5", &critter_config.right_line) .replace("$3", &self.left_tongue)
.replace("$6", &critter_config.up_line) .replace("$4", &self.right_tongue)
.replace("$7", &critter_config.left_line); .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)] #[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;