From 1b00f281d98e2cf82e0e6aed91b776c18eb73292 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 21 Feb 2023 17:05:14 +0100 Subject: dirlist: fix conversion of dirlist-format strings.SplitN is not like python str.split() method... It requires an exact number of items including the trailing non split part. Add unit tests to ensure it works. Fixes: 6cfbc87d8ab0 ("dirlist: use templates instead of % mini language") Reported-by: John Mcenroy Signed-off-by: Robin Jarry Tested-by: Ben Lee-Cohen Reviewed-by: Vincenzo Palazzo --- config/ui.go | 6 +++--- config/ui_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/config/ui.go b/config/ui.go index 599651d1..b1330ffd 100644 --- a/config/ui.go +++ b/config/ui.go @@ -581,12 +581,12 @@ func convertDirlistFormat(format string) (string, string) { return s }, ) - tokens := strings.SplitN(tmpl, "%>", 1) + tokens := strings.SplitN(tmpl, "%>", 2) switch len(tokens) { case 2: - return tokens[0], tokens[1] + return strings.TrimSpace(tokens[0]), strings.TrimSpace(tokens[1]) case 1: - return tokens[0], "" + return strings.TrimSpace(tokens[0]), "" default: return "", "" } diff --git a/config/ui_test.go b/config/ui_test.go index 677dcc86..80373da2 100644 --- a/config/ui_test.go +++ b/config/ui_test.go @@ -45,3 +45,21 @@ func TestConvertIndexFormat(t *testing.T) { assert.Equal(t, ALIGN_LEFT|WIDTH_AUTO, columns[3].Flags) assert.Equal(t, `{{.Subject}}`, templateText(columns[3].Template)) } + +func TestConvertDirlistFormat(t *testing.T) { + left, right := convertDirlistFormat("%n %>r") + assert.Equal(t, "{{.Folder}}", left) + assert.Equal(t, unreadExists, right) + + left, right = convertDirlistFormat("%n %>r ") + assert.Equal(t, "{{.Folder}}", left) + assert.Equal(t, unreadExists, right) + + left, right = convertDirlistFormat("%r%>n") + assert.Equal(t, unreadExists, left) + assert.Equal(t, "{{.Folder}}", right) + + left, right = convertDirlistFormat("%>N") + assert.Equal(t, "", left) + assert.Equal(t, "{{.Folder | compactDir}}", right) +} -- cgit