Skip to content

Commit f28db2d

Browse files
authored
Merge pull request #269 from Muscraft/no-leading-zero
fix: Don't add leading zero to single digit ansi 256
2 parents 20258de + ba45662 commit f28db2d

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/anstyle/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ std = []
3232

3333
[dev-dependencies]
3434
lexopt = "0.3.0"
35+
snapbox = "0.6.5"
3536

3637
[lints]
3738
workspace = true

crates/anstyle/src/color.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl DisplayBuffer {
591591
let c2: u8 = (code / 10) % 10;
592592
let c3: u8 = code % 10;
593593

594-
let mut printed = true;
594+
let mut printed = false;
595595
if c1 != 0 {
596596
printed = true;
597597
self.buffer[self.len] = b'0' + c1;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Style { fg: Some(Ansi256(Ansi256Color(0))), bg: None, underline: None, effects: Effects() }
2+
Style { fg: Some(Ansi256(Ansi256Color(1))), bg: None, underline: None, effects: Effects() }
3+
Style { fg: Some(Ansi256(Ansi256Color(2))), bg: None, underline: None, effects: Effects() }
4+
Style { fg: Some(Ansi256(Ansi256Color(3))), bg: None, underline: None, effects: Effects() }
5+
Style { fg: Some(Ansi256(Ansi256Color(4))), bg: None, underline: None, effects: Effects() }
6+
Style { fg: Some(Ansi256(Ansi256Color(5))), bg: None, underline: None, effects: Effects() }
7+
Style { fg: Some(Ansi256(Ansi256Color(6))), bg: None, underline: None, effects: Effects() }
8+
Style { fg: Some(Ansi256(Ansi256Color(7))), bg: None, underline: None, effects: Effects() }
9+
Style { fg: Some(Ansi256(Ansi256Color(8))), bg: None, underline: None, effects: Effects() }
10+
Style { fg: Some(Ansi256(Ansi256Color(9))), bg: None, underline: None, effects: Effects() }
11+
Style { fg: Some(Ansi256(Ansi256Color(10))), bg: None, underline: None, effects: Effects() }
12+
Style { fg: Some(Ansi256(Ansi256Color(11))), bg: None, underline: None, effects: Effects() }
13+
Style { fg: Some(Ansi256(Ansi256Color(12))), bg: None, underline: None, effects: Effects() }
14+
Style { fg: Some(Ansi256(Ansi256Color(13))), bg: None, underline: None, effects: Effects() }
15+
Style { fg: Some(Ansi256(Ansi256Color(14))), bg: None, underline: None, effects: Effects() }
16+
Style { fg: Some(Ansi256(Ansi256Color(15))), bg: None, underline: None, effects: Effects() }

crates/anstyle/tests/testsuite.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::fmt::{Result, Write};
2+
3+
use anstyle::{Ansi256Color, AnsiColor};
4+
5+
#[test]
6+
fn no_leading_zero() -> Result {
7+
let mut actual = String::new();
8+
let ansi_colors = vec![
9+
AnsiColor::Black,
10+
AnsiColor::Red,
11+
AnsiColor::Green,
12+
AnsiColor::Yellow,
13+
AnsiColor::Blue,
14+
AnsiColor::Magenta,
15+
AnsiColor::Cyan,
16+
AnsiColor::White,
17+
AnsiColor::BrightBlack,
18+
AnsiColor::BrightRed,
19+
AnsiColor::BrightGreen,
20+
AnsiColor::BrightYellow,
21+
AnsiColor::BrightBlue,
22+
AnsiColor::BrightMagenta,
23+
AnsiColor::BrightCyan,
24+
AnsiColor::BrightWhite,
25+
];
26+
27+
for c in ansi_colors {
28+
let c = Ansi256Color::from_ansi(c).on_default();
29+
writeln!(actual, "{c}{c:?}{c:#}")?;
30+
}
31+
32+
snapbox::assert_data_eq!(actual, snapbox::file!["no_leading_zero.vte": Text].raw());
33+
34+
Ok(())
35+
}

0 commit comments

Comments
 (0)