Micro-optimization

This commit is contained in:
Kovid Goyal 2023-03-23 11:03:17 +05:30
parent 14b58ba015
commit 4bc9cf84a3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 32 additions and 22 deletions

View File

@ -6,11 +6,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"kitty/tools/utils"
"kitty/tools/utils/images"
"path/filepath" "path/filepath"
"strings" "strings"
"kitty/tools/utils"
"kitty/tools/utils/images"
"github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers" "github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles" "github.com/alecthomas/chroma/v2/styles"

View File

@ -65,62 +65,71 @@ func (self *sgr_state) reset() {
} }
func (self sgr_state) as_sgr(for_close bool) string { func (self sgr_state) as_sgr(for_close bool) string {
ans := make([]string, 0, 4) ans := make([]byte, 0, 32)
if for_close { if for_close {
if self.bold || self.dim { if self.bold {
ans = append(ans, "22") ans = append(ans, "221;"...)
}
if self.dim {
ans = append(ans, "222;"...)
} }
if self.italic { if self.italic {
ans = append(ans, "23") ans = append(ans, "23;"...)
} }
if self.reverse { if self.reverse {
ans = append(ans, "27") ans = append(ans, "27;"...)
} }
if self.strikethrough { if self.strikethrough {
ans = append(ans, "29") ans = append(ans, "29;"...)
} }
if self.underline_style != no_underline && self.underline_style != nil_underline { if self.underline_style != no_underline && self.underline_style != nil_underline {
ans = append(ans, "4:0") ans = append(ans, "4:0;"...)
} }
if self.fg.number != 0 { if self.fg.number != 0 {
ans = append(ans, "39") ans = append(ans, "39;"...)
} }
if self.bg.number != 0 { if self.bg.number != 0 {
ans = append(ans, "49") ans = append(ans, "49;"...)
} }
if self.uc.number != 0 { if self.uc.number != 0 {
ans = append(ans, "59") ans = append(ans, "59;"...)
} }
} else { } else {
if self.bold { if self.bold {
ans = append(ans, "1") ans = append(ans, "1;"...)
} }
if self.dim { if self.dim {
ans = append(ans, "2") ans = append(ans, "2;"...)
} }
if self.italic { if self.italic {
ans = append(ans, "3") ans = append(ans, "3;"...)
} }
if self.reverse { if self.reverse {
ans = append(ans, "7") ans = append(ans, "7;"...)
} }
if self.strikethrough { if self.strikethrough {
ans = append(ans, "9") ans = append(ans, "9;"...)
} }
if self.underline_style != no_underline && self.underline_style != nil_underline { if self.underline_style != no_underline && self.underline_style != nil_underline {
ans = append(ans, fmt.Sprintf("4:%d", self.underline_style)) ans = append(ans, fmt.Sprintf("4:%d;", self.underline_style)...)
} }
if q := self.fg.as_sgr(30); q != "" { if q := self.fg.as_sgr(30); q != "" {
ans = append(ans, q) ans = append(ans, q...)
ans = append(ans, ';')
} }
if q := self.bg.as_sgr(40); q != "" { if q := self.bg.as_sgr(40); q != "" {
ans = append(ans, q) ans = append(ans, q...)
ans = append(ans, ';')
} }
if q := self.uc.as_sgr(50); q != "" { if q := self.uc.as_sgr(50); q != "" {
ans = append(ans, q) ans = append(ans, q...)
ans = append(ans, ';')
} }
} }
return strings.Join(ans, ";") if len(ans) > 0 {
ans = ans[:len(ans)-1]
}
return utils.UnsafeBytesToString(ans)
} }
func (self sgr_state) as_escape_codes(for_close bool) string { func (self sgr_state) as_escape_codes(for_close bool) string {