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 {
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>) -> 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 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,69 +142,47 @@ 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(
1 => bubble_body.push_str(&manipulate::pad_left(
&format!("{} {} {}\n", self.left, lines[0], self.right),
left_pad_length,
" "
),
bubble_bottom
)
}
" ",
)),
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",
self.bottom_left,
lines[n - 1],
self.bottom_right
"{} {}{} {}\n",
left,
line,
" ".repeat(longest_length - count::count_graphemes(&line)),
right
),
left_pad_length,
" ",
));
}
}
}
return format!("{}{}{}", bubble_top, bubble_body, bubble_bottom);
}
}
}
}
fn wrap_once(text: &str, max_length: usize) -> (String, Option<String>) {

View file

@ -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())
}