From a325243047696e052ec76cc2e46e406b940ae012 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Wed, 29 Dec 2021 21:34:44 +0100 Subject: [PATCH] add tree view --- internal/piwigo/piwigotools/tree.go | 36 +++++++++++++++++ internal/piwigocli/images_list.go | 63 +++-------------------------- 2 files changed, 42 insertions(+), 57 deletions(-) diff --git a/internal/piwigo/piwigotools/tree.go b/internal/piwigo/piwigotools/tree.go index 447ebd9..23cb82a 100644 --- a/internal/piwigo/piwigotools/tree.go +++ b/internal/piwigo/piwigotools/tree.go @@ -8,6 +8,7 @@ import ( type Tree interface { AddNode(string) Tree FlatView() chan string + TreeView() chan string } type node struct { @@ -51,3 +52,38 @@ func (t *node) FlatView() (out chan string) { }() return out } + +func (t *node) TreeView() (out chan string) { + out = make(chan string) + treeLinkChar := "│ " + treeMidChar := "├── " + treeEndChar := "└── " + treeAfterEndChar := " " + + go func() { + defer close(out) + + var tree func(string, *node) + + tree = func(prefix string, t *node) { + for i, st := range t.Children { + switch i { + case len(t.Children) - 1: + out <- prefix + treeEndChar + st.Name + tree(prefix+treeAfterEndChar, st) + case 0: + out <- prefix + treeMidChar + st.Name + tree(prefix+treeLinkChar, st) + default: + out <- prefix + treeMidChar + st.Name + tree(prefix+treeLinkChar, st) + } + } + } + + out <- t.Name + tree("", t) + + }() + return out +} diff --git a/internal/piwigocli/images_list.go b/internal/piwigocli/images_list.go index 4ff0718..2d5b69a 100644 --- a/internal/piwigocli/images_list.go +++ b/internal/piwigocli/images_list.go @@ -114,66 +114,15 @@ func (c *ImagesListCommand) Execute(args []string) error { } bar.Close() - results := rootTree.FlatView() + var results chan string + if c.Tree { + results = rootTree.TreeView() + } else { + results = rootTree.FlatView() + } for filename := range results { fmt.Println(filename) } - // sort.Strings(results) - - // if !c.Tree { - // for _, r := range results { - // fmt.Println(r) - // } - // return nil - // } - - // type Tree struct { - // Name string - // Children []*Tree - // } - - // treeMap := make(map[string]*Tree) - // treeMap[""] = &Tree{Name: "."} - - // for _, r := range results { - // parentpath := "" - // fullpath := "" - // for _, s := range strings.Split(r, "/") { - // parentpath = fullpath - // fullpath += s + "/" - // if _, ok := treeMap[fullpath]; ok { - // continue - // } - // treeMap[fullpath] = &Tree{Name: s} - // treeMap[parentpath].Children = append(treeMap[parentpath].Children, treeMap[fullpath]) - // } - // } - - // var treeView func(*Tree, string) - // treeLinkChar := "│ " - // treeMidChar := "├── " - // treeEndChar := "└── " - // treeAfterEndChar := " " - - // treeView = func(t *Tree, prefix string) { - // for i, st := range t.Children { - // switch i { - // case len(t.Children) - 1: - // fmt.Println(prefix + treeEndChar + st.Name) - // treeView(st, prefix+treeAfterEndChar) - // case 0: - // fmt.Println(prefix + treeMidChar + st.Name) - // treeView(st, prefix+treeLinkChar) - // default: - // fmt.Println(prefix + treeMidChar + st.Name) - // treeView(st, prefix+treeLinkChar) - // } - // } - // } - - // fmt.Println(treeMap[""].Name) - // treeView(treeMap[""], "") - return nil }