mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 10:12:37 +02:00
simplify tree
This commit is contained in:
parent
a325243047
commit
84b9445e1d
@ -3,17 +3,19 @@ package piwigotools
|
|||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tree interface {
|
type Tree interface {
|
||||||
AddNode(string) Tree
|
Add(string) Tree
|
||||||
|
AddPath(string) Tree
|
||||||
FlatView() chan string
|
FlatView() chan string
|
||||||
TreeView() chan string
|
TreeView() chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
type node struct {
|
type node struct {
|
||||||
Name string
|
Name string
|
||||||
Children []*node
|
children map[string]*node
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTree() Tree {
|
func NewTree() Tree {
|
||||||
@ -22,11 +24,41 @@ func NewTree() Tree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *node) AddNode(name string) Tree {
|
func (t *node) Add(name string) Tree {
|
||||||
n := &node{Name: name}
|
if t.children == nil {
|
||||||
t.Children = append(t.Children, n)
|
t.children = map[string]*node{}
|
||||||
|
}
|
||||||
|
n, ok := t.children[name]
|
||||||
|
if !ok {
|
||||||
|
n = &node{Name: name}
|
||||||
|
t.children[name] = n
|
||||||
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
func (t *node) AddPath(path string) Tree {
|
||||||
|
n := Tree(t)
|
||||||
|
for _, name := range strings.Split(path, "/") {
|
||||||
|
n = n.Add(name)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *node) Children() []*node {
|
||||||
|
childs := make([]*node, len(t.children))
|
||||||
|
i := 0
|
||||||
|
for _, n := range t.children {
|
||||||
|
childs[i] = n
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
sort.Slice(childs, func(i, j int) bool {
|
||||||
|
return childs[i].Name < childs[j].Name
|
||||||
|
})
|
||||||
|
return childs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *node) HasChildren() bool {
|
||||||
|
return t.children != nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t *node) FlatView() (out chan string) {
|
func (t *node) FlatView() (out chan string) {
|
||||||
out = make(chan string)
|
out = make(chan string)
|
||||||
@ -35,14 +67,11 @@ func (t *node) FlatView() (out chan string) {
|
|||||||
var flatten func(string, *node)
|
var flatten func(string, *node)
|
||||||
|
|
||||||
flatten = func(path string, t *node) {
|
flatten = func(path string, t *node) {
|
||||||
switch t.Children {
|
switch t.HasChildren() {
|
||||||
case nil:
|
case false:
|
||||||
out <- path
|
out <- path
|
||||||
default:
|
case true:
|
||||||
sort.Slice(t.Children, func(i, j int) bool {
|
for _, child := range t.Children() {
|
||||||
return t.Children[i].Name < t.Children[j].Name
|
|
||||||
})
|
|
||||||
for _, child := range t.Children {
|
|
||||||
flatten(filepath.Join(path, child.Name), child)
|
flatten(filepath.Join(path, child.Name), child)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,9 +95,10 @@ func (t *node) TreeView() (out chan string) {
|
|||||||
var tree func(string, *node)
|
var tree func(string, *node)
|
||||||
|
|
||||||
tree = func(prefix string, t *node) {
|
tree = func(prefix string, t *node) {
|
||||||
for i, st := range t.Children {
|
children := t.Children()
|
||||||
|
for i, st := range children {
|
||||||
switch i {
|
switch i {
|
||||||
case len(t.Children) - 1:
|
case len(children) - 1:
|
||||||
out <- prefix + treeEndChar + st.Name
|
out <- prefix + treeEndChar + st.Name
|
||||||
tree(prefix+treeAfterEndChar, st)
|
tree(prefix+treeAfterEndChar, st)
|
||||||
case 0:
|
case 0:
|
||||||
@ -83,7 +113,6 @@ func (t *node) TreeView() (out chan string) {
|
|||||||
|
|
||||||
out <- t.Name
|
out <- t.Name
|
||||||
tree("", t)
|
tree("", t)
|
||||||
|
|
||||||
}()
|
}()
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,6 @@ func (c *ImagesListCommand) Execute(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rootTree := piwigotools.NewTree()
|
rootTree := piwigotools.NewTree()
|
||||||
cache := map[string]piwigotools.Tree{}
|
|
||||||
|
|
||||||
bar := progressbar.Default(1, "listing")
|
bar := progressbar.Default(1, "listing")
|
||||||
for page := 0; ; page++ {
|
for page := 0; ; page++ {
|
||||||
@ -81,29 +80,14 @@ func (c *ImagesListCommand) Execute(args []string) error {
|
|||||||
|
|
||||||
for _, image := range resp.Images {
|
for _, image := range resp.Images {
|
||||||
for _, cat := range image.Categories {
|
for _, cat := range image.Categories {
|
||||||
categoryPath := strings.Split(categories[cat.Id].Name[len(rootCatName):], " / ")
|
filename := filepath.Join(
|
||||||
if !filter.MatchString(
|
strings.ReplaceAll(categories[cat.Id].Name[len(rootCatName):], " / ", "/"),
|
||||||
filepath.Join(
|
image.Filename,
|
||||||
filepath.Join(categoryPath...),
|
)
|
||||||
image.Filename,
|
if !filter.MatchString(filename) {
|
||||||
),
|
|
||||||
) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
curpath := ""
|
rootTree.AddPath(filename)
|
||||||
cur := rootTree
|
|
||||||
for _, path := range categoryPath {
|
|
||||||
curpath = filepath.Join(curpath, path)
|
|
||||||
node, ok := cache[curpath]
|
|
||||||
switch ok {
|
|
||||||
case true:
|
|
||||||
cur = node
|
|
||||||
case false:
|
|
||||||
cur = cur.AddNode(path)
|
|
||||||
cache[curpath] = cur
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cur.AddNode(image.Filename)
|
|
||||||
}
|
}
|
||||||
bar.Add(1)
|
bar.Add(1)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user