Skip to content

jpal91/colorize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crates.io docs.rs

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.

About

Rust macros to assist in adding ANSI color escape codes to text

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages