Overview
This crate allows you to format log messages with colors and styles, convert hex colors to RGBA values, and print styled tables easily. It integrates well with the colored crate for rich text styling in the console.
Table of Contents
Installation
Run the following Cargo command in your project directory:
cargo add styledlog
or add styledlog to your Cargo.toml file:
[]
= "MAJOR.MINOR.PATCH" # Replace with the latest version
Usage
You can start using styledlog by including it in your main file:
use *;
Example
let formatted_text = style_text!;
println!;
To see the full features and usage examples of styledlog, please visit the following link:
Full Features and Usage Examples
Modules
colorful
This module provides color utilities for styling strings with colors defined in hex format, allowing for both general use and custom color definitions through the CustomColorTrait.
formatter
This module contains functions for formatting output, including:
-
style_text!: A macro for formatting strings by replacing placeholders within a template. The macro allows users to inject dynamic values into a string while applying color and styling to those values. It creates an easy way to format complex output without the need for manual string concatenation. -
hex_to_rgba(hex: &str) -> Result<(u8, u8, u8, f32), String>: Converts a hex color string to RGBA values. It parses various hex color formats (e.g.,#RRGGBB,#RRGGBBAA,#RGB,#RGBA) and handles errors gracefully. -
print_table(...): A utility function for printing a stylized table with headers, rows, and footers. It calculates column widths dynamically based on the content and formats the table for easy readability in the console.
level
This module handles log levels and their styles. It contains:
-
log_level(newline_before: &str, level: &str, style_level: Style, message: &str, action: &str): Logs a message based on the provided log level and style. The action can be "show" or "hide". -
add_level(level: &'static str): Adds a log level to the allowed list for display. -
remove_level(level: &str): Removes a log level from the allowed list.
The Style struct allows you to define new styles for logs, including color bold, italic and underline embellishments.
Examples
Coloring Text
You can change the color of your text by calling:
let text = "Hello, World!".to_custom_color;
println!;
Creating a Table with Style
To print a table, use print_table:
use *;
Logging Message Levels
This is an example of how to log message levels:
use *;
License
This project is licensed under the MIT or Apache 2.0 License - see the LICENSE file for details.
Contributing
Contributions are welcome! If you have suggestions or improvements, feel free to submit an issue or a pull request.