vari/lib.rs
1#![doc = include_str!("../README.md")]
2#![doc(html_logo_url = "https://raw.githubusercontent.com/azur1s/vari/master/assets/vari_logo.png")]
3
4pub mod anchor;
5pub mod util;
6
7#[cfg(feature = "fun")]
8pub mod fun;
9
10#[cfg(feature = "colorize")]
11pub mod colorize;
12
13#[cfg(feature = "log")]
14pub mod log;
15
16/// Format a string with ANSI escape sequences.
17///
18/// Parse color anchor by detecting if there is a color anchor
19/// and then replace it with ANSI escape sequence.
20/// If there is a invalid color anchor (eg. "\[$not_a_color]"), it will panic.
21///
22/// # Example:
23/// ```
24/// let f = vari::format("[$cyan]Hi![$/]");
25/// assert_eq!(f, "\x1b[36mHi!\x1b[0m");
26/// ```
27pub fn format(message: &str) -> String {
28 anchor::compile_anchor(anchor::split_anchor(message.to_string()))
29}
30
31/// Like vari::format() but condensed into a macro for convenience, and can also format strings.
32///
33/// Format a strings like Rust's format!() macro, and then run it through
34/// vari::format() function.
35///
36/// # Example:
37/// ```
38/// let f = vformat!("[$cyan]Hi![$/]");
39/// assert_eq!(f, "\x1b[36mHi!\x1b[0m");
40/// ```
41#[macro_export]
42macro_rules! vformat {
43 ($($arg:tt)*) => {{
44 let formatted = std::fmt::format(std::format_args!($($arg)*));
45 let result = vari::format(formatted.as_str());
46 result
47 }}
48}
49
50/// vformat!() macro but also print the result to stdout.
51///
52/// A macro for formatting and printting string with vari::format() function.
53/// Kind of like Rust's print!() macro but with colored strings.
54///
55/// # Example:
56/// ```
57/// vprintln!("[$cyan]Hi![$/]");
58/// ```
59#[macro_export]
60macro_rules! vprint {
61 ($($arg:tt)*) => {{
62 let formatted = std::fmt::format(std::format_args!($($arg)*));
63 let result = vari::format(formatted.as_str());
64 print!("{}", result);
65 }}
66}
67
68/// vprint!() but with a newlines. If no arguments are given, it will print a newline.
69///
70/// A macro for formatting and printing string with vari::format() function.
71/// It's just vari::vprint!() but with a newline at the end. For convenience.
72///
73/// # Example:
74/// ```
75/// vprintln!("[$cyan]Hi![$/]");
76/// ```
77#[macro_export]
78macro_rules! vprintln {
79 () => {{
80 println();
81 }};
82 ($($arg:tt)*) => {{
83 let formatted = std::fmt::format(std::format_args!($($arg)*));
84 let result = vari::format(formatted.as_str());
85 println!("{}", result);
86 }}
87}
88
89/// Expands to the file, line and column on where it was called.
90///
91/// Return the file, line and column on where it was called with dimmed style.
92/// Used for debugging (with vari::util::log()).
93///
94/// # Example:
95/// ```
96/// log!("Debugging!", here!());
97/// ```
98#[macro_export]
99macro_rules! here {
100 () => {{
101 &vformat!("[$dim]{}:{}:{}[$/]", file!(), line!(), column!())
102 }}
103}