From 8f97bf806a183d07158a02b641335b8738f3fc7d Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Tue, 26 Aug 2025 14:49:33 +1000 Subject: [PATCH] Add unit tests for escape sequence parse functions (#2116) --- colors.go | 8 -------- misc.go | 45 +++++++++++++++++++++++++++++++++------------ misc_test.go | 34 ++++++++++++++++++++++++++++++++++ ui.go | 8 -------- 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/colors.go b/colors.go index d4fc46bb..5fa22ab1 100644 --- a/colors.go +++ b/colors.go @@ -88,14 +88,6 @@ func parseStyles() styleMap { 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) { if len(toks) == 0 { return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks) diff --git a/misc.go b/misc.go index ef8adf98..521e5e02 100644 --- a/misc.go +++ b/misc.go @@ -13,9 +13,22 @@ import ( "unicode" "unicode/utf8" + "github.com/gdamore/tcell/v2" "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 replaceTilde(s string) string { @@ -324,18 +337,6 @@ func getFileExtension(file fs.FileInfo) string { 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 // or a subpath of it. // 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 // a given string. // diff --git a/misc_test.go b/misc_test.go index 776265c8..fa70d8d2 100644 --- a/misc_test.go +++ b/misc_test.go @@ -7,6 +7,8 @@ import ( "strings" "testing" "time" + + "github.com/gdamore/tcell/v2" ) 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) { tests := []struct { s string diff --git a/ui.go b/ui.go index 178ce295..9cdf0f09 100644 --- a/ui.go +++ b/ui.go @@ -744,14 +744,6 @@ func (ui *ui) echomsg(msg string) { 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) { ui.echo(fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), msg)) log.Printf("error: %s", msg)