From 502298d47b526f841122d3b962a4daface863347 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 27 Jun 2024 22:19:53 +0200 Subject: [PATCH] feat(cli)!: remove printing::divider, add borderprint borderfmt and borderfmt_advanced divider was removed because it was too simple and code duplication makes therefore more sense for it's usecase. Refs: #84 --- members/libpt-cli/src/printing.rs | 108 ++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 7 deletions(-) diff --git a/members/libpt-cli/src/printing.rs b/members/libpt-cli/src/printing.rs index 808de0b..615b6e9 100644 --- a/members/libpt-cli/src/printing.rs +++ b/members/libpt-cli/src/printing.rs @@ -1,11 +1,105 @@ -//! # tools that make printing stuff better +use comfy_table::presets; +use comfy_table::{CellAlignment, ContentArrangement, Table}; +use console::{style, Color}; -/// Quickly get a one line visual divider -pub fn divider() -> String { - format!("{:=^80}", "=") +/// Prints content with a simple border around it. +/// +/// This function is a convenience wrapper around [blockfmt] and [println]. It automatically +/// formats the content with a border using the specified color and then prints it to the console. +/// +/// # Example +/// +/// ``` +/// use libpt_cli::console::Color; +/// use libpt_cli::printing::blockprint; +/// # fn main() { +/// blockprint("Hello world!".to_string(), Color::Blue); +/// # } +/// ``` +#[inline] +pub fn blockprint(content: impl ToString, color: Color) { + println!("{}", blockfmt(content, color)) } -/// Quickly print a one line visual divider -pub fn print_divider() { - println!("{:=^80}", "=") +/// Formats content with a simple border around it. +/// +/// This function is a convenience wrapper around [blockfmt_advanced] with preset values for +/// border style, content arrangement, and cell alignment. It automatically formats the content +/// with a border as large as possible and centers the content. The resulting cell is colored in +/// the specified color. +/// +/// # Example +/// +/// ``` +/// use libpt_cli::console::Color; +/// use libpt_cli::printing::blockfmt; +/// # fn main() { +/// let formatted_content = blockfmt("Hello world!".to_string(), Color::Blue); +/// println!("{}", formatted_content); +/// # } +/// ``` +#[inline] +pub fn blockfmt(content: impl ToString, color: Color) -> String { + blockfmt_advanced( + content, + color, + presets::UTF8_BORDERS_ONLY, + ContentArrangement::DynamicFullWidth, + CellAlignment::Center + ) +} + +/// Formats content with a border around it. +/// +/// Unless you are looking for something specific, use [blockfmt] or [blockprint]. +/// +/// The border can be created using box-drawing characters, and the content is formatted +/// within the border. The function allows customization of the border's color, preset, +/// content arrangement, and cell alignment. +/// +/// # Example +/// ``` +/// use libpt_cli::comfy_table::{presets, CellAlignment, ContentArrangement}; +/// use libpt_cli::console::Color; +/// use libpt_cli::printing::blockfmt_advanced; +/// # fn main() { +/// println!( +/// "{}", +/// blockfmt_advanced( +/// "Hello world!".to_string(), +/// Color::Blue, +/// presets::UTF8_FULL, +/// ContentArrangement::DynamicFullWidth, +/// CellAlignment::Center +/// ) +/// ); +/// # } +/// ``` +/// ```text +/// ┌────────────────────────────────────────────────────────────────────────────────────────┐ +/// │ Hello world! │ +/// └────────────────────────────────────────────────────────────────────────────────────────┘ +/// ``` +/// +/// # Parameters +/// +/// +pub fn blockfmt_advanced( + content: impl ToString, + color: Color, + preset: &str, + arrangement: ContentArrangement, + alignment: CellAlignment +) -> String { + let mut table = Table::new(); + table + .load_preset(preset) + .set_content_arrangement(arrangement) + .add_row(vec![content.to_string()]); + table + .column_mut(0) + .unwrap() + .set_cell_alignment(alignment); + + format!("{}", style(table).fg(color)) }