-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
148 lines (135 loc) · 3.53 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
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"net"
"net/http"
"sort"
"strings"
"time"
"github.com/pkg/browser"
"github.com/subtle-byte/ghloc/internal/infrastructure/local_files_provider"
"github.com/subtle-byte/ghloc/internal/server/rest"
"github.com/subtle-byte/ghloc/internal/service/loc_count"
)
//go:embed server_static
var serverStatic embed.FS
func main() {
var matcher *string
flag.Func("m", "sets `match expression` used to filter results, sets flag -c implicitly", func(s string) error {
matcher = &s
return nil
})
cli := flag.Bool("c", false, "output to` `console")
flag.Parse()
locsForPaths := countLOCsForPaths()
if matcher != nil {
printInConsole(locsForPaths, *matcher)
} else if *cli {
printInConsole(locsForPaths, "")
} else {
runServer(locsForPaths)
}
}
func countLOCsForPaths() []loc_count.LOCForPath {
fmt.Print("Counting lines of code...")
counted := make(chan bool, 1)
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-counted:
return
case <-ticker.C:
fmt.Print(".")
}
}
}()
files, err := local_files_provider.GetFilesInDir(".")
if err != nil {
panic(err)
}
locCounter := loc_count.NewFilesLOCCounter()
for _, file := range files {
func() {
fileReader, err := file.Opener()
if err != nil {
panic(err)
}
defer fileReader.Close()
err = locCounter.AddFile(file.Path, fileReader)
if err != nil {
panic(err)
}
}()
}
locsForPaths := locCounter.GetLOCsForPaths()
counted <- true
fmt.Println()
return locsForPaths
}
func printInConsole(locsForPaths []loc_count.LOCForPath, matcher string) {
statTree := loc_count.BuildStatTree(locsForPaths, nil, &matcher)
type LocAndLang struct {
Loc int
Lang string
}
stats := []LocAndLang{}
for lang, loc := range statTree.LOCByLangs {
stats = append(stats, LocAndLang{loc, lang})
}
sort.Slice(stats, func(i, j int) bool { return stats[i].Loc > stats[j].Loc })
firstWidth := 50
secondWidth := 20
width := firstWidth + secondWidth + 3
printPair := func(a, b interface{}, sep string) {
aStr := fmt.Sprint(a)
if len(aStr) > firstWidth {
aStr = aStr[:firstWidth-3] + "..."
}
bStr := fmt.Sprint(b)
sepLen := (firstWidth - len(aStr)) + (secondWidth - len(bStr))
fmt.Printf(" %v%v%v \n", aStr, strings.Repeat(sep, sepLen+1), bStr)
}
fmt.Println(strings.Repeat("=", width))
printPair("File type", "Lines of code", " ")
fmt.Println(strings.Repeat("=", width))
for _, stat := range stats {
printPair(stat.Lang, stat.Loc, ".")
}
fmt.Println(strings.Repeat("=", width))
printPair("Total", statTree.LOC, " ")
fmt.Println(strings.Repeat("=", width))
}
func runServer(locsForPaths []loc_count.LOCForPath) {
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var filter *string
if filters := r.Form["filter"]; len(filters) >= 1 {
filter = &filters[0]
}
var matcher *string
if matchers := r.Form["match"]; len(matchers) >= 1 {
matcher = &matchers[0]
}
statTree := loc_count.BuildStatTree(locsForPaths, filter, matcher)
rest.WriteResponse(w, r, (*rest.SortedStat)(statTree), true)
})
serverStatic, err := fs.Sub(serverStatic, "server_static")
if err != nil {
panic(err)
}
http.Handle("/", http.FileServer(http.FS(serverStatic)))
socket, err := net.Listen("tcp", "localhost:0") // :0 means random free port
if err != nil {
panic(err)
}
url := fmt.Sprintf("http://%v", socket.Addr())
fmt.Println("Web UI:", url)
fmt.Println("API: ", url+"/api")
go browser.OpenURL(url)
http.Serve(socket, nil)
}