fixed bubble rendering bug, renamed text method

bubble_from_lines was also cleaned up significantly.
This commit is contained in:
spiders 2022-05-04 17:17:50 -07:00
parent 6e0aeb53e7
commit 905ea38557
2 changed files with 34 additions and 60 deletions

View file

@ -122,23 +122,19 @@ impl BubbleConfig {
} }
} }
pub fn text(&self, text: &str) -> String { // front end to bubble_from_lines which takes normal text.
self.bubble_from_lines(wrap_block(text, self.wrap)) 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>) -> String { // generates a bubble (doesn't end in newline) using a vector of strings as lines.
fn bubble_from_lines(&self, lines: &Vec<String>) -> String {
let longest_length: usize; 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() { match lines.iter().map(|s| count::count_graphemes(s)).max() {
None => return "".to_string(), None => return "".to_string(),
Some(l) => longest_length = l, 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 { let left_pad_length = if longest_length < self.anchor {
self.anchor + (longest_length / 2) + 2 self.anchor + (longest_length / 2) + 2
} else { } else {
@ -146,70 +142,48 @@ impl BubbleConfig {
}; };
let bubble_top = manipulate::pad_left( let bubble_top = manipulate::pad_left(
&format!( &format!(" {} \n", self.top.repeat(longest_length + 2),),
" {}{}{} \n",
self.top,
self.top.repeat(line_length),
self.top
),
left_pad_length, left_pad_length,
" ", " ",
); );
let bubble_bottom = manipulate::pad_left( let bubble_bottom = manipulate::pad_left(
&format!( &format!(" {} ", self.bottom.repeat(longest_length + 2),),
" {}{}{} ",
self.bottom,
self.bottom.repeat(line_length),
self.bottom
),
left_pad_length, left_pad_length,
" ", " ",
); );
let mut bubble_body = String::new(); let mut bubble_body = String::new();
match lines.len() { match lines.len() {
1 => { 1 => bubble_body.push_str(&manipulate::pad_left(
return format!(
"{}{}{}",
bubble_top,
manipulate::pad_left(
&format!("{} {} {}\n", self.left, lines[0], self.right), &format!("{} {} {}\n", self.left, lines[0], self.right),
left_pad_length, left_pad_length,
" " " ",
), )),
bubble_bottom
)
}
n => { n => {
bubble_body.push_str(&manipulate::pad_left( for (i, line) in lines.iter().enumerate() {
&format!("{} {} {}\n", self.top_left, lines[0], self.top_right), let (left, right) = if i == 0 {
left_pad_length, (self.top_left.clone(), self.top_right.clone())
" ", } else if i == n - 1 {
)); (self.bottom_left.clone(), self.bottom_right.clone())
if n > 2 { } else {
for i in 1..n - 1 { (self.middle_left.clone(), self.middle_right.clone())
bubble_body.push_str(&manipulate::pad_left( };
&format!("{} {} {}\n", self.middle_left, lines[i], self.middle_right),
left_pad_length,
" ",
));
}
}
bubble_body.push_str(&manipulate::pad_left( bubble_body.push_str(&manipulate::pad_left(
&format!( &format!(
"{} {} {}\n", "{} {}{} {}\n",
self.bottom_left, left,
lines[n - 1], line,
self.bottom_right " ".repeat(longest_length - count::count_graphemes(&line)),
right
), ),
left_pad_length, left_pad_length,
" ", " ",
)); ));
}
}
}
return format!("{}{}{}", bubble_top, bubble_body, bubble_bottom); return format!("{}{}{}", bubble_top, bubble_body, bubble_bottom);
} }
} }
}
}
fn wrap_once(text: &str, max_length: usize) -> (String, Option<String>) { fn wrap_once(text: &str, max_length: usize) -> (String, Option<String>) {
let whitespace_not_newline = |c: char| c.is_whitespace() && c != '\n'; let whitespace_not_newline = |c: char| c.is_whitespace() && c != '\n';

View file

@ -189,7 +189,7 @@ impl Args {
let bubble_config = BubbleConfig::config_from_string( let bubble_config = BubbleConfig::config_from_string(
critter_config.template.anchor, critter_config.template.anchor,
DEFAULT_MAXIMUM_LINE_LENGTH, DEFAULT_MAXIMUM_LINE_LENGTH,
Some("".to_string()), None,
); );
(critter_config, bubble_config) (critter_config, bubble_config)
@ -197,7 +197,7 @@ impl Args {
} }
fn output(text: &str, critter_config: CritterConfig, bubble_config: BubbleConfig) -> () { 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()) println!("{}", critter_config.format_critter())
} }