-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathformats.go
197 lines (160 loc) Β· 7.04 KB
/
formats.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/rfyiamcool/pgcacher/pkg/pcstats"
)
type PcStatusList []pcstats.PcStatus
func (a PcStatusList) Len() int {
return len(a)
}
func (a PcStatusList) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a PcStatusList) Less(i, j int) bool {
return a[j].Cached < a[i].Cached
}
func (stats PcStatusList) FormatUnicode() {
maxName := stats.maxNameLen()
// create horizontal grid line
pad := strings.Repeat("β", maxName+2)
top := fmt.Sprintf("β%sβ¬βββββββββββββββββ¬ββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββ¬ββββββββββ", pad)
hr := fmt.Sprintf("β%sβΌβββββββββββββββββΌββββββββββββββΌβββββββββββββββββΌββββββββββββββΌββββββββββ€", pad)
bot := fmt.Sprintf("β%sβ΄βββββββββββββββββ΄ββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββ΄ββββββββββ", pad)
var size_sum, page_sum, cached_page_sum, cached_size, cached_size_sum int64
fmt.Println(top)
// -nohdr may be chosen to save 2 lines of precious vertical space
pad = strings.Repeat(" ", maxName-4)
fmt.Printf("β Name%s β Size β Pages β Cached Size β Cached Pagesβ Percent β\n", pad)
fmt.Println(hr)
for _, pcs := range stats {
pad = strings.Repeat(" ", maxName-len(pcs.Name))
// The cache is counted through the pageοΌcan't count accurately.
// Here cached file size is calculated by pcs.Size and pcs.Percent,
// so it is not completely accurate, but it has reference value.
cached_size = int64(float64(pcs.Size) * pcs.Percent / 100)
// %-7.3f was chosen to make it easy to scan the percentages vertically
// I tried a few different formats only this one kept the decimals aligned
fmt.Printf("β %s%s β %-15sβ %-12dβ %-15sβ %-12dβ %-7.3f β\n",
pcs.Name, pad, ConvertUnit(pcs.Size), pcs.Pages, ConvertUnit(cached_size), pcs.Cached, pcs.Percent)
size_sum += pcs.Size
page_sum += int64(pcs.Pages)
cached_page_sum += int64(pcs.Cached)
cached_size_sum += cached_size
}
fmt.Println(hr)
pad = strings.Repeat(" ", maxName-len("Sum"))
fmt.Printf("β %s%s β %-15sβ %-12dβ %-15sβ %-12dβ %-7.3f β\n",
"Sum", pad, ConvertUnit(size_sum), page_sum, ConvertUnit(cached_size_sum), cached_page_sum, (float64(cached_page_sum)/float64(page_sum))*100.00)
fmt.Println(bot)
}
func (stats PcStatusList) FormatText() {
maxName := stats.maxNameLen()
// create horizontal grid line
pad := strings.Repeat("-", maxName+2)
top := fmt.Sprintf("+%s+----------------+-------------+----------------+-------------+---------+", pad)
hr := fmt.Sprintf("|%s+----------------+-------------+----------------+-------------+---------|", pad)
bot := fmt.Sprintf("+%s+----------------+-------------+----------------+-------------+---------+", pad)
var size_sum, page_sum, cached_page_sum, cached_size, cached_size_sum int64
fmt.Println(top)
// -nohdr may be chosen to save 2 lines of precious vertical space
pad = strings.Repeat(" ", maxName-4)
fmt.Printf("| Name%s | Size β Pages β Cached Size β Cached Pagesβ Percent β\n", pad)
fmt.Println(hr)
for _, pcs := range stats {
pad = strings.Repeat(" ", maxName-len(pcs.Name))
cached_size = int64(float64(pcs.Size) * pcs.Percent / 100)
// %-7.3f was chosen to make it easy to scan the percentages vertically
// I tried a few different formats only this one kept the decimals aligned
fmt.Printf("| %s%s | %-15s| %-12d| %-15s| %-12d| %-7.3f |\n",
pcs.Name, pad, ConvertUnit(pcs.Size), pcs.Pages, ConvertUnit(cached_size), pcs.Cached, pcs.Percent)
size_sum += pcs.Size
page_sum += int64(pcs.Pages)
cached_page_sum += int64(pcs.Cached)
cached_size_sum += cached_size
}
fmt.Println(hr)
pad = strings.Repeat(" ", maxName-len("Sum"))
fmt.Printf("β %s%s β %-15sβ %-12dβ %-15sβ %-12dβ %-7.3f β\n",
"Sum", pad, ConvertUnit(size_sum), page_sum, ConvertUnit(cached_size_sum), cached_page_sum, (float64(cached_page_sum)/float64(page_sum))*100.00)
fmt.Println(bot)
}
func (stats PcStatusList) FormatPlain() {
maxName := stats.maxNameLen()
var size_sum, page_sum, cached_page_sum, cached_size, cached_size_sum int64
// -nohdr may be chosen to save 2 lines of precious vertical space
pad := strings.Repeat(" ", maxName-4)
fmt.Printf("Name%s Size Pages Cached Size Cached Pages Percent\n", pad)
for _, pcs := range stats {
pad := strings.Repeat(" ", maxName-len(pcs.Name))
cached_size = int64(float64(pcs.Size) * pcs.Percent / 100)
// %-7.3f was chosen to make it easy to scan the percentages vertically
// I tried a few different formats only this one kept the decimals aligned
fmt.Printf("%s%s %-15s %-12d %-15s %-12d %-7.3f\n",
pcs.Name, pad, ConvertUnit(pcs.Size), pcs.Pages, ConvertUnit(cached_size), pcs.Cached, pcs.Percent)
size_sum += pcs.Size
page_sum += int64(pcs.Pages)
cached_page_sum += int64(pcs.Cached)
cached_size_sum += cached_size
}
pad = strings.Repeat(" ", maxName-len("Sum"))
fmt.Printf("%s%s %-15s %-12d %-15s %-12d %-7.3f\n",
"Sum", pad, ConvertUnit(size_sum), page_sum, ConvertUnit(cached_size_sum), cached_page_sum, (float64(cached_page_sum)/float64(page_sum))*100.00)
}
func (stats PcStatusList) FormatTerse() {
fmt.Println("name,size,timestamp,mtime,pages,cached,percent")
for _, pcs := range stats {
time := pcs.Timestamp.Unix()
mtime := pcs.Mtime.Unix()
fmt.Printf("%s,%d,%d,%d,%d,%d,%g\n",
pcs.Name, pcs.Size, time, mtime, pcs.Pages, pcs.Cached, pcs.Percent)
}
}
func (stats PcStatusList) FormatJson() {
b, err := json.Marshal(stats)
if err != nil {
log.Fatalf("JSON formatting failed: %s\n", err)
}
os.Stdout.Write(b)
fmt.Println("")
}
// maxNameLen returns the len of longest filename in the stat list
// if the bnameFlag is set, this will return the max basename len
func (stats PcStatusList) maxNameLen() int {
var maxName int
for _, pcs := range stats {
if len(pcs.Name) > maxName {
maxName = len(pcs.Name)
}
}
if maxName < 5 {
maxName = 5
}
return maxName
}
// define some const unit
// convert origin size data to a friendly readable string.
func ConvertUnit(byteSize int64) string {
const KB int64 = 1024
const MB int64 = 1024 * KB
const GB int64 = 1024 * MB
const TB int64 = 1024 * GB
const PB int64 = 1024 * TB
switch {
case byteSize >= PB:
return fmt.Sprintf("%.3fP", (float64(byteSize) / float64(PB)))
case byteSize >= TB:
return fmt.Sprintf("%.3fT", (float64(byteSize) / float64(TB)))
case byteSize >= GB:
return fmt.Sprintf("%.3fG", (float64(byteSize) / float64(GB)))
case byteSize >= MB:
return fmt.Sprintf("%.3fM", (float64(byteSize) / float64(MB)))
case byteSize >= KB:
return fmt.Sprintf("%.3fK", (float64(byteSize) / float64(KB)))
default:
return fmt.Sprintf("%dB", byteSize)
}
}