Skip to content

Commit e00652b

Browse files
committed
Extract icon lookup to icon package.
1 parent 609f0e5 commit e00652b

File tree

3 files changed

+105
-23
lines changed

3 files changed

+105
-23
lines changed

icon/files.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package icon
2+
3+
import (
4+
"path/filepath"
5+
)
6+
7+
// ForFile - Return rune icon corresponding to input file name
8+
func ForFile(name string) rune {
9+
ext := filepath.Ext(name)
10+
if len(ext) == 0 { return files["file"] }
11+
icon := files[ext[1:]]
12+
if icon == 0 { return files["file"] }
13+
return icon
14+
}
15+
16+
var files = map[string]rune{
17+
"ai": '\ue7b4',
18+
"android": '\ue70e',
19+
"apple": '\uf179',
20+
"audio": '\uf001',
21+
"avro": '\ue60b',
22+
"c": '\ue61e',
23+
"clj": '\ue768',
24+
"coffee": '\uf0f4',
25+
"conf": '\ue615',
26+
"cpp": '\ue61d',
27+
"css": '\ue749',
28+
"d": '\ue7af',
29+
"dart": '\ue798',
30+
"db": '\uf1c0',
31+
"diff": '\uf440',
32+
"doc": '\uf1c2',
33+
"docker": '\uf308',
34+
"ebook": '\ue28b',
35+
"env": '\uf462',
36+
"epub": '\ue28a',
37+
"erl": '\ue7b1',
38+
"file": '\uf15b',
39+
"font": '\uf031',
40+
"gform": '\uf298',
41+
"git": '\uf1d3',
42+
"go": '\ue626',
43+
"gruntfile.js": '\ue74c',
44+
"hs": '\ue777',
45+
"html": '\uf13b',
46+
"image": '\uf1c5',
47+
"iml": '\ue7b5',
48+
"java": '\ue204',
49+
"js": '\ue74e',
50+
"json": '\ue60b',
51+
"jsx": '\ue7ba',
52+
"less": '\ue758',
53+
"log": '\uf18d',
54+
"lua": '\ue620',
55+
"md": '\uf48a',
56+
"mustache": '\ue60f',
57+
"npmignore": '\ue71e',
58+
"pdf": '\uf1c1',
59+
"php": '\ue73d',
60+
"pl": '\ue769',
61+
"ppt": '\uf1c4',
62+
"psd": '\ue7b8',
63+
"py": '\ue606',
64+
"r": '\uf25d',
65+
"rb": '\ue21e',
66+
"rdb": '\ue76d',
67+
"rss": '\uf09e',
68+
"rubydoc": '\ue73b',
69+
"sass": '\ue603',
70+
"scala": '\ue737',
71+
"shell": '\uf489',
72+
"sqlite3": '\ue7c4',
73+
"styl": '\ue600',
74+
"tex": '\ue600',
75+
"ts": '\ue628',
76+
"twig": '\ue61c',
77+
"txt": '\uf15c',
78+
"video": '\uf03d',
79+
"vim": '\ue62b',
80+
"windows": '\uf17a',
81+
"xls": '\uf1c3',
82+
"xml": '\ue619',
83+
"yarn.lock": '\ue718',
84+
"yml": '\uf481',
85+
"zip": '\uf410',
86+
}

icon/folders.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package icon
2+
3+
// ForFolder - Return rune icon corresponding to input folder name
4+
func ForFolder(name string) rune {
5+
return '\uf115'
6+
}

node/root.go

+13-23
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,11 @@ import (
44
"os"
55
"log"
66
"fmt"
7-
"path/filepath"
87
"io/ioutil"
98
"github.com/fatih/color"
9+
"github.com/drn/nerd-ls/icon"
1010
)
1111

12-
var icons = map[string]rune{
13-
".DS_Store": '',
14-
".bash_history": '',
15-
".bash_profile": '',
16-
".conf": '',
17-
".env": '',
18-
".git": '',
19-
".go": '',
20-
".js": '',
21-
".json": '',
22-
".md": '',
23-
".rb": '',
24-
".yml": '',
25-
"dir": '',
26-
}
27-
2812
// Node - Contains all info necessary to render file or directory
2913
type Node struct {
3014
Name string
@@ -64,14 +48,20 @@ func new(file os.FileInfo) Node {
6448
func rawName(file os.FileInfo) string {
6549
suffix := ""
6650
if file.IsDir() { suffix = "/" }
67-
return fmt.Sprintf("%c %s%s ", icon(file), file.Name(), suffix)
51+
52+
return fmt.Sprintf(
53+
"%c %s%s ",
54+
fetchIcon(file),
55+
file.Name(),
56+
suffix,
57+
)
6858
}
6959

70-
func icon(file os.FileInfo) rune {
71-
if file.IsDir() { return icons["dir"] }
72-
icon := icons[filepath.Ext(file.Name())]
73-
if icon == 0 { return ' ' }
74-
return icon
60+
func fetchIcon(file os.FileInfo) rune {
61+
if file.IsDir() {
62+
return icon.ForFolder(file.Name())
63+
}
64+
return icon.ForFile(file.Name())
7565
}
7666

7767
func colorize(file os.FileInfo, name string) string {

0 commit comments

Comments
 (0)