Add support for underline styles (#1896)

This commit is contained in:
Joe Lim authored and GitHub committed 2025-02-09 23:39:15 +11:00
1 parent 3b56a9fa64
commit b3c652c4d4
2 files changed
+91 -50

No files matched your search

+83 -50
View File
@@ -98,83 +98,116 @@ func parseEscapeSequence(s string) tcell.Style {
func applyAnsiCodes(s string, st tcell.Style) tcell.Style { func applyAnsiCodes(s string, st tcell.Style) tcell.Style {
toks := strings.Split(s, ";") toks := strings.Split(s, ";")
nums := make([]int, 0, len(toks))
for _, tok := range toks {
if tok == "" {
nums = append(nums, 0)
continue
}
n, err := strconv.Atoi(tok)
if err != nil {
log.Printf("converting escape code: %s", err)
continue
}
nums = append(nums, n)
}
// ECMA-48 details the standard // ECMA-48 details the standard
// TODO: should we support turning off attributes? // TODO: should we support turning off attributes?
// Probably because this is used for previewers too // Probably because this is used for previewers too
numslen := len(nums) tokslen := len(toks)
for i := 0; i < numslen; i++ { for i := 0; i < tokslen; i++ {
n := nums[i] switch toks[i] {
switch { case "", "0":
case n == 0:
st = tcell.StyleDefault st = tcell.StyleDefault
case n == 1: case "1":
st = st.Bold(true) st = st.Bold(true)
case n == 2: case "2":
st = st.Dim(true) st = st.Dim(true)
case n == 3: case "3":
st = st.Italic(true) st = st.Italic(true)
case n == 4: case "4:0":
st = st.Underline(false)
case "4", "4:1":
st = st.Underline(true) st = st.Underline(true)
case n == 5 || n == 6: case "4:2":
st = st.Underline(tcell.UnderlineStyleDouble)
case "4:3":
st = st.Underline(tcell.UnderlineStyleCurly)
case "4:4":
st = st.Underline(tcell.UnderlineStyleDotted)
case "4:5":
st = st.Underline(tcell.UnderlineStyleDashed)
case "5", "6":
st = st.Blink(true) st = st.Blink(true)
case n == 7: case "7":
st = st.Reverse(true) st = st.Reverse(true)
case n == 8: case "8":
// TODO: tcell PR for proper conceal // TODO: tcell PR for proper conceal
_, bg, _ := st.Decompose() _, bg, _ := st.Decompose()
st = st.Foreground(bg) st = st.Foreground(bg)
case n == 9: case "9":
st = st.StrikeThrough(true) st = st.StrikeThrough(true)
case n >= 30 && n <= 37: case "30", "31", "32", "33", "34", "35", "36", "37":
n, _ := strconv.Atoi(toks[i])
st = st.Foreground(tcell.PaletteColor(n - 30)) st = st.Foreground(tcell.PaletteColor(n - 30))
case n >= 90 && n <= 97: case "90", "91", "92", "93", "94", "95", "96", "97":
n, _ := strconv.Atoi(toks[i])
st = st.Foreground(tcell.PaletteColor(n - 82)) st = st.Foreground(tcell.PaletteColor(n - 82))
case n == 38: case "38":
if i+3 <= numslen && nums[i+1] == 5 { if toks[i+1] == "5" && i+2 < tokslen {
st = st.Foreground(tcell.PaletteColor(nums[i+2])) n, err := strconv.Atoi(toks[i+2])
if err != nil {
log.Printf("unknown ansi code: %s", toks[i+2])
continue
}
st = st.Foreground(tcell.PaletteColor(n))
i += 2 i += 2
} else if i+5 <= numslen && nums[i+1] == 2 { } else if toks[i+1] == "2" && i+4 < tokslen {
st = st.Foreground(tcell.NewRGBColor( r, err := strconv.Atoi(toks[i+2])
int32(nums[i+2]), if err != nil {
int32(nums[i+3]), log.Printf("unknown ansi code: %s", toks[i+2])
int32(nums[i+4]))) continue
}
g, err := strconv.Atoi(toks[i+3])
if err != nil {
log.Printf("unknown ansi code: %s", toks[i+3])
continue
}
b, err := strconv.Atoi(toks[i+4])
if err != nil {
log.Printf("unknown ansi code: %s", toks[i+4])
continue
}
st = st.Foreground(tcell.NewRGBColor(int32(r), int32(g), int32(b)))
i += 4 i += 4
} else { } else {
log.Printf("unknown ansi code or incorrect form: %d", n) log.Printf("unknown ansi code or incorrect form: %s", toks[i])
} }
case n >= 40 && n <= 47: case "40", "41", "42", "43", "44", "45", "46", "47":
n, _ := strconv.Atoi(toks[i])
st = st.Background(tcell.PaletteColor(n - 40)) st = st.Background(tcell.PaletteColor(n - 40))
case n >= 100 && n <= 107: case "100", "101", "102", "103", "104", "105", "106", "107":
n, _ := strconv.Atoi(toks[i])
st = st.Background(tcell.PaletteColor(n - 92)) st = st.Background(tcell.PaletteColor(n - 92))
case n == 48: case "48":
if i+3 <= numslen && nums[i+1] == 5 { if toks[i+1] == "5" && i+2 < tokslen {
st = st.Background(tcell.PaletteColor(nums[i+2])) n, err := strconv.Atoi(toks[i+2])
if err != nil {
log.Printf("unknown ansi code: %s", toks[i+2])
continue
}
st = st.Background(tcell.PaletteColor(n))
i += 2 i += 2
} else if i+5 <= numslen && nums[i+1] == 2 { } else if toks[i+1] == "2" && i+4 < tokslen {
st = st.Background(tcell.NewRGBColor( r, err := strconv.Atoi(toks[i+2])
int32(nums[i+2]), if err != nil {
int32(nums[i+3]), log.Printf("unknown ansi code: %s", toks[i+2])
int32(nums[i+4]))) continue
}
g, err := strconv.Atoi(toks[i+3])
if err != nil {
log.Printf("unknown ansi code: %s", toks[i+3])
continue
}
b, err := strconv.Atoi(toks[i+4])
if err != nil {
log.Printf("unknown ansi code: %s", toks[i+4])
continue
}
st = st.Background(tcell.NewRGBColor(int32(r), int32(g), int32(b)))
i += 4 i += 4
} else { } else {
log.Printf("unknown ansi code or incorrect form: %d", n) log.Printf("unknown ansi code or incorrect form: %s", toks[i])
} }
default: default:
log.Printf("unknown ansi code: %d", n) log.Printf("unknown ansi code: %s", toks[i])
} }
} }
+8
View File
@@ -35,6 +35,14 @@ func TestApplyAnsiCodes(t *testing.T) {
{"4", none.Bold(true), none.Bold(true).Underline(true)}, {"4", none.Bold(true), none.Bold(true).Underline(true)},
{"4", none.Foreground(tcell.ColorMaroon).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true).Underline(true)}, {"4", none.Foreground(tcell.ColorMaroon).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true).Underline(true)},
{"4:0", none, none},
{"4:0", none.Underline(true), none},
{"4:1", none, none.Underline(true)},
{"4:2", none, none.Underline(tcell.UnderlineStyleDouble)},
{"4:3", none, none.Underline(tcell.UnderlineStyleCurly)},
{"4:4", none, none.Underline(tcell.UnderlineStyleDotted)},
{"4:5", none, none.Underline(tcell.UnderlineStyleDashed)},
{"31", none, none.Foreground(tcell.ColorMaroon)}, {"31", none, none.Foreground(tcell.ColorMaroon)},
{"31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)}, {"31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
{"31", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)}, {"31", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},