Debian Codegraph @

icons.go

raw
1 package main
2
3 import (
4 "log"
5 "os"
6 "path/filepath"
7 "strings"
8
9 "github.com/gdamore/tcell/v3"
10 )
11
12 type iconDef struct {
13 icon string
14 hasStyle bool
15 style tcell.Style
16 }
17
18 type iconMap struct {
19 icons map[string]iconDef
20 useLinkTarget bool
21 }
22
23 func iconWithoutStyle(icon string) iconDef {
24 return iconDef{icon, false, tcell.StyleDefault}
25 }
26
27 func iconWithStyle(icon string, style tcell.Style) iconDef {
28 return iconDef{icon, true, style}
29 }
30
31 func parseIcons() iconMap {
32 im := iconMap{
33 icons: make(map[string]iconDef),
34 useLinkTarget: false,
35 }
36
37 defaultIcons := []string{
38 "ln=l",
39 "or=l",
40 "tw=t",
41 "ow=d",
42 "st=t",
43 "di=d",
44 "pi=p",
45 "so=s",
46 "bd=b",
47 "cd=c",
48 "su=u",
49 "sg=g",
50 "ex=x",
51 "fi=-",
52 }
53
54 im.parseEnv(strings.Join(defaultIcons, ":"))
55
56 if env := os.Getenv("LF_ICONS"); env != "" {
57 im.parseEnv(env)
58 }
59
60 for _, path := range gIconsPaths {
61 if _, err := os.Stat(path); !os.IsNotExist(err) {
62 im.parseFile(path)
63 }
64 }
65
66 return im
67 }
68
69 func (im *iconMap) parseFile(path string) {
70 log.Printf("reading file: %s", path)
71
72 f, err := os.Open(path)
73 if err != nil {
74 log.Printf("opening icons file: %s", err)
75 return
76 }
77 defer f.Close()
78
79 arrs, err := readArrays(f, 1, 3)
80 if err != nil {
81 log.Printf("reading icons file: %s", err)
82 return
83 }
84
85 for _, arr := range arrs {
86 im.parseArray(arr)
87 }
88 }
89
90 func (im *iconMap) parseEnv(env string) {
91 for entry := range strings.SplitSeq(env, ":") {
92 if entry == "" {
93 continue
94 }
95
96 pair := strings.Split(entry, "=")
97
98 if len(pair) != 2 {
99 log.Printf("invalid $LF_ICONS entry: %s", entry)
100 return
101 }
102
103 im.parseArray(pair)
104 }
105 }
106
107 func (im *iconMap) parseArray(arr []string) {
108 key := replaceTilde(arr[0])
109
110 if filepath.IsAbs(key) {
111 key = filepath.Clean(key)
112 }
113
114 switch len(arr) {
115 case 1:
116 delete(im.icons, key)
117 case 2:
118 icon := arr[1]
119 if key == "ln" && icon == "target" {
120 im.useLinkTarget = true
121 } else {
122 im.icons[key] = iconWithoutStyle(icon)
123 }
124 case 3:
125 icon, color := arr[1], arr[2]
126 im.icons[key] = iconWithStyle(icon, applySGR(color, tcell.StyleDefault))
127 }
128 }
129
130 func (im iconMap) get(f *file) iconDef {
131 if val, ok := im.icons[f.path]; ok {
132 return val
133 }
134
135 if f.IsDir() {
136 if val, ok := im.icons[f.Name()+"/"]; ok {
137 return val
138 }
139 }
140
141 var key string
142
143 switch {
144 case f.linkState == working && !im.useLinkTarget:
145 key = "ln"
146 case f.linkState == broken:
147 key = "or"
148 case f.IsDir() && f.Mode()&os.ModeSticky != 0 && f.Mode()&0o002 != 0:
149 key = "tw"
150 case f.IsDir() && f.Mode()&0o002 != 0:
151 key = "ow"
152 case f.IsDir() && f.Mode()&os.ModeSticky != 0:
153 key = "st"
154 case f.IsDir():
155 key = "di"
156 case f.Mode()&os.ModeNamedPipe != 0:
157 key = "pi"
158 case f.Mode()&os.ModeSocket != 0:
159 key = "so"
160 case f.Mode()&os.ModeCharDevice != 0:
161 key = "cd"
162 case f.Mode()&os.ModeDevice != 0:
163 key = "bd"
164 case f.Mode()&os.ModeSetuid != 0:
165 key = "su"
166 case f.Mode()&os.ModeSetgid != 0:
167 key = "sg"
168 case isExecutable(f.FileInfo):
169 key = "ex"
170 }
171
172 if val, ok := im.icons[key]; ok {
173 return val
174 }
175
176 if val, ok := im.icons[f.Name()+"*"]; ok {
177 return val
178 }
179
180 if val, ok := im.icons["*"+f.Name()]; ok {
181 return val
182 }
183
184 if val, ok := im.icons[filepath.Base(f.Name())+".*"]; ok {
185 return val
186 }
187
188 if val, ok := im.icons["*"+strings.ToLower(f.ext)]; ok {
189 return val
190 }
191
192 if val, ok := im.icons["fi"]; ok {
193 return val
194 }
195
196 return iconWithoutStyle(" ")
197 }