https://github.com/azur1s/vari
Vari (Väri) is a Rust library for formatting strings with colors and cosmetic stuff to the terminal.
https://github.com/azur1s/vari
ansi cli color colors cosmetics formatter rust style terminal
Last synced: 11 months ago
JSON representation
Vari (Väri) is a Rust library for formatting strings with colors and cosmetic stuff to the terminal.
- Host: GitHub
- URL: https://github.com/azur1s/vari
- Owner: azur1s
- License: agpl-3.0
- Created: 2022-01-12T16:56:44.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-15T19:03:28.000Z (over 4 years ago)
- Last Synced: 2025-04-13T04:46:08.878Z (about 1 year ago)
- Topics: ansi, cli, color, colors, cosmetics, formatter, rust, style, terminal
- Language: Rust
- Homepage:
- Size: 166 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Vari
[](https://crates.io/crates/vari)
[](https://docs.rs/vari/)
[](https://crates.io/crates/vari)
[](https://github.com/azur1s/vari#license)
Vari (Väri) is a Rust library for formatting strings with colors and cosmetic stuff to the terminal. Like [Rich](https://github.com/Textualize/rich) library for Python.
> Väri means "color" in Finnish.
## Installing
Vari come with color anchors as default feature,
The other opt-in features are: "log", "fun",
```toml
[dependencies]
vari = "0.2.1"
```
## Features
### Color Anchor
> This is in default features.
Color anchor are a bbcode-like markup for colors and styles (eg. "[\$red]", "[bg\$yellow]", "[\$bold]")
Anchors
Colors:
- [$black]
- [$red]
- [$green]
- [$yellow]
- [$blue]
- [$magenta]
- [$cyan]
- [$white]
- [$reset] or [$r] or [$/]
Bright colors:
- [$bright_black] or [$brightblack]
- [$bright_red] or [$brightred]
- [$bright_green] or [$brightgreen]
- [$bright_yellow] or [$brightyellow]
- [$bright_blue] or [$brightblue]
- [$bright_magenta] or [$brightmagenta]
- [$bright_cyan] or [$brightcyan]
- [$bright_white] or [$brightwhite]
Styles
- [$regular]
- [$bold]
- [$dim] or [$low] or [$low_intensity] or [$lowintensity]
- [$italic]
- [$underline]
- [$blink] or [$blinking]
- [$reverse] or [$reversed]
- [$invisible] or [$hidden]
- [$strikethrough] or [$strike_through]
Note: [bg$any] is a valid anchors, it will be translated to [$reversed][$any] (where `any` is the color/style name above)
```rust
// [$/] is shorthand for [$reset]
let message = vari::format("[$blue]Hello, [$green]World![$/]");
println!("{}", message);
// Custom RGB!
println!("{}", vari::format("[$[114, 119, 39]]#727727![$[66, 4, 32]] Do you see it?[$/]"));
// Style anchor and also easy macros :O
vprintln!("{}Bold and Italic :O{}", "[$bold][$italic]", "[$/]");
// Background color
vprintln!("{}Backgroundssss{}[$/]", "[bg$magenta]", "[bg$[188, 188, 188]]World![$/]")
// Hexadecimal
vprintln!("[$#ffffff]Hello, [$#000000]World![$/]");
```
### Colorize
```toml
[dependencies]
vari = { version = "0.2.1", features = ["colorize"] }
```
Colorize string directly by calling colorize() method, like [colored](https://github.com/mackwic/colored) crate.
For example: "red".colorize("red") is the same as "[\$red]red[$/]"
Note: Chaining is not yet implemented, because .colorize() adds [$/] so you can't chain styles
The argument should be the color's name (the same name as the anchor colors).
```rust
use vari::colorize::Colorize;
fn main() {
println!("{}", "Hello, World".colorize("cyan"));
println!("{}", "This is red".colorize("brightred"));
println!("{}", "Bold.".colorize("bold"));
}
```
### Log
```toml
[dependencies]
vari = { version = "0.2.1", features = ["log"] }
```
Some println-ish function for logging
```rust
let log_message = vformat!("[$green]This message is send by main.rs![$/]");
let log_file = vformat!("[$dim]src/main.rs[$/]");
vari::util::log(&log_message, &log_file);
```

### No ANSI for .len()
> This is in default features.
This might be used in padding calculation, because in colored string (eg. "\x1b[31mTest\x1b[0m"),
the length calculated also contains the "[31m" and the "[0m" in it, making the padding incorrect.
So this trait implements a ".no_ansi()" which remove all the ANSI escape sequence and then you could do
".len()" after it.
```rust
// vari::util::log()
// Calculate padding amount between the message.
// eg. left________right
let padding_amount = w - right.no_ansi().len() - left.no_ansi().len();
let padding = " ".repeat(padding_amount);
let mut result = String::new();
result.push_str(left);
result.push_str(&padding);
result.push_str(right);
return result
```

### Fun
```toml
[dependencies]
vari = { version = "0.2.1", features = ["fun"] }
```
```rust
fn main() {
// Rainbow colors!
println!("{}", vari::fun::rainbow("Rainbow!!!"));
}
```
## License
This crate is under [AGPL-3.0](https://www.gnu.org/licenses/agpl-3.0.en.html) license.