From 905ea38557147c34e9ff08c50d575a60389665e3 Mon Sep 17 00:00:00 2001 From: spiders Date: Wed, 4 May 2022 17:17:50 -0700 Subject: [PATCH] fixed bubble rendering bug, renamed text method bubble_from_lines was also cleaned up significantly. --- src/bubbles.rs | 90 ++++++++++++++++++-------------------------------- src/main.rs | 4 +-- 2 files changed, 34 insertions(+), 60 deletions(-) diff --git a/src/bubbles.rs b/src/bubbles.rs index 5675070..76145a2 100644 --- a/src/bubbles.rs +++ b/src/bubbles.rs @@ -122,23 +122,19 @@ impl BubbleConfig { } } - pub fn text(&self, text: &str) -> String { - self.bubble_from_lines(wrap_block(text, self.wrap)) + // front end to bubble_from_lines which takes normal text. + pub fn bubble_from_text(&self, text: &str) -> String { + self.bubble_from_lines(&wrap_block(text, self.wrap)) } - fn bubble_from_lines(&self, lines: Vec) -> String { + // generates a bubble (doesn't end in newline) using a vector of strings as lines. + fn bubble_from_lines(&self, lines: &Vec) -> String { let longest_length: usize; - let lengths: Vec<(&String, usize)> = lines - .iter() - .zip(lines.iter().map(|s| count::count_graphemes(s))) - .collect(); match lines.iter().map(|s| count::count_graphemes(s)).max() { None => return "".to_string(), Some(l) => longest_length = l, }; - // let line_length = cmp::max(longest_length, min_length); - let line_length = longest_length; let left_pad_length = if longest_length < self.anchor { self.anchor + (longest_length / 2) + 2 } else { @@ -146,68 +142,46 @@ impl BubbleConfig { }; let bubble_top = manipulate::pad_left( - &format!( - " {}{}{} \n", - self.top, - self.top.repeat(line_length), - self.top - ), + &format!(" {} \n", self.top.repeat(longest_length + 2),), left_pad_length, " ", ); let bubble_bottom = manipulate::pad_left( - &format!( - " {}{}{} ", - self.bottom, - self.bottom.repeat(line_length), - self.bottom - ), + &format!(" {} ", self.bottom.repeat(longest_length + 2),), left_pad_length, " ", ); let mut bubble_body = String::new(); - match lines.len() { - 1 => { - return format!( - "{}{}{}", - bubble_top, - manipulate::pad_left( - &format!("{} {} {}\n", self.left, lines[0], self.right), - left_pad_length, - " " - ), - bubble_bottom - ) - } + 1 => bubble_body.push_str(&manipulate::pad_left( + &format!("{} {} {}\n", self.left, lines[0], self.right), + left_pad_length, + " ", + )), n => { - bubble_body.push_str(&manipulate::pad_left( - &format!("{} {} {}\n", self.top_left, lines[0], self.top_right), - left_pad_length, - " ", - )); - if n > 2 { - for i in 1..n - 1 { - bubble_body.push_str(&manipulate::pad_left( - &format!("{} {} {}\n", self.middle_left, lines[i], self.middle_right), - left_pad_length, - " ", - )); - } + for (i, line) in lines.iter().enumerate() { + let (left, right) = if i == 0 { + (self.top_left.clone(), self.top_right.clone()) + } else if i == n - 1 { + (self.bottom_left.clone(), self.bottom_right.clone()) + } else { + (self.middle_left.clone(), self.middle_right.clone()) + }; + bubble_body.push_str(&manipulate::pad_left( + &format!( + "{} {}{} {}\n", + left, + line, + " ".repeat(longest_length - count::count_graphemes(&line)), + right + ), + left_pad_length, + " ", + )); } - bubble_body.push_str(&manipulate::pad_left( - &format!( - "{} {} {}\n", - self.bottom_left, - lines[n - 1], - self.bottom_right - ), - left_pad_length, - " ", - )); - return format!("{}{}{}", bubble_top, bubble_body, bubble_bottom); } } + return format!("{}{}{}", bubble_top, bubble_body, bubble_bottom); } } diff --git a/src/main.rs b/src/main.rs index 6edded6..900a22c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -189,7 +189,7 @@ impl Args { let bubble_config = BubbleConfig::config_from_string( critter_config.template.anchor, DEFAULT_MAXIMUM_LINE_LENGTH, - Some("".to_string()), + None, ); (critter_config, bubble_config) @@ -197,7 +197,7 @@ impl Args { } fn output(text: &str, critter_config: CritterConfig, bubble_config: BubbleConfig) -> () { - print!("{}", bubble_config.text(text)); + print!("{}", bubble_config.bubble_from_text(text)); println!("{}", critter_config.format_critter()) }