-
Notifications
You must be signed in to change notification settings - Fork 11
/
console.go
91 lines (74 loc) · 2.08 KB
/
console.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
package printer
import (
"cmp"
"fmt"
"path/filepath"
"slices"
"github.com/dustin/go-humanize"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/samber/lo"
"golang.org/x/exp/maps"
"github.com/Zxilly/go-size-analyzer/internal/entity"
"github.com/Zxilly/go-size-analyzer/internal/result"
)
func percentString(f float64) string {
return fmt.Sprintf("%.2f%%", f)
}
type CommonOption struct {
HideSections bool
HideMain bool
HideStd bool
}
func Text(r *result.Result, options *CommonOption) string {
t := table.NewWriter()
allKnownSize := uint64(0)
t.SetTitle("%s", filepath.Base(r.Name))
t.AppendHeader(table.Row{"Percent", "Name", "Size", "Type"})
type sizeEntry struct {
name string
size uint64
typ string
percent string
}
entries := make([]sizeEntry, 0)
pkgs := maps.Values(r.Packages)
for _, p := range pkgs {
if options.HideMain && p.Type == entity.PackageTypeMain {
continue
}
if options.HideStd && p.Type == entity.PackageTypeStd {
continue
}
allKnownSize += p.Size
entries = append(entries, sizeEntry{
name: p.Name,
size: p.Size,
typ: p.Type,
percent: percentString(float64(p.Size) / float64(r.Size) * 100),
})
}
if !options.HideSections {
sections := lo.Filter(r.Sections, func(s *entity.Section, _ int) bool {
return s.Size > s.KnownSize && s.Size != s.KnownSize && !s.OnlyInMemory
})
for _, s := range sections {
unknownSize := s.FileSize - s.KnownSize
allKnownSize += unknownSize
entries = append(entries, sizeEntry{
name: s.Name,
size: unknownSize,
typ: "section",
percent: percentString(float64(unknownSize) / float64(r.Size) * 100),
})
}
}
slices.SortFunc(entries, func(a, b sizeEntry) int {
return -cmp.Compare(a.size, b.size)
})
for _, e := range entries {
t.AppendRow(table.Row{e.percent, e.name, humanize.Bytes(e.size), e.typ})
}
t.AppendFooter(table.Row{percentString(float64(allKnownSize) / float64(r.Size) * 100), "Known", humanize.Bytes(allKnownSize)})
t.AppendFooter(table.Row{"100%", "Total", humanize.Bytes(r.Size)})
return t.Render()
}