Crates.io | colorize-macros |
lib.rs | colorize-macros |
version | 0.8.0 |
source | src |
created_at | 2024-02-16 18:26:04.553252 |
updated_at | 2024-05-16 23:12:19.738238 |
description | A set of Rust macros to assist in turning text into colors for printing on the terminal. |
homepage | |
repository | https://github.com/jpal91/colorize |
max_upload_size | |
id | 1142706 |
size | 10,333 |
A set of Rust macros to assist in turning text into colors for printing on the terminal.
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.
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"
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.
colorize!("{}, {}", b => "Hello", Fg-> "world")
where "Hello" and "world" are both bold but "world" is the only word that's green)This crate was originally inspired by the row macro in prettytable.