From 2a32e2d838c5b10e0073af0926c63563a7db5ab5 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sun, 13 May 2018 10:55:18 +0100 Subject: [PATCH] operations: turn ListFormatted into a Format method on ListFormat --- cmd/lsf/lsf.go | 2 +- fs/operations/operations.go | 8 ++++---- fs/operations/operations_test.go | 26 +++++++++++++------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cmd/lsf/lsf.go b/cmd/lsf/lsf.go index 0a7468286..58c77d157 100644 --- a/cmd/lsf/lsf.go +++ b/cmd/lsf/lsf.go @@ -166,7 +166,7 @@ func Lsf(fsrc fs.Fs, out io.Writer) error { continue } } - fmt.Fprintln(out, operations.ListFormatted(&entry, &list)) + fmt.Fprintln(out, list.Format(entry)) } return nil }) diff --git a/fs/operations/operations.go b/fs/operations/operations.go index f4da97366..d6434afa1 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -1445,11 +1445,11 @@ func (l *ListFormat) AppendOutput(functionToAppend func() string) { l.output = append(l.output, functionToAppend) } -// ListFormatted prints information about specific file in specific format -func ListFormatted(entry *fs.DirEntry, list *ListFormat) string { - list.entry = *entry +// Format prints information about the DirEntry in the format defined +func (l *ListFormat) Format(entry fs.DirEntry) string { + l.entry = entry var out string - for _, fun := range list.output { + for _, fun := range l.output { out += fun() } return out diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index a8abc918f..a2000b8be 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -694,43 +694,43 @@ func TestListFormat(t *testing.T) { var list operations.ListFormat list.AddPath() list.SetDirSlash(false) - assert.Equal(t, "subdir", operations.ListFormatted(&items[1], &list)) + assert.Equal(t, "subdir", list.Format(items[1])) list.SetDirSlash(true) - assert.Equal(t, "subdir/", operations.ListFormatted(&items[1], &list)) + assert.Equal(t, "subdir/", list.Format(items[1])) list.SetOutput(nil) - assert.Equal(t, "", operations.ListFormatted(&items[1], &list)) + assert.Equal(t, "", list.Format(items[1])) list.AppendOutput(func() string { return "a" }) list.AppendOutput(func() string { return "b" }) - assert.Equal(t, "ab", operations.ListFormatted(&items[1], &list)) + assert.Equal(t, "ab", list.Format(items[1])) list.SetSeparator(":::") - assert.Equal(t, "a:::b", operations.ListFormatted(&items[1], &list)) + assert.Equal(t, "a:::b", list.Format(items[1])) list.SetOutput(nil) list.AddModTime() - assert.Equal(t, items[0].ModTime().Local().Format("2006-01-02 15:04:05"), operations.ListFormatted(&items[0], &list)) + assert.Equal(t, items[0].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[0])) list.SetOutput(nil) list.AddID() - _ = operations.ListFormatted(&items[0], &list) // Can't really check anything - at least it didn't panic! + _ = list.Format(items[0]) // Can't really check anything - at least it didn't panic! list.SetOutput(nil) list.AddMimeType() - assert.Contains(t, operations.ListFormatted(&items[0], &list), "/") - assert.Equal(t, "inode/directory", operations.ListFormatted(&items[1], &list)) + assert.Contains(t, list.Format(items[0]), "/") + assert.Equal(t, "inode/directory", list.Format(items[1])) list.SetOutput(nil) list.AddSize() - assert.Equal(t, "1", operations.ListFormatted(&items[0], &list)) + assert.Equal(t, "1", list.Format(items[0])) list.AddPath() list.AddModTime() list.SetDirSlash(true) list.SetSeparator("__SEP__") - assert.Equal(t, "1__SEP__a__SEP__"+items[0].ModTime().Local().Format("2006-01-02 15:04:05"), operations.ListFormatted(&items[0], &list)) - assert.Equal(t, fmt.Sprintf("%d", items[1].Size())+"__SEP__subdir/__SEP__"+items[1].ModTime().Local().Format("2006-01-02 15:04:05"), operations.ListFormatted(&items[1], &list)) + assert.Equal(t, "1__SEP__a__SEP__"+items[0].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[0])) + assert.Equal(t, fmt.Sprintf("%d", items[1].Size())+"__SEP__subdir/__SEP__"+items[1].ModTime().Local().Format("2006-01-02 15:04:05"), list.Format(items[1])) for _, test := range []struct { ht hash.Type @@ -742,7 +742,7 @@ func TestListFormat(t *testing.T) { } { list.SetOutput(nil) list.AddHash(test.ht) - got := operations.ListFormatted(&items[0], &list) + got := list.Format(items[0]) if got != "UNSUPPORTED" && got != "" { assert.Equal(t, test.want, got) }