package ui
import (
)
type sgrTokenizer struct {
text string
styling Styling
content string
}
const sgrPrefix = "\033["
func ( *sgrTokenizer) () bool {
for strings.HasPrefix(.text, sgrPrefix) {
:= strings.TrimPrefix(.text, sgrPrefix)
:= strings.IndexFunc(, func( rune) bool {
return != ';' && ( < '0' || > '9')
})
if == -1 {
.text = ""
return false
}
:= []
:= [:]
.text = [+1:]
if == 'm' {
.styling = StylingFromSGR()
.content = ""
return true
}
}
if .text == "" {
return false
}
:= ""
:= strings.Index(.text, sgrPrefix)
if == -1 {
= .text
} else {
= .text[:]
}
.text = .text[len():]
.styling = nil
.content =
return true
}
func ( *sgrTokenizer) () (Styling, string) {
return .styling, .content
}
func ( string) Text {
var Text
var Style
:= sgrTokenizer{text: }
for .Next() {
, := .Token()
if != nil {
.transform(&)
}
if != "" {
= append(, &Segment{, })
}
}
return
}
var sgrStyling = map[int]Styling{
0: Reset,
1: Bold,
2: Dim,
4: Underlined,
5: Blink,
7: Inverse,
}
func ( string) Style {
var Style
StylingFromSGR().transform(&)
return
}
func ( string) Styling {
:= jointStyling{}
:= getSGRCodes()
if len() == 0 {
return Reset
}
for len() > 0 {
:= [0]
:= 1
var Styling
switch {
case sgrStyling[] != nil:
= sgrStyling[]
case 30 <= && <= 37:
= Fg(ansiColor( - 30))
case 40 <= && <= 47:
= Bg(ansiColor( - 40))
case 90 <= && <= 97:
= Fg(ansiBrightColor( - 90))
case 100 <= && <= 107:
= Bg(ansiBrightColor( - 100))
case == 38 && len() >= 3 && [1] == 5:
= Fg(xterm256Color([2]))
= 3
case == 48 && len() >= 3 && [1] == 5:
= Bg(xterm256Color([2]))
= 3
case == 38 && len() >= 5 && [1] == 2:
= Fg(trueColor{
uint8([2]), uint8([3]), uint8([4])})
= 5
case == 48 && len() >= 5 && [1] == 2:
= Bg(trueColor{
uint8([2]), uint8([3]), uint8([4])})
= 5
default:
}
= [:]
if != nil {
= append(, )
}
}
return
}
func ( string) []int {
var []int
for , := range strings.Split(, ";") {
if == "" {
= append(, 0)
} else {
, := strconv.Atoi()
if == nil {
= append(, )
}
}
}
return
}