Skip to main content

woad/
lib.rs

1//! Minimal ANSI terminal colour codes, for Rust.
2//!
3//! **woad** provides fixed SGR sequences, and stream-conditional sequences
4//! that emit only when the destination warrants colour. The decision is the
5//! portable half of the **woad** 0.3.0 colour contract: cached environment
6//! inspection, `TERM=dumb`, and [`std::io::IsTerminal`]. It is not a
7//! console or TUI framework.
8//!
9//! Windows console-mode changes, virtual-terminal gating, and handle
10//! currencies are not part of this alpha. [`console_supports_ansi`] is
11//! always `true`, which is the non-Windows answer. On Windows a terminal is
12//! treated with the Unix rule.
13//!
14//! ```
15//! use woad::{reset_for, seq_for, FG_GREEN};
16//!
17//! let stdout = std::io::stdout();
18//!
19//! println!("{}ok{}", seq_for(FG_GREEN, &stdout), reset_for(&stdout),);
20//! ```
21
22#[path = "version.rs"]
23mod crate_version;
24pub use crate_version::{
25    version,
26    version_string,
27    VERSION,
28    VERSION_MAJOR,
29    VERSION_MINOR,
30    VERSION_PATCH,
31    VERSION_RELSTATUS,
32};
33
34mod policy;
35pub use policy::{
36    blinking_for,
37    bold_for,
38    colour_policy,
39    console_supports_ansi,
40    is_tty,
41    refresh,
42    reset_for,
43    seq_for,
44    seq_for_absent,
45    supports_colour,
46    supports_colour_absent,
47    ColourPolicy,
48};
49#[cfg(unix)]
50pub use policy::{
51    fd_is_tty,
52    fd_supports_colour,
53    seq_for_fd,
54};
55
56mod sequences;
57pub use sequences::{
58    BG_BLACK,
59    BG_BLUE,
60    BG_BRIGHT_BLACK,
61    BG_BRIGHT_BLUE,
62    BG_BRIGHT_CYAN,
63    BG_BRIGHT_GREEN,
64    BG_BRIGHT_MAGENTA,
65    BG_BRIGHT_RED,
66    BG_BRIGHT_WHITE,
67    BG_BRIGHT_YELLOW,
68    BG_CYAN,
69    BG_GREEN,
70    BG_MAGENTA,
71    BG_RED,
72    BG_WHITE,
73    BG_YELLOW,
74    BLINKING,
75    BOLD,
76    FG_BLACK,
77    FG_BLUE,
78    FG_BRIGHT_BLACK,
79    FG_BRIGHT_BLUE,
80    FG_BRIGHT_CYAN,
81    FG_BRIGHT_GREEN,
82    FG_BRIGHT_MAGENTA,
83    FG_BRIGHT_RED,
84    FG_BRIGHT_WHITE,
85    FG_BRIGHT_YELLOW,
86    FG_CYAN,
87    FG_GREEN,
88    FG_MAGENTA,
89    FG_RED,
90    FG_WHITE,
91    FG_YELLOW,
92    RESET,
93};
94
95
96#[cfg(test)]
97mod tests {
98    #![allow(non_snake_case)]
99
100
101    #[test]
102    fn TEST_RESET() {
103        assert_eq!("\x1b[0m", crate::RESET);
104    }
105
106    #[test]
107    fn TEST_BOLD() {
108        assert_eq!("\x1b[1m", crate::BOLD);
109    }
110
111    #[test]
112    fn TEST_BLINKING() {
113        assert_eq!("\x1b[5m", crate::BLINKING);
114    }
115
116    #[test]
117    fn TEST_FG_RED() {
118        assert_eq!("\x1b[31m", crate::FG_RED);
119    }
120
121    #[test]
122    fn TEST_BG_BLUE() {
123        assert_eq!("\x1b[44m", crate::BG_BLUE);
124    }
125
126    #[test]
127    fn TEST_CODES_ARE_CSI_SGR() {
128        let codes = [
129            crate::BLINKING,
130            crate::BOLD,
131            crate::RESET,
132            crate::FG_BLACK,
133            crate::FG_BRIGHT_WHITE,
134            crate::BG_BLACK,
135            crate::BG_BRIGHT_WHITE,
136        ];
137
138        for code in codes {
139            assert!(code.starts_with("\x1b["));
140            assert!(code.ends_with('m'));
141        }
142    }
143}