Skip to content

Commit

Permalink
perf: Improve performance of ansistyles.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Oct 6, 2021
1 parent 0d26fdb commit 40012e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
19 changes: 19 additions & 0 deletions pkg/ansistyles/ansistyle_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ansistyles

import (
"testing"
)

func BenchmarkAnsi(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Ansi(7)
}
}

func BenchmarkAnsi16m(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Ansi16m(10, 30, 255)
}
}
19 changes: 13 additions & 6 deletions pkg/ansistyles/ansistyles.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import (
"fmt"
"math"
"regexp"
"strconv"
)

// CSPair contains the ANSI color codes to open and close a given color.
Expand All @@ -122,38 +123,44 @@ func namedCSPair(open uint8, close uint8) CSPair {
//
// `color` should be a number between 30 and 37 or 90 and 97, inclusive.
func Ansi(color uint8) string {
return fmt.Sprintf("\u001B[%dm", color)
return "\u001B[" + strconv.FormatUint(uint64(color), 10) + "m"
}

// BgAnsi returns the string used to set the background color, based on a basic 16-color Ansi code.
//
// `color` should be a number between 30 and 37 or 90 and 97, inclusive.
func BgAnsi(color uint8) string {
return fmt.Sprintf("\u001B[%dm", color+10)
return "\u001B[" + strconv.FormatUint(uint64(color+10), 10) + "m"
}

// Ansi256 returns the string used to set the foreground color, based on Ansi 256 color lookup table.
//
// See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit.
func Ansi256(color uint8) string {
return fmt.Sprintf("\u001B[38;5;%dm", color)
return "\u001B[38;5;" + strconv.FormatUint(uint64(color), 10) + "m"
}

// BgAnsi256 returns the string used to set the background color, based on Ansi 256 color lookup table.
//
// See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit.
func BgAnsi256(color uint8) string {
return fmt.Sprintf("\u001B[48;5;%dm", color)
return "\u001B[48;5;" + strconv.FormatUint(uint64(color), 10) + "m"
}

// Ansi16m returns the string used to set a 24bit foreground color.
func Ansi16m(red uint8, green uint8, blue uint8) string {
return fmt.Sprintf("\u001B[38;2;%v;%v;%vm", red, green, blue)
return "\u001B[38;2;" +
strconv.FormatUint(uint64(red), 10) + ";" +
strconv.FormatUint(uint64(green), 10) + ";" +
strconv.FormatUint(uint64(blue), 10) + "m"
}

// BgAnsi16m returns the string used to set a 24bit background color.
func BgAnsi16m(red uint8, green uint8, blue uint8) string {
return fmt.Sprintf("\u001B[48;2;%v;%v;%vm", red, green, blue)
return "\u001B[48;2;" +
strconv.FormatUint(uint64(red), 10) + ";" +
strconv.FormatUint(uint64(green), 10) + ";" +
strconv.FormatUint(uint64(blue), 10) + "m"
}

// Close is the "close" code for 256 amd 16m ansi color codes.
Expand Down

0 comments on commit 40012e6

Please sign in to comment.