From b291745a3ef067e1696c7507505215e7b1d4c1f1 Mon Sep 17 00:00:00 2001 From: spiders Date: Thu, 5 May 2022 14:12:18 -0700 Subject: [PATCH] added preliminary kijefiles support --- src/bubbles.rs | 2 +- src/critters.rs | 38 +++++++++++++++++++++++++++++--------- src/main.rs | 26 ++++++++++++++++++++------ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/bubbles.rs b/src/bubbles.rs index 9635f87..0b8e30a 100644 --- a/src/bubbles.rs +++ b/src/bubbles.rs @@ -160,7 +160,7 @@ impl BubbleConfig { " ", ); let bubble_bottom = manipulate::pad_left( - &format!(" {} ", self.bottom.repeat(longest_length + 2),), + &format!(" {} \n", self.bottom.repeat(longest_length + 2),), left_pad_length, " ", ); diff --git a/src/critters.rs b/src/critters.rs index c1355a4..d2101c8 100644 --- a/src/critters.rs +++ b/src/critters.rs @@ -1,4 +1,6 @@ use super::kule::*; +use std::fs; +use std::io; use voca_rs::*; // represents inherent structural information about a critter @@ -45,11 +47,10 @@ impl CritterConfig { object: &Option, format: &Option, name: &Option, - ) -> CritterConfig { + ) -> Result { let kijetesantakalu = CritterTemplate { anchor: 14, - critter: r" -$8 $9$6 + critter: r"$8 $9$6 $8 /__ $9$6 $8 / $1$2\ $9$5 $8 | |$3$4 @@ -59,8 +60,7 @@ $8 (III|\|| $9$0" }; let kijetesantakalu_little = CritterTemplate { anchor: 13, - critter: r" -$8 $9$6 + critter: r"$8 $9$6 $8 /__ $9$6 $8 / $1$2\ $9$5 $8 | |$3$4 @@ -69,8 +69,7 @@ $8 (I|\|| $9$0" }; let soweli = CritterTemplate { anchor: 10, - critter: r" -$8 $9$6 + critter: r"$8 $9$6 $8 ___ $9$6 $8 $1$2) $9$5 $8 |||| $9$0" @@ -159,11 +158,11 @@ $8 |||| $9$0" "kijetesantakalu" => (), "lili" => config.template = kijetesantakalu_little, "soweli" => config.template = soweli, - _ => (), + name => config.template = template_from_file(&name)?, } } - return config; + return Ok(config); } // gives a fully formatted version of the critter @@ -183,3 +182,24 @@ $8 |||| $9$0" .replace("$0", &self.object); } } + +// attempts to interpret file as a path, and if this fails, tries appending it to every location in the kijepath environment variable. +pub fn template_from_file(name: &str) -> Result { + let file = fs::read_to_string(name) + .map_err(|_| "mi ken ala lukin e lipu kije\ncouldn't find/read kijefile")?; + let mut lines = file.lines().skip_while(|l| l.starts_with('#')); // skips comments + + let anchor: usize; + if let Some(anchor_line) = lines.next() { + anchor = anchor_line + .trim() + .parse() + .map_err(|_| "nanpa li nasa\ncouldn't parse anchor as number")?; + } else { + return Err("ale li weka tan lipu kije\nkijefile missing content"); + } + let mut critter = String::new(); + lines.for_each(|l| critter.push_str(&format!("{}\n", l))); + + Ok(CritterTemplate { anchor, critter }) +} diff --git a/src/main.rs b/src/main.rs index d6bf850..a3ed3e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,8 +21,16 @@ use voca_rs::*; fn main() { let cli = Args::parse(); let mut text = String::new(); - let (critter_config, bubble_config) = cli.configs_from_arguments(); - + let (critter_config, bubble_config); + match cli.configs_from_arguments() { + Err(s) => { + println!("pakala a!\n{}", s); + return; + } + Ok((c, b)) => { + (critter_config, bubble_config) = (c, b); + } + } if !cli.text.is_empty() { text = cli.text.join(" ") } else { @@ -104,7 +112,7 @@ struct Args { } impl Args { - fn configs_from_arguments(&self) -> (CritterConfig, BubbleConfig) { + fn configs_from_arguments(&self) -> Result<(CritterConfig, BubbleConfig), String> { let mut eyes = self.lukin.clone(); let mut tongue = self.uta.clone(); let mut line = self.palisa.clone(); @@ -206,8 +214,14 @@ impl Args { _ => String::new(), }) } - let critter_config = - CritterConfig::config_from_string(&eyes, &tongue, &line, &object, &Some(format), &name); + let critter_config = CritterConfig::config_from_string( + &eyes, + &tongue, + &line, + &object, + &Some(format), + &name, + )?; let bubble_config = BubbleConfig::config_from_string( critter_config.template.anchor, self.pakala, @@ -215,7 +229,7 @@ impl Args { &border, ); - (critter_config, bubble_config) + Ok((critter_config, bubble_config)) } }