-
Notifications
You must be signed in to change notification settings - Fork 0
/
zee.go
115 lines (94 loc) · 1.8 KB
/
zee.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
110
111
112
113
114
115
package main
import (
"fmt"
"io/fs"
"os"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
const (
// tag
TagA = "-a"
TagL = "-l"
// space
spc = " "
spc2 = " "
// content-type
BIN = "application/octet-stream"
)
type ZeeConfig struct {
termwidth int
path string
handleTag HandleTag
}
type ItemStat struct {
fs.FileInfo
val string
length int
}
var args = os.Args
func main() {
w, _, err := terminal.GetSize(0)
if err != nil {
panic(err.Error())
}
config := &ZeeConfig{
termwidth: w,
path: ".",
handleTag: SupportedTags[TagA],
}
if len(args) > 1 {
for _, v := range args[1:] {
if strings.Contains(v, "-") {
if f, ok := SupportedTags[v]; ok {
config.handleTag = f
} else {
throwError(fmt.Errorf("undefined tag %v", v))
}
} else {
config.path = v
}
}
}
s, err := readAndParseDir(config)
if err != nil {
throwError(err)
}
fmt.Println(s)
}
func readAndParseDir(config *ZeeConfig) (string, error) {
dir, err := os.ReadDir(config.path)
if err != nil {
return "", err
}
if len(dir) == 0 {
return "", nil
}
var ItemStats []ItemStat
for _, v := range dir {
info, err := v.Info()
if err != nil {
return "", nil
}
strItem := createStringItem(info)
ItemStats = append(ItemStats, ItemStat{
val: strItem,
length: len(strItem),
FileInfo: info,
})
}
return config.handleTag(&ItemStats, config.termwidth)
}
func createStringItem(info fs.FileInfo) string {
var icon string
if info.IsDir() {
icon = Type["dir"]
} else if v, ok := Type[strings.ToLower(strings.Split(info.Name(), ".")[len(strings.Split(info.Name(), "."))-1])]; ok {
icon = v
} else if info.Mode().IsRegular() && info.Mode()&0o111 != 0 {
icon = Type["bin"]
} else {
icon = Type["text"]
}
return fmt.Sprint(icon, spc, info.Name())
}