smn_rust_util 0.1.0

A collection of utility functions for Rust
Documentation
use crate::logging::logging_color::colorize;
use super::logging_color::Color;

/// Logs a message with a newline.
///
/// # Parameters
///
/// - `msg`: The message to log.
///
/// # Examples
///
pub fn logln(msg: &str) {
    println!("{}", msg);
}

/// Logs a colored message with a newline.
///
/// # Parameters
///
/// - `msg`: The message to log.
/// - `color`: The `Color` variant to apply.
///
/// # Examples
///
pub fn logln_color(msg: &str, color: Color) {
    println!("{}", colorize(msg, color));
}

/// Logs a message without a newline.
/// 
/// # Parameters
/// 
/// - `msg`: The message to log.
/// 
pub fn log(msg: &str) {
    print!("{}", msg);
}

/// Logs a colored message without a newline.
///
/// # Parameters
///
/// - `msg`: The message to log.
/// - `color`: The `Color` variant to apply.
///
pub fn log_color(msg: &str, color: Color) {
    print!("{}", colorize(msg, color));
}



/// Logs a horizontal line composed of dashes (`-`), colored as specified.
///
/// # Parameters
///
/// - `length`: The total length of the line.
/// - `color`: The `Color` variant to apply.
///
pub fn log_line(length: u32, color: Color) {
    let line = "-".repeat(length as usize);
    logln_color(&line, color);
}

/// Logs a header message centered within a line of dashes (`-`), maintaining the specified total length and color.
///
/// # Parameters
///
/// - `msg`: The header message to log.
/// - `length`: The total length of the line, including the message.
/// - `color`: The `Color` variant to apply.
/// 
pub fn log_line_header(msg: &str, length: u32, color: Color) {
    let msg_length = msg.len() as u32;

    // Ensure the line is at least as long as the message
    if length <= msg_length {
        logln_color(msg, color);
        return;
    }

    // Calculate padding length
    let total_padding = length - msg_length;
    let left_padding = total_padding / 2;
    let right_padding = total_padding - left_padding;

    // Construct the line
    let left_padding = "-".repeat(left_padding as usize);
    let right_padding = "-".repeat(right_padding as usize);
    let line = format!("{}{}{}", left_padding, msg, right_padding);

    logln_color(&line, color);
}