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