src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/ui/parse_sgr_test.go (about) 1 package ui 2 3 import ( 4 "testing" 5 6 "src.elv.sh/pkg/tt" 7 ) 8 9 func TestParseSGREscapedText(t *testing.T) { 10 tt.Test(t, ParseSGREscapedText, 11 Args("").Rets(Text(nil)), 12 Args("text").Rets(T("text")), 13 Args("\033[1mbold").Rets(T("bold", Bold)), 14 Args("\033[1mbold\033[31mbold red").Rets( 15 Concat(T("bold", Bold), T("bold red", Bold, FgRed))), 16 Args("\033[1mbold\033[;31mred").Rets( 17 Concat(T("bold", Bold), T("red", FgRed))), 18 // Non-SGR CSI sequences are removed. 19 Args("\033[Atext").Rets(T("text")), 20 // Control characters not part of CSI escape sequences are left 21 // untouched. 22 Args("t\x01ext").Rets(T("t\x01ext")), 23 ) 24 } 25 26 func TestStyleFromSGR(t *testing.T) { 27 tt.Test(t, StyleFromSGR, 28 Args("1").Rets(Style{Bold: true}), 29 // Invalid codes are ignored 30 Args("1;invalid;10000").Rets(Style{Bold: true}), 31 // ANSI colors. 32 Args("31;42").Rets(Style{Fg: Red, Bg: Green}), 33 // ANSI bright colors. 34 Args("91;102"). 35 Rets(Style{Fg: BrightRed, Bg: BrightGreen}), 36 // XTerm 256 color. 37 Args("38;5;1;48;5;2"). 38 Rets(Style{Fg: XTerm256Color(1), Bg: XTerm256Color(2)}), 39 // True colors. 40 Args("38;2;1;2;3;48;2;10;20;30"). 41 Rets(Style{ 42 Fg: TrueColor(1, 2, 3), Bg: TrueColor(10, 20, 30)}), 43 ) 44 }