6 releases (3 breaking)
0.8.0 | May 16, 2024 |
---|---|
0.7.1 | Feb 26, 2024 |
0.6.4 | Feb 19, 2024 |
0.5.1 | Feb 16, 2024 |
#622 in Command-line interface
11KB
70 lines
colorize
A set of Rust macros to assist in turning text into colors for printing on the terminal.
Purpose
As I was working with another command line utility, I wanted the ability to convert regular text into ANSI color formatted text more easily, so I wrote a series of macros to help with formatting and/or printing that could be reusable.
Adding
You can add the macros to your project by using cargo
or adding colorize-macros
to your depedencies.
cargo add colorize-macros
[dependencies]
colorize-macros = "^0.8"
Usage
use colorize::{print_color, colorize};
// println "Hello world" in bold green
print_color!("{}", Fgb->"Hello world");
// Returns "Hello" in italic blue and "World" underlined in magenta
let color_string = colorize!("{} {}", iFb->"Hello", Fmu->"World");
assert_eq!(
String::from("\x1b[3;34mHello\x1b[0m \x1b[4;35mWorld\x1b[0m"),
color_string
);
// Add a format token to multiple inputs using `=>`
// The below example will produce "Hello" with a green foreground,
// "world" with a blue background, both in bold.
let color_string = colorize!("{}, {}", b => Fg->"Hello", Bb->"world");
assert_eq!(
String::from("\x1b[1;32mHello\x1b[0m \x1b[1;44mworld\x1b[0m"),
color_string
);
use std::path::PathBuf;
let user_path = PathBuf::from("/home/color/my_new_file.txt");
let pretty_path = colorize!("{:?}", Fgu->user_path.clone());
assert_eq!(
String::from("\x1b[32;4m\"/home/color/my_new_file.txt\"\x1b[0m"),
pretty_path
);
print_color!("{} {} {:?}"b => "Moving", Fy->user_path, "to", Fg->PathBuf::from("/home/new_color_dir/my_second_file.txt"));
See the colorize macro docs for further style specs.
Development
- Add background color
- Add ability to format multiple arguments with the same input (ie
colorize!("{}, {}", b => "Hello", Fg-> "world")
where "Hello" and "world" are both bold but "world" is the only word that's green) - Integrate a color set of log macros from the log crate
Special Thanks
This crate was originally inspired by the row macro in prettytable.
Dependencies
~245–700KB
~17K SLoC