2024-06-28 00:40:52 +02:00
|
|
|
//! Utilities for formatting, bordering, aligning and printing text content
|
2024-06-27 22:31:36 +02:00
|
|
|
//!
|
|
|
|
//! This module provides functions for formatting content with borders and colors, printing them to the console.
|
|
|
|
//! The functions in this module are designed to simplify the process of creating visually appealing
|
|
|
|
//! output for CLI applications.
|
2024-06-28 14:40:50 +02:00
|
|
|
//!
|
|
|
|
//! Note that most of the utilities in this module are focused on communication with humans, not
|
2024-07-09 17:39:06 +02:00
|
|
|
//! with machines. Consider evaluating [`std::io::IsTerminal`] before using colorful, dynamic and bordered
|
2024-06-28 14:50:13 +02:00
|
|
|
//! printing. If you are talking to a machine, it might be useful to not add extra space, add a
|
|
|
|
//! newline per output or even output JSON. An example that does this well is `ls`:
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! $ ls
|
|
|
|
//! Cargo.lock Cargo.toml data LICENSE members README.md scripts src target
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! $ ls | cat
|
|
|
|
//! Cargo.lock
|
|
|
|
//! Cargo.toml
|
|
|
|
//! data
|
|
|
|
//! LICENSE
|
|
|
|
//! members
|
|
|
|
//! README.md
|
|
|
|
//! scripts
|
|
|
|
//! src
|
|
|
|
//! target
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! See the [CLI Rustbook](https://rust-cli.github.io/book/in-depth/machine-communication.html) for
|
|
|
|
//! more information on the topic.
|
2024-06-27 22:31:36 +02:00
|
|
|
|
2024-06-27 22:19:53 +02:00
|
|
|
use comfy_table::presets;
|
|
|
|
use comfy_table::{CellAlignment, ContentArrangement, Table};
|
|
|
|
use console::{style, Color};
|
2023-07-09 17:53:20 +02:00
|
|
|
|
2024-06-27 22:31:36 +02:00
|
|
|
/// Prints content with a simple border around it
|
2024-06-27 22:19:53 +02:00
|
|
|
///
|
|
|
|
/// 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() {
|
2024-07-09 18:04:47 +02:00
|
|
|
/// blockprint("Hello world!", Color::Blue);
|
2024-06-27 22:19:53 +02:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2024-07-09 18:04:47 +02:00
|
|
|
#[allow(clippy::needless_pass_by_value)] // we just take an impl, using a &impl is much less ergonomic
|
2024-06-27 22:19:53 +02:00
|
|
|
pub fn blockprint(content: impl ToString, color: Color) {
|
2024-07-09 17:39:06 +02:00
|
|
|
println!("{}", blockfmt(content, color));
|
2023-07-09 17:53:20 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 22:31:36 +02:00
|
|
|
/// Formats content with a simple border around it
|
2024-06-27 22:19:53 +02:00
|
|
|
///
|
2024-07-09 17:39:06 +02:00
|
|
|
/// This function is a convenience wrapper around [`blockfmt_advanced`] with preset values for
|
2024-06-27 22:19:53 +02:00
|
|
|
/// 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() {
|
2024-07-09 18:04:47 +02:00
|
|
|
/// let formatted_content = blockfmt("Hello world!", Color::Blue);
|
2024-06-27 22:19:53 +02:00
|
|
|
/// println!("{}", formatted_content);
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2024-07-09 18:04:47 +02:00
|
|
|
#[allow(clippy::needless_pass_by_value)] // we just take an impl, using a &impl is much less ergonomic
|
2024-06-27 22:19:53 +02:00
|
|
|
pub fn blockfmt(content: impl ToString, color: Color) -> String {
|
|
|
|
blockfmt_advanced(
|
|
|
|
content,
|
2024-06-27 22:22:46 +02:00
|
|
|
Some(color),
|
2024-06-27 22:19:53 +02:00
|
|
|
presets::UTF8_BORDERS_ONLY,
|
|
|
|
ContentArrangement::DynamicFullWidth,
|
2024-06-27 22:22:46 +02:00
|
|
|
CellAlignment::Center,
|
2024-06-27 22:19:53 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-27 22:31:36 +02:00
|
|
|
/// Formats content with a border around it
|
2024-06-27 22:19:53 +02:00
|
|
|
///
|
|
|
|
/// 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(
|
2024-07-09 18:04:47 +02:00
|
|
|
/// "Hello world!",
|
2024-06-27 22:22:46 +02:00
|
|
|
/// Some(Color::Blue),
|
2024-06-27 22:19:53 +02:00
|
|
|
/// presets::UTF8_FULL,
|
|
|
|
/// ContentArrangement::DynamicFullWidth,
|
|
|
|
/// CellAlignment::Center
|
|
|
|
/// )
|
|
|
|
/// );
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
/// ```text
|
|
|
|
/// ┌────────────────────────────────────────────────────────────────────────────────────────┐
|
|
|
|
/// │ Hello world! │
|
|
|
|
/// └────────────────────────────────────────────────────────────────────────────────────────┘
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
2024-06-27 22:26:18 +02:00
|
|
|
/// - `content`: The content to be formatted within the border
|
|
|
|
/// - `color`: The color of the border and text
|
|
|
|
/// - `preset`: The preset style for the border
|
|
|
|
/// - `arrangement`: The arrangement of the the border (e.g., stretch to sides, wrap around )
|
|
|
|
/// - `alignment`: The alignment of the content within the cells (e.g., left, center, right)
|
2024-07-09 18:04:47 +02:00
|
|
|
#[allow(clippy::missing_panics_doc)] // we add a row then unwrap it, no panic should be possible
|
|
|
|
#[allow(clippy::needless_pass_by_value)] // we just take an impl, using a &impl is much less ergonomic
|
2024-06-27 22:19:53 +02:00
|
|
|
pub fn blockfmt_advanced(
|
|
|
|
content: impl ToString,
|
2024-06-27 22:22:46 +02:00
|
|
|
color: Option<Color>,
|
2024-06-27 22:19:53 +02:00
|
|
|
preset: &str,
|
|
|
|
arrangement: ContentArrangement,
|
2024-06-27 22:22:46 +02:00
|
|
|
alignment: CellAlignment,
|
2024-06-27 22:19:53 +02:00
|
|
|
) -> String {
|
|
|
|
let mut table = Table::new();
|
|
|
|
table
|
|
|
|
.load_preset(preset)
|
|
|
|
.set_content_arrangement(arrangement)
|
|
|
|
.add_row(vec![content.to_string()]);
|
2024-06-27 22:22:46 +02:00
|
|
|
table.column_mut(0).unwrap().set_cell_alignment(alignment);
|
2024-06-27 22:19:53 +02:00
|
|
|
|
2024-06-27 22:22:46 +02:00
|
|
|
match color {
|
|
|
|
Some(c) => format!("{}", style(table).fg(c)),
|
|
|
|
None => table.to_string(),
|
|
|
|
}
|
2023-07-09 17:53:20 +02:00
|
|
|
}
|