Skip to content

Commit bbc0872

Browse files
authored
Terminal escape sequence constants (#2461)
1 parent 1ae6a6d commit bbc0872

File tree

3 files changed

+102
-11
lines changed

3 files changed

+102
-11
lines changed

README.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,30 @@ A number of constants are predefined:
18771877
| `HEX`<sup>1.27.0</sup> | `"0123456789abcdef"` |
18781878
| `HEXLOWER`<sup>1.27.0</sup> | `"0123456789abcdef"` |
18791879
| `HEXUPPER`<sup>1.27.0</sup> | `"0123456789ABCDEF"` |
1880+
| `CLEAR`<sup>master</sup> | `"\ec"` |
1881+
| `NORMAL`<sup>master</sup> | `"\e[0m"` |
1882+
| `BOLD`<sup>master</sup> | `"\e[1m"` |
1883+
| `ITALIC`<sup>master</sup> | `"\e[3m"` |
1884+
| `UNDERLINE`<sup>master</sup> | `"\e[4m"` |
1885+
| `INVERT`<sup>master</sup> | `"\e[7m"` |
1886+
| `HIDE`<sup>master</sup> | `"\e[8m"` |
1887+
| `STRIKETHROUGH`<sup>master</sup> | `"\e[9m"` |
1888+
| `BLACK`<sup>master</sup> | `"\e[30m"` |
1889+
| `RED`<sup>master</sup> | `"\e[31m"` |
1890+
| `GREEN`<sup>master</sup> | `"\e[32m"` |
1891+
| `YELLOW`<sup>master</sup> | `"\e[33m"` |
1892+
| `BLUE`<sup>master</sup> | `"\e[34m"` |
1893+
| `MAGENTA`<sup>master</sup> | `"\e[35m"` |
1894+
| `CYAN`<sup>master</sup> | `"\e[36m"` |
1895+
| `WHITE`<sup>master</sup> | `"\e[37m"` |
1896+
| `BG_BLACK`<sup>master</sup> | `"\e[40m"` |
1897+
| `BG_RED`<sup>master</sup> | `"\e[41m"` |
1898+
| `BG_GREEN`<sup>master</sup> | `"\e[42m"` |
1899+
| `BG_YELLOW`<sup>master</sup> | `"\e[43m"` |
1900+
| `BG_BLUE`<sup>master</sup> | `"\e[44m"` |
1901+
| `BG_MAGENTA`<sup>master</sup> | `"\e[45m"` |
1902+
| `BG_CYAN`<sup>master</sup> | `"\e[46m"` |
1903+
| `BG_WHITE`<sup>master</sup> | `"\e[47m"` |
18801904

18811905
```just
18821906
@foo:
@@ -1888,9 +1912,29 @@ $ just foo
18881912
0123456789abcdef
18891913
```
18901914

1915+
Constants starting with `\e` are
1916+
[ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code).
1917+
1918+
`CLEAR` clears the screen, similar to the `clear` command. The rest are of the
1919+
form `\e[Nm`, where `N` is an integer, and set terminal display attributes.
1920+
1921+
Terminal display attribute escape sequences can be combined, for example text
1922+
weight `BOLD`, text style `STRIKETHROUGH`, foreground color `CYAN`, and
1923+
background color `BG_BLUE`. They should be followed by `NORMAL`, to reset the
1924+
terminal back to normal.
1925+
1926+
Escape sequences should be quoted, since `[` is treated as a special character
1927+
by some shells.
1928+
1929+
```just
1930+
@foo:
1931+
echo '{{BOLD + STRIKETHROUGH + CYAN + BG_BLUE}}Hi!{{NORMAL}}'
1932+
```
1933+
18911934
### Attributes
18921935

1893-
Recipes, `mod` statements, and aliases may be annotated with attributes that change their behavior.
1936+
Recipes, `mod` statements, and aliases may be annotated with attributes that
1937+
change their behavior.
18941938

18951939
| Name | Type | Description |
18961940
|------|------|-------------|

justfile

+4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ build-book:
169169
mdbook build book/en
170170
mdbook build book/zh
171171

172+
[group: 'dev']
173+
print-readme-constants-table:
174+
cargo test constants::tests::readme_table -- --nocapture
175+
172176
# run all polyglot recipes
173177
[group: 'demo']
174178
polyglot: _python _js _perl _sh _ruby

src/constants.rs

+53-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
use super::*;
22

3+
const CONSTANTS: [(&str, &str, &str); 27] = [
4+
("HEX", "0123456789abcdef", "1.27.0"),
5+
("HEXLOWER", "0123456789abcdef", "1.27.0"),
6+
("HEXUPPER", "0123456789ABCDEF", "1.27.0"),
7+
("CLEAR", "\x1bc", "master"),
8+
("NORMAL", "\x1b[0m", "master"),
9+
("BOLD", "\x1b[1m", "master"),
10+
("ITALIC", "\x1b[3m", "master"),
11+
("UNDERLINE", "\x1b[4m", "master"),
12+
("INVERT", "\x1b[7m", "master"),
13+
("HIDE", "\x1b[8m", "master"),
14+
("STRIKETHROUGH", "\x1b[9m", "master"),
15+
("BLACK", "\x1b[30m", "master"),
16+
("RED", "\x1b[31m", "master"),
17+
("GREEN", "\x1b[32m", "master"),
18+
("YELLOW", "\x1b[33m", "master"),
19+
("BLUE", "\x1b[34m", "master"),
20+
("MAGENTA", "\x1b[35m", "master"),
21+
("CYAN", "\x1b[36m", "master"),
22+
("WHITE", "\x1b[37m", "master"),
23+
("BG_BLACK", "\x1b[40m", "master"),
24+
("BG_RED", "\x1b[41m", "master"),
25+
("BG_GREEN", "\x1b[42m", "master"),
26+
("BG_YELLOW", "\x1b[43m", "master"),
27+
("BG_BLUE", "\x1b[44m", "master"),
28+
("BG_MAGENTA", "\x1b[45m", "master"),
29+
("BG_CYAN", "\x1b[46m", "master"),
30+
("BG_WHITE", "\x1b[47m", "master"),
31+
];
32+
333
pub(crate) fn constants() -> &'static HashMap<&'static str, &'static str> {
4-
static CONSTANTS: OnceLock<HashMap<&str, &str>> = OnceLock::new();
5-
6-
CONSTANTS.get_or_init(|| {
7-
vec![
8-
("HEX", "0123456789abcdef"),
9-
("HEXLOWER", "0123456789abcdef"),
10-
("HEXUPPER", "0123456789ABCDEF"),
11-
]
12-
.into_iter()
13-
.collect()
34+
static MAP: OnceLock<HashMap<&str, &str>> = OnceLock::new();
35+
MAP.get_or_init(|| {
36+
CONSTANTS
37+
.into_iter()
38+
.map(|(name, value, _version)| (name, value))
39+
.collect()
1440
})
1541
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
47+
#[test]
48+
fn readme_table() {
49+
println!("| Name | Value |");
50+
println!("|------|-------------|");
51+
for (name, value, version) in CONSTANTS {
52+
println!(
53+
"| `{name}`<sup>{version}</sup> | `\"{}\"` |",
54+
value.replace('\x1b', "\\e")
55+
);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)