mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-06-21 22:39:56 +02:00
tree map list of files, then flatview
This commit is contained in:
parent
654f9b5634
commit
d1814742fa
53
internal/piwigo/piwigotools/tree.go
Normal file
53
internal/piwigo/piwigotools/tree.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package piwigotools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tree interface {
|
||||||
|
AddNode(string) Tree
|
||||||
|
FlatView() chan string
|
||||||
|
}
|
||||||
|
|
||||||
|
type node struct {
|
||||||
|
Name string
|
||||||
|
Children []*node
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTree() Tree {
|
||||||
|
return &node{
|
||||||
|
Name: ".",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *node) AddNode(name string) Tree {
|
||||||
|
n := &node{Name: name}
|
||||||
|
t.Children = append(t.Children, n)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *node) FlatView() (out chan string) {
|
||||||
|
out = make(chan string)
|
||||||
|
go func() {
|
||||||
|
defer close(out)
|
||||||
|
var flatten func(string, *node)
|
||||||
|
|
||||||
|
flatten = func(path string, t *node) {
|
||||||
|
switch t.Children {
|
||||||
|
case nil:
|
||||||
|
out <- path
|
||||||
|
default:
|
||||||
|
sort.Slice(t.Children, func(i, j int) bool {
|
||||||
|
return t.Children[i].Name < t.Children[j].Name
|
||||||
|
})
|
||||||
|
for _, child := range t.Children {
|
||||||
|
flatten(filepath.Join(path, child.Name), child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flatten("", t)
|
||||||
|
}()
|
||||||
|
return out
|
||||||
|
}
|
@ -3,8 +3,8 @@ package piwigocli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/celogeek/piwigo-cli/internal/piwigo"
|
"github.com/celogeek/piwigo-cli/internal/piwigo"
|
||||||
@ -59,7 +59,9 @@ func (c *ImagesListCommand) Execute(args []string) error {
|
|||||||
filter = regexp.MustCompile("(?i)" + c.Filter)
|
filter = regexp.MustCompile("(?i)" + c.Filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
var results []string
|
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++ {
|
||||||
var resp ImagesListResult
|
var resp ImagesListResult
|
||||||
@ -78,17 +80,31 @@ func (c *ImagesListCommand) Execute(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, image := range resp.Images {
|
for _, image := range resp.Images {
|
||||||
for _, category := range image.Categories {
|
for _, cat := range image.Categories {
|
||||||
cat, ok := categories[category.Id]
|
categoryPath := strings.Split(categories[cat.Id].Name[len(rootCatName):], " / ")
|
||||||
if !ok {
|
if !filter.MatchString(
|
||||||
|
filepath.Join(
|
||||||
|
filepath.Join(categoryPath...),
|
||||||
|
image.Filename,
|
||||||
|
),
|
||||||
|
) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
catName := strings.ReplaceAll(cat.Name[len(rootCatName):], " / ", "/")
|
curpath := ""
|
||||||
filename := fmt.Sprintf("%s/%s", catName, image.Filename)
|
cur := rootTree
|
||||||
if filter.MatchString(filename) {
|
for _, path := range categoryPath {
|
||||||
results = append(results, filename)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,61 +114,66 @@ func (c *ImagesListCommand) Execute(args []string) error {
|
|||||||
}
|
}
|
||||||
bar.Close()
|
bar.Close()
|
||||||
|
|
||||||
sort.Strings(results)
|
results := rootTree.FlatView()
|
||||||
|
for filename := range results {
|
||||||
if !c.Tree {
|
fmt.Println(filename)
|
||||||
for _, r := range results {
|
|
||||||
fmt.Println(r)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tree struct {
|
// sort.Strings(results)
|
||||||
Name string
|
|
||||||
Children []*Tree
|
|
||||||
}
|
|
||||||
|
|
||||||
treeMap := make(map[string]*Tree)
|
// if !c.Tree {
|
||||||
treeMap[""] = &Tree{Name: "."}
|
// for _, r := range results {
|
||||||
|
// fmt.Println(r)
|
||||||
|
// }
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
for _, r := range results {
|
// type Tree struct {
|
||||||
parentpath := ""
|
// Name string
|
||||||
fullpath := ""
|
// Children []*Tree
|
||||||
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)
|
// treeMap := make(map[string]*Tree)
|
||||||
treeLinkChar := "│ "
|
// treeMap[""] = &Tree{Name: "."}
|
||||||
treeMidChar := "├── "
|
|
||||||
treeEndChar := "└── "
|
|
||||||
treeAfterEndChar := " "
|
|
||||||
|
|
||||||
treeView = func(t *Tree, prefix string) {
|
// for _, r := range results {
|
||||||
for i, st := range t.Children {
|
// parentpath := ""
|
||||||
switch i {
|
// fullpath := ""
|
||||||
case len(t.Children) - 1:
|
// for _, s := range strings.Split(r, "/") {
|
||||||
fmt.Println(prefix + treeEndChar + st.Name)
|
// parentpath = fullpath
|
||||||
treeView(st, prefix+treeAfterEndChar)
|
// fullpath += s + "/"
|
||||||
case 0:
|
// if _, ok := treeMap[fullpath]; ok {
|
||||||
fmt.Println(prefix + treeMidChar + st.Name)
|
// continue
|
||||||
treeView(st, prefix+treeLinkChar)
|
// }
|
||||||
default:
|
// treeMap[fullpath] = &Tree{Name: s}
|
||||||
fmt.Println(prefix + treeMidChar + st.Name)
|
// treeMap[parentpath].Children = append(treeMap[parentpath].Children, treeMap[fullpath])
|
||||||
treeView(st, prefix+treeLinkChar)
|
// }
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(treeMap[""].Name)
|
// var treeView func(*Tree, string)
|
||||||
treeView(treeMap[""], "")
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user