refactor: move optionToFmtstr() to termseq.go (#2323)

This commit is contained in:
CatsDeservePets authored and GitHub committed 2025-12-27 17:37:30 +01:00
1 parent eb01cb7c7b
commit 57d02dd01d
4 files changed
+26 -26

No files matched your search

-10
View File
@@ -446,16 +446,6 @@ 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 reads lines from a file to be displayed as a preview. // This function reads lines from a file to be displayed as a preview.
// The number of lines to read is capped since files can be very large. // The number of lines to read is capped since files can be very large.
// Lines are split on `\n` characters, and `\r` characters are discarded. // Lines are split on `\n` characters, and `\r` characters are discarded.
-16
View File
@@ -508,22 +508,6 @@ func TestTruncateFilename(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 TestReadLines(t *testing.T) { func TestReadLines(t *testing.T) {
tests := []struct { tests := []struct {
s string s string
+10
View File
@@ -85,6 +85,16 @@ func readTermSequence(s string) 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 // This function takes an escape sequence option (e.g. `\033[1m`) and converts
// it to a `tcell.Style` object. // it to a `tcell.Style` object.
// Legacy function that only accepts SGR. Kept for convenience. // Legacy function that only accepts SGR. Kept for convenience.
+16
View File
@@ -83,6 +83,22 @@ func TestReadTermSequence(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) { func TestParseEscapeSequence(t *testing.T) {
tests := []struct { tests := []struct {
s string s string