From 57d02dd01d4a9acf3c2d4b2f7d6199d44c4ea998 Mon Sep 17 00:00:00 2001 From: CatsDeservePets <145048791+CatsDeservePets@users.noreply.github.com> Date: Sat, 27 Dec 2025 17:37:30 +0100 Subject: [PATCH] refactor: move `optionToFmtstr()` to `termseq.go` (#2323) --- misc.go | 10 ---------- misc_test.go | 16 ---------------- termseq.go | 10 ++++++++++ termseq_test.go | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/misc.go b/misc.go index cc5fa470..93d0ae9e 100644 --- a/misc.go +++ b/misc.go @@ -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. // 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. diff --git a/misc_test.go b/misc_test.go index d1a40917..3955f421 100644 --- a/misc_test.go +++ b/misc_test.go @@ -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) { tests := []struct { s string diff --git a/termseq.go b/termseq.go index bd140170..313d5147 100644 --- a/termseq.go +++ b/termseq.go @@ -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 // it to a `tcell.Style` object. // Legacy function that only accepts SGR. Kept for convenience. diff --git a/termseq_test.go b/termseq_test.go index 7d7dbb5e..d572ab25 100644 --- a/termseq_test.go +++ b/termseq_test.go @@ -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) { tests := []struct { s string