From 4bc9cf84a3f5829a2d897f268ef3db58c00f6840 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 23 Mar 2023 11:03:17 +0530 Subject: [PATCH] Micro-optimization --- tools/cmd/diff/highlight.go | 5 +-- tools/utils/style/indent-and-wrap.go | 49 ++++++++++++++++------------ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/tools/cmd/diff/highlight.go b/tools/cmd/diff/highlight.go index 4bd97e143..882626875 100644 --- a/tools/cmd/diff/highlight.go +++ b/tools/cmd/diff/highlight.go @@ -6,11 +6,12 @@ import ( "errors" "fmt" "io" - "kitty/tools/utils" - "kitty/tools/utils/images" "path/filepath" "strings" + "kitty/tools/utils" + "kitty/tools/utils/images" + "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" "github.com/alecthomas/chroma/v2/styles" diff --git a/tools/utils/style/indent-and-wrap.go b/tools/utils/style/indent-and-wrap.go index ebeb7061e..b82a693d3 100644 --- a/tools/utils/style/indent-and-wrap.go +++ b/tools/utils/style/indent-and-wrap.go @@ -65,62 +65,71 @@ func (self *sgr_state) reset() { } func (self sgr_state) as_sgr(for_close bool) string { - ans := make([]string, 0, 4) + ans := make([]byte, 0, 32) if for_close { - if self.bold || self.dim { - ans = append(ans, "22") + if self.bold { + ans = append(ans, "221;"...) + } + if self.dim { + ans = append(ans, "222;"...) } if self.italic { - ans = append(ans, "23") + ans = append(ans, "23;"...) } if self.reverse { - ans = append(ans, "27") + ans = append(ans, "27;"...) } if self.strikethrough { - ans = append(ans, "29") + ans = append(ans, "29;"...) } 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 { - ans = append(ans, "39") + ans = append(ans, "39;"...) } if self.bg.number != 0 { - ans = append(ans, "49") + ans = append(ans, "49;"...) } if self.uc.number != 0 { - ans = append(ans, "59") + ans = append(ans, "59;"...) } } else { if self.bold { - ans = append(ans, "1") + ans = append(ans, "1;"...) } if self.dim { - ans = append(ans, "2") + ans = append(ans, "2;"...) } if self.italic { - ans = append(ans, "3") + ans = append(ans, "3;"...) } if self.reverse { - ans = append(ans, "7") + ans = append(ans, "7;"...) } if self.strikethrough { - ans = append(ans, "9") + ans = append(ans, "9;"...) } 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 != "" { - ans = append(ans, q) + ans = append(ans, q...) + ans = append(ans, ';') } 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 != "" { - 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 {