From f91159e0ce709f3b3964fa99ae81eea60fb229a8 Mon Sep 17 00:00:00 2001 From: comfysage <67917529+comfysage@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:01:54 +0200 Subject: [PATCH] refactor!: separate lib and cli --- src/lib.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 107 ++++---------------------------------------------- 2 files changed, 117 insertions(+), 100 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..6256922 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,110 @@ +use textwrap::wrap; + +use unicode_width::UnicodeWidthStr; + +pub static KITTY: &str = " + /l、 + (゚、 。 7 + l ~ヽ + じしf_,)ノ + "; + +pub struct FormatOptions { + pub think: bool, + pub width: u16, +} + +struct Chars { + arrow: &'static str, + top: &'static str, + bottom: &'static str, + left: &'static str, + right: &'static str, + single_left: &'static str, + single_right: &'static str, + angled_up_right: &'static str, + angled_up_left: &'static str, + angled_down_right: &'static str, + angled_down_left: &'static str, +} + +static SAY_CHARS: Chars = Chars { + arrow: "\\", + top: "-", + bottom: "-", + left: "|", + right: "|", + single_left: "<", + single_right: ">", + angled_up_right: "/", + angled_up_left: "\\", + angled_down_right: "\\", + angled_down_left: "/", +}; + +static THINK_CHARS: Chars = Chars { + arrow: "○", + top: "⏜", + bottom: "⏝", + left: "(", + right: ")", + single_left: "(", + single_right: ")", + angled_up_right: "⎛", + angled_up_left: "⎞", + angled_down_right: "⎝", + angled_down_left: "⎠", +}; +pub fn generate(message: &str, format_opts: FormatOptions) -> String { + let think = format_opts.think; + let width = format_opts.width; + + let chars = if think { &THINK_CHARS } else { &SAY_CHARS }; + let mut lines = wrap(&message, width as usize); + let longest = lines.iter().map(|line| line.width()).max().unwrap(); + return format!( + " + {} +{} + {} + {} + {}", + chars.top.repeat(longest), + if lines.len() == 1 { + format!("{} {} {}", chars.single_left, lines[0], chars.single_right) + } else { + let mut result = format!( + "{} {}{}{}", + chars.angled_up_right, + lines[0], + " ".repeat(longest - lines[0].width() + 1), + chars.angled_up_left + ); + lines.remove(0); + let last = lines.pop().unwrap(); + + for line in lines { + result = format!( + "{}\n{} {}{}{}", + result, + chars.left, + line, + " ".repeat(longest - line.width() + 1), + chars.right, + ); + } + + format!( + "{}\n{} {}{}{}", + result, + chars.angled_down_right, + last, + " ".repeat(longest - last.width() + 1), + chars.angled_down_left, + ) + }, + chars.bottom.repeat(longest), + chars.arrow, + chars.arrow, + ); +} diff --git a/src/main.rs b/src/main.rs index a9495f4..d146a45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,50 +5,7 @@ use clap_stdin::MaybeStdin; use crossterm::terminal; -use textwrap::wrap; -use unicode_width::UnicodeWidthStr; - -struct Chars { - arrow: &'static str, - top: &'static str, - bottom: &'static str, - left: &'static str, - right: &'static str, - single_left: &'static str, - single_right: &'static str, - angled_up_right: &'static str, - angled_up_left: &'static str, - angled_down_right: &'static str, - angled_down_left: &'static str, -} - -static SAY_CHARS: Chars = Chars { - arrow: "\\", - top: "-", - bottom: "-", - left: "|", - right: "|", - single_left: "<", - single_right: ">", - angled_up_right: "/", - angled_up_left: "\\", - angled_down_right: "\\", - angled_down_left: "/", -}; - -static THINK_CHARS: Chars = Chars { - arrow: "○", - top: "⏜", - bottom: "⏝", - left: "(", - right: ")", - single_left: "(", - single_right: ")", - angled_up_right: "⎛", - angled_up_left: "⎞", - angled_down_right: "⎝", - angled_down_left: "⎠", -}; +use kittysay::{generate, FormatOptions, KITTY}; #[derive(Parser)] #[command(version, about)] @@ -73,56 +30,13 @@ fn main() -> Result<()> { let (cols, _) = terminal::size()?; let width = args.width.unwrap_or(45).min(cols.saturating_sub(5)); - let chars = if args.think { &THINK_CHARS } else { &SAY_CHARS }; - let mut lines = wrap(&args.message, width as usize); - let longest = lines.iter().map(|line| line.width()).max().unwrap(); + let format_opts = FormatOptions { + think: args.think, + width, + }; - let msg = format!( - " - {} -{} - {} - {} - {}", - chars.top.repeat(longest), - if lines.len() == 1 { - format!("{} {} {}", chars.single_left, lines[0], chars.single_right) - } else { - let mut result = format!( - "{} {}{}{}", - chars.angled_up_right, - lines[0], - " ".repeat(longest - lines[0].width() + 1), - chars.angled_up_left - ); - lines.remove(0); - let last = lines.pop().unwrap(); - - for line in lines { - result = format!( - "{}\n{} {}{}{}", - result, - chars.left, - line, - " ".repeat(longest - line.width() + 1), - chars.right, - ); - } - - format!( - "{}\n{} {}{}{}", - result, - chars.angled_down_right, - last, - " ".repeat(longest - last.width() + 1), - chars.angled_down_left, - ) - }, - chars.bottom.repeat(longest), - chars.arrow, - chars.arrow, - ); + let msg = generate(&args.message, format_opts); let mut msg_color = console::Color::White; let mut cat_color = console::Color::White; @@ -131,17 +45,10 @@ fn main() -> Result<()> { cat_color = console::Color::Color256(colors[1]); } - let cat = " - /l、 - (゚、 。 7 - l ~ヽ - じしf_,)ノ - "; - println!( "{}{}", console::style(msg).fg(msg_color), - console::style(cat).fg(cat_color) + console::style(KITTY).fg(cat_color) ); Ok(())