-
Notifications
You must be signed in to change notification settings - Fork 0
/
miller.go
109 lines (96 loc) · 2.39 KB
/
miller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package miller
import (
"os"
"path"
"time"
)
// Columns contains the state of the miller columns
type Columns struct {
CurrentFolder []string
Categories []*Category
}
// Category is a column
type Category struct {
Path []string `json:"path"`
CategoryName string `json:"categoryName"`
IsLowestLevel bool `json:"isLowestLevel"`
Items []*CategoryItem `json:"items"`
}
// CategoryItem are the items in a column
type CategoryItem struct {
Name string `json:"name,omitempty"`
Size int64 `json:"size,omitempty"`
Mode os.FileMode `json:"mode,omitempty"`
ModTime time.Time `json:"modTime,omitempty"`
}
// NewColumns initialises the cascading list
func NewColumns(dir []string) (*Columns, error) {
c := &Columns{
Categories: []*Category{},
CurrentFolder: dir,
}
items, err := c.ListDir(dir)
if err != nil {
return nil, err
}
c.Categories = append(c.Categories, &Category{
Items: items,
Path: dir,
IsLowestLevel: true,
})
return c, nil
}
// Descend moves down a level in the tree, appending the category and folder to the struct
func (c *Columns) Descend(dir string) error {
args := append(c.CurrentFolder, dir)
file, err := os.Open(path.Join(args...))
if err != nil {
return err
}
defer file.Close()
c.CurrentFolder = append(c.CurrentFolder, dir)
items, err := c.ListDir(c.CurrentFolder)
if err != nil {
return err
}
c.Categories = append(c.Categories, &Category{
Items: items,
Path: c.CurrentFolder,
IsLowestLevel: false,
})
return nil
}
// Ascend moves up a level in the tree, removing the last category and folder in the struct
func (c *Columns) Ascend() {
if len(c.Categories) == 1 {
return
}
c.CurrentFolder = c.CurrentFolder[:len(c.CurrentFolder)-1]
c.Categories = c.Categories[:len(c.Categories)-1]
}
// ListDir returns all the folders in directory
func (c *Columns) ListDir(directory []string) ([]*CategoryItem, error) {
dir, err := os.Open(path.Join(directory...))
if err != nil {
return nil, err
}
defer dir.Close()
entries, err := dir.Readdir(0)
if err != nil {
return nil, err
}
list := []*CategoryItem{}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
f := &CategoryItem{
Name: entry.Name(),
Size: entry.Size(),
Mode: entry.Mode(),
ModTime: entry.ModTime(),
}
list = append(list, f)
}
return list, nil
}