-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
178 lines (150 loc) · 4.27 KB
/
report.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
package main
import (
"bytes"
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/gookit/color"
"golang.org/x/crypto/ssh/terminal"
)
//go:embed styles.css
var css string
func minimalColorise(langColor string) color.RGBColor {
return color.HEX(langColor)
}
func generateBar(summaries []Language, width int) {
innerWidth := width
totalLines := 0
totalLines = SumLines(summaries)
if totalLines == 0 {
fmt.Fprintf(os.Stderr, " no code found in %s\n", "/path/to/dir")
return
}
filled := 0
for _, summary := range summaries {
percent := (summary.TotalCount * innerWidth) / totalLines
if percent == 0 {
continue
}
if filled == 0 {
fmt.Println()
fmt.Print(" ")
}
filled += percent
if strings.HasPrefix(summary.getColor(), "#") {
col := color.HEX(summary.getColor(), true)
col.Print(strings.Repeat(" ", percent))
}
}
if filled != 0 {
fmt.Print(strings.Repeat(" ", innerWidth-filled))
fmt.Println()
fmt.Println()
}
}
func Display(langs []Language, opts Option) {
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
width = 80
}
fmt.Println()
for _, lang := range langs {
space := width - (len(lang.Name) + 4) - len(fmt.Sprintf("%d", lang.TotalCount))
inlay := color.HEX("#a1a1a1").Sprint(strings.Repeat(".", space-2))
format := map[bool]string{true: " %s %s%s %d", false: " %s %s%s %d\n"}[opts.blame]
fmt.Printf(format, minimalColorise(lang.getColor()).Sprint("●"), lang.Name, inlay, lang.TotalCount)
if opts.blame {
for i, file := range lang.Files {
graphChar := "└"
if i < len(lang.Files)-1 {
graphChar = "├"
}
fmt.Printf("\n %s %s", graphChar, file.Name())
if i == len(lang.Files)-1 {
fmt.Println()
}
}
}
}
generateBar(langs, width)
}
func HtmlDisplay(root string, langs []Language, opts Option) {
var output bytes.Buffer
openedInBrowser := false
root = map[bool]string{true: root, false: filepath.Base(root)}[opts.html]
output.WriteString("<!doctype html>\n")
output.WriteString(fmt.Sprintf("<html>\n<head>\n<title>Tally - %s</title>\n<style>\n%s</style>\n</head>\n \n", root, css))
output.WriteString("<body>\n\n")
var totalLines int
for _, lang := range langs {
totalLines += lang.TotalCount
}
var remainingLines = totalLines
output.WriteString("<div aria-hidden class='bar'>")
for _, lang := range langs {
remainingLines -= lang.TotalCount
output.WriteString(fmt.Sprintf("<div aria-hidden title=%s style=\"background-color:%s; flex-grow: %d\"></div>", lang.Name, lang.getColor(), lang.TotalCount))
}
if remainingLines > 0 {
output.WriteString(fmt.Sprintf("<div aria-hidden title=\"Other languages\" style=\"background-color: gray; flex-grow: %d\"></div>", remainingLines))
}
output.WriteString("</div>")
output.WriteString("<table>\n<colgroup><col /><col width=\"15%\" /><col width=\"15%\" /></colgroup>\n <th>Language</th><th>Lines</th><th>File Count</th>")
for _, lang := range langs {
output.WriteString(fmt.Sprintf("<tr><td><span style=\"color: %s\">●</span> %s</td><td>%d</td><td>%d</td></tr>",
lang.getColor(), lang.Name, lang.TotalCount, lang.FileCount))
}
output.WriteString("</table>\n</body>\n</html>")
tempFile := temp(output.String())
if tempFile == nil {
return
}
if err := openBrowser(tempFile.Name()); err != nil {
fmt.Println("Error opening browser:", err)
defer os.Remove(tempFile.Name())
} else {
openedInBrowser = true
}
if openedInBrowser {
defer func() {
time.Sleep(10 * time.Second)
os.Remove(tempFile.Name())
fmt.Println("Deleted temporary file:", tempFile.Name())
}()
}
}
func temp(in string) *os.File {
tempFile, err := os.CreateTemp("", "tally-*.html")
if err != nil {
fmt.Println("Error creating temporary file:", err)
return nil
}
defer tempFile.Close()
_, err = tempFile.WriteString(in)
if err != nil {
fmt.Println("Error writing to temporary file:", err)
return nil
}
return tempFile
}
func openBrowser(filePath string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "rundll32"
args = []string{"url.dll,FileProtocolHandler", filePath}
case "darwin":
cmd = "open"
args = []string{filePath}
default:
cmd = "xdg-open"
args = []string{filePath}
}
return exec.Command(cmd, args...).Start()
}