-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
210 lines (196 loc) · 4.78 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)
var langStore = map[string]int64{}
var langMap map[string]string = loadLanguagesJSON()
var totalSize int64 = 0
var languageJSONPath string = getLanguageJSONPath()
type file struct {
name string
size int64
}
func getLanguageJSONPath() string {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
return dirname + "/.config/barley/" + "languages.json"
}
func loadLanguagesJSON() map[string]string {
jsonFile, err := os.Open(languageJSONPath)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]string
json.Unmarshal([]byte(byteValue), &result)
return result
}
func getLang(filename string) string {
filenameRunes := []rune(filename)
reverse(filenameRunes)
extensionChars := []rune{}
for i := range filenameRunes {
currentChar := filenameRunes[i]
if currentChar == '.' {
reverse(extensionChars)
break
}
extensionChars = append(extensionChars, currentChar)
}
return langMap[string(extensionChars)]
}
func reverse(s interface{}) {
n := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}
}
func trackFile(langName string, f file, detailed bool) {
langStore[langName] += f.size
totalSize += f.size
if detailed {
sizeStr := strconv.Itoa(int(f.size))
fmt.Printf("\033[0;34m" + langName + "\033[0m" + strings.Repeat(" ", (30-len(langName))))
fmt.Printf(f.name)
fmt.Printf("\033[0;32m" + " (" + sizeStr + ")" + "\033[0m")
fmt.Println()
}
}
func forFiles(f file, detailed bool) {
fileLanguage := getLang(f.name)
if fileLanguage != "" {
trackFile(fileLanguage, f, detailed)
}
}
func visualizeStore() {
fmt.Println("\nTOTAL:")
for langName, size := range langStore {
percentage := strconv.Itoa(int((size*100)/totalSize)) + "%%"
langPreviewStr := "\033[0;34m" + langName + "\033[0m" + "\033[0;32m" + " (" + strconv.Itoa(int(size)) + ")" + "\033[0m"
fmt.Printf(langPreviewStr)
fmt.Printf(strings.Repeat(" ", (70 - len(langPreviewStr))))
fmt.Printf(percentage)
fmt.Println()
}
}
func visualizeBars() {
color := []string{
"\033[0;31m",
"\033[0;32m",
"\033[0;33m",
"\033[0;34m",
"\033[0;35m",
"\033[0;36m",
"\033[0;37m",
}
i := 0
colorReset := "\033[0m"
colorBlack := "\033[0;30m"
barView := []string{}
summary := []string{}
for langName, v := range langStore {
fiftyNumerator := uint8((v * 50) / totalSize)
if fiftyNumerator >= 1 {
langSummary := color[i] + "\n" + langName + " => " + strconv.Itoa(int(fiftyNumerator)*2) + "%" + " (size: " + strconv.Itoa(int(v)) + ")"
barView = append(barView, color[i]+strings.Repeat("█", int(fiftyNumerator))+"▌")
summary = append(summary, langSummary)
i++
}
if i >= len(color) {
break
}
}
reverse(barView)
reverse(summary)
if len(langStore) > len(color) {
summary = append(summary, colorReset+"\n...")
}
barView = append(barView, colorReset)
barViewString := strings.Join(barView[:], "")
summaryString := strings.Join(summary[:], "")
// get parent directory name
wd, err := os.Getwd()
if err != nil {
panic(err)
}
projectName := filepath.Base(wd)
headerFmt := "\n" + projectName + " " + colorBlack + wd + colorReset + "\n"
// print out all the formatted data
fmt.Println(headerFmt)
fmt.Println(barViewString)
fmt.Println(summaryString + "\n")
}
func main() {
help := false
detailed := false
args := os.Args
for i := range args {
// command line arguments
switch args[i] {
case "--detailed", "-d":
detailed = true
case "--help", "-h":
help = true
fmt.Println(`
barley
barley analyzes the programming languages used in the current directory and produces a GitHub-like programming language usage bar
COMMANDS:
--detailed -d: Also provides file-specific information
--help -h: Shows this help message
`)
}
}
if !help {
err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
switch info.Name() {
case ".git":
if info.IsDir() {
return filepath.SkipDir
}
case "node_modules":
if info.IsDir() {
return filepath.SkipDir
}
case "package.json":
if !info.IsDir() {
return filepath.SkipDir
}
case "package-lock.json":
if !info.IsDir() {
return filepath.SkipDir
}
}
if info.IsDir() && info.Name() == ".git" {
return filepath.SkipDir
}
// perform analysis on files, all values stored in langStore
forFiles(file{name: info.Name(), size: info.Size()}, detailed)
return nil
})
if err != nil {
log.Println(err)
}
// visualize global language store
if detailed {
visualizeStore()
}
// visualize results
visualizeBars()
}
}