@@ -2,9 +2,10 @@ package ansi
22
33import (
44 "fmt"
5- "github.com/rivo/uniseg"
65 "strconv"
76 "strings"
7+
8+ "github.com/rivo/uniseg"
89)
910
1011// TextStyle is a type representing the
@@ -321,6 +322,7 @@ func Parse(input string, options ...ParseOption) ([]*StyledText, error) {
321322 skip --
322323 continue
323324 }
325+ param = stripLeadingZeros (param )
324326 switch param {
325327 case "0" , "" :
326328 colourMap = ColourMap ["Regular" ]
@@ -369,9 +371,10 @@ func Parse(input string, options ...ParseOption) ([]*StyledText, error) {
369371 return nil , invalid
370372 }
371373 // 256 colours
372- if params [index + 1 ] == "5" {
374+ param1 := stripLeadingZeros (params [index + 1 ])
375+ if param1 == "5" {
373376 skip = 2
374- colIndexText := params [index + 2 ]
377+ colIndexText := stripLeadingZeros ( params [index + 2 ])
375378 colIndex , err := strconv .Atoi (colIndexText )
376379 if err != nil {
377380 return nil , invalid256ColSequence
@@ -391,7 +394,7 @@ func Parse(input string, options ...ParseOption) ([]*StyledText, error) {
391394 if len (params )- index < 5 {
392395 return nil , invalidTrueColorSequence
393396 }
394- if params [ index + 1 ] != "2" {
397+ if param1 != "2" {
395398 return nil , invalidTrueColorSequence
396399 }
397400 var r , g , b uint8
@@ -465,6 +468,13 @@ func Parse(input string, options ...ParseOption) ([]*StyledText, error) {
465468 }
466469}
467470
471+ func stripLeadingZeros (s string ) string {
472+ if len (s ) < 2 {
473+ return s
474+ }
475+ return strings .TrimLeft (s , "0" )
476+ }
477+
468478// HasEscapeCodes tests that input has escape codes.
469479func HasEscapeCodes (input string ) bool {
470480 return strings .IndexAny (input , "\033 [" ) != - 1
0 commit comments