Add unit tests for escape sequence parse functions (#2116)

This commit is contained in:
Joe Lim authored and GitHub committed 2025-08-26 14:49:33 +10:00
1 parent 04afd78e65
commit 8f97bf806a
4 files changed
+67 -28

No files matched your search

-8
View File
@@ -88,14 +88,6 @@ func parseStyles() styleMap {
return sm return sm
} }
func parseEscapeSequence(s string) tcell.Style {
s = strings.TrimPrefix(s, "\033[")
if i := strings.IndexByte(s, 'm'); i >= 0 {
s = s[:i]
}
return applyAnsiCodes(s, tcell.StyleDefault)
}
func parseColor(toks []string) (tcell.Color, int, error) { func parseColor(toks []string) (tcell.Color, int, error) {
if len(toks) == 0 { if len(toks) == 0 {
return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks)
+33 -12
View File
@@ -13,9 +13,22 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
) )
var (
reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
reRulerSub = regexp.MustCompile(`%[apmcsvfithPd]|%\{[^}]+\}`)
reSixelSize = regexp.MustCompile(`"1;1;(\d+);(\d+)`)
)
var (
reWord = regexp.MustCompile(`(\pL|\pN)+`)
reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`)
reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`)
)
func isRoot(name string) bool { return filepath.Dir(name) == name } func isRoot(name string) bool { return filepath.Dir(name) == name }
func replaceTilde(s string) string { func replaceTilde(s string) string {
@@ -324,18 +337,6 @@ func getFileExtension(file fs.FileInfo) string {
return filepath.Ext(file.Name()) return filepath.Ext(file.Name())
} }
var (
reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
reRulerSub = regexp.MustCompile(`%[apmcsvfithPd]|%\{[^}]+\}`)
reSixelSize = regexp.MustCompile(`"1;1;(\d+);(\d+)`)
)
var (
reWord = regexp.MustCompile(`(\pL|\pN)+`)
reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`)
reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`)
)
// This function deletes entries from a map if the key is either the given path // This function deletes entries from a map if the key is either the given path
// or a subpath of it. // or a subpath of it.
// This is useful for clearing cached data when a directory is moved or deleted. // This is useful for clearing cached data when a directory is moved or deleted.
@@ -349,6 +350,26 @@ func deletePathRecursive[T any](m map[string]T, path string) {
} }
} }
// This function takes an escape sequence option (e.g. `\033[1m`) and outputs a
// complete format string (e.g. `\033[1m%s\033[0m`).
func optionToFmtstr(optstr string) string {
if !strings.Contains(optstr, "%s") {
return optstr + "%s\033[0m"
} else {
return optstr
}
}
// This function takes an escape sequence option (e.g. `\033[1m`) and converts
// it to a `tcell.Style` object.
func parseEscapeSequence(s string) tcell.Style {
s = strings.TrimPrefix(s, "\033[")
if i := strings.IndexByte(s, 'm'); i >= 0 {
s = s[:i]
}
return applyAnsiCodes(s, tcell.StyleDefault)
}
// This function is used to remove style-related ANSI escape sequences from // This function is used to remove style-related ANSI escape sequences from
// a given string. // a given string.
// //
+34
View File
@@ -7,6 +7,8 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/gdamore/tcell/v2"
) )
func TestIsRoot(t *testing.T) { func TestIsRoot(t *testing.T) {
@@ -361,6 +363,38 @@ func TestGetFileExtension(t *testing.T) {
} }
} }
func TestOptionToFmtstr(t *testing.T) {
tests := []struct {
s string
exp string
}{
{"\033[1m", "\033[1m%s\033[0m"},
{"\033[1;7;31;42m", "\033[1;7;31;42m%s\033[0m"},
}
for _, test := range tests {
if got := optionToFmtstr(test.s); got != test.exp {
t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got)
}
}
}
func TestParseEscapeSequence(t *testing.T) {
tests := []struct {
s string
exp tcell.Style
}{
{"\033[1m", tcell.StyleDefault.Bold(true)},
{"\033[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(tcell.ColorMaroon).Background(tcell.ColorGreen)},
}
for _, test := range tests {
if got := parseEscapeSequence(test.s); got != test.exp {
t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got)
}
}
}
func TestStripAnsi(t *testing.T) { func TestStripAnsi(t *testing.T) {
tests := []struct { tests := []struct {
s string s string
-8
View File
@@ -744,14 +744,6 @@ func (ui *ui) echomsg(msg string) {
log.Print(msg) log.Print(msg)
} }
func optionToFmtstr(optstr string) string {
if !strings.Contains(optstr, "%s") {
return optstr + "%s\033[0m"
} else {
return optstr
}
}
func (ui *ui) echoerr(msg string) { func (ui *ui) echoerr(msg string) {
ui.echo(fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), msg)) ui.echo(fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), msg))
log.Printf("error: %s", msg) log.Printf("error: %s", msg)