From 89d8339e3de274ca658feea8cf27bbcfb8139167 Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Wed, 8 Mar 2023 11:34:43 -0500 Subject: [PATCH] [+] Chat bubble --- tngame-rs/src/cowsay.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tngame-rs/src/cowsay.rs diff --git a/tngame-rs/src/cowsay.rs b/tngame-rs/src/cowsay.rs new file mode 100644 index 0000000..98d53a7 --- /dev/null +++ b/tngame-rs/src/cowsay.rs @@ -0,0 +1,25 @@ +use crate::AsciiArt; + +pub fn gen_bubble(text: &str) -> String { + let mut o = String::with_capacity(text.len() + 100); + let mut lines = text.lines().map(|line| line.trim()); + let max_width = lines.clone().map(|line| line.len()).max().unwrap(); + + o.push_str("."); + o.push_str("=".repeat(max_width + 2).as_str()); + o.push_str(".\n"); + for line in lines { + o.push_str("| "); + o.push_str(line); + o.push_str(" ".repeat(max_width - line.len()).as_str()); + o.push_str(" |\n"); + } + o.push_str("."); + o.push_str("=".repeat(max_width + 2).as_str()); + o.push_str(".\n"); + o +} + +pub fn gen_bubble_ascii(text: &str) -> AsciiArt { + AsciiArt::new(&gen_bubble(text), "cowsay") +} \ No newline at end of file