fork/gdu

git clone https://git.julia.blue/fork/gdu

ShivamB25feat: honor block size environment (#604)fe605ec

ignore
4.0 KiB195 linesraw
1// Package common contains commong logic and interfaces used across Gdu
2// nolint: revive //Why: this is common package
3package common
4
5import (
6	"os"
7	"regexp"
8	"strconv"
9)
10
11// UI struct
12type UI struct {
13	Analyzer              Analyzer
14	IgnoreDirPaths        map[string]struct{}
15	IgnoreDirPathPatterns *regexp.Regexp
16	IgnoreHidden          bool
17	IgnoreTypes           []string
18	IncludeTypes          []string
19	UseColors             bool
20	UseSIPrefix           bool
21	ShowProgress          bool
22	ShowApparentSize      bool
23	ShowRelativeSize      bool
24	FilteringFiles        bool
25	blockSize             int64
26	blockSuffix           string
27}
28
29// SetAnalyzer sets analyzer instance
30func (ui *UI) SetAnalyzer(a Analyzer) {
31	ui.Analyzer = a
32}
33
34// SetFollowSymlinks sets whether symlinks to files should be followed
35func (ui *UI) SetFollowSymlinks(v bool) {
36	ui.Analyzer.SetFollowSymlinks(v)
37}
38
39// SetShowAnnexedSize sets whether to use annexed size of git-annex files
40func (ui *UI) SetShowAnnexedSize(v bool) {
41	ui.Analyzer.SetShowAnnexedSize(v)
42}
43
44// SetTimeFilter sets the time filter function for file inclusion
45func (ui *UI) SetTimeFilter(timeFilter TimeFilter) {
46	ui.Analyzer.SetTimeFilter(timeFilter)
47	ui.FilteringFiles = true
48}
49
50// SetArchiveBrowsing sets whether browsing of zip/jar archives is enabled
51func (ui *UI) SetArchiveBrowsing(v bool) {
52	ui.Analyzer.SetArchiveBrowsing(v)
53}
54
55// SetBlockSizeFromEnvironment applies the BLOCK_SIZE or BLOCKSIZE output format.
56func (ui *UI) SetBlockSizeFromEnvironment() {
57	value, ok := os.LookupEnv("BLOCK_SIZE")
58	if !ok {
59		value, ok = os.LookupEnv("BLOCKSIZE")
60	}
61	if !ok {
62		return
63	}
64
65	switch value {
66	case "human-readable":
67		return
68	case "si":
69		ui.UseSIPrefix = true
70		return
71	}
72
73	blockSize, suffix, ok := parseBlockSize(value)
74	if !ok {
75		return
76	}
77	ui.blockSize = blockSize
78	ui.blockSuffix = suffix
79}
80
81// FormatBlockSize formats size as a rounded-up count of configured blocks.
82func (ui *UI) FormatBlockSize(size int64) (string, bool) {
83	if ui.blockSize == 0 {
84		return "", false
85	}
86
87	blocks := size / ui.blockSize
88	if size > 0 && size%ui.blockSize != 0 {
89		blocks++
90	}
91	return strconv.FormatInt(blocks, 10) + ui.blockSuffix, true
92}
93
94func parseBlockSize(value string) (blockSize int64, suffix string, ok bool) {
95	if value == "" {
96		return 0, "", false
97	}
98
99	index := 0
100	for index < len(value) && value[index] >= '0' && value[index] <= '9' {
101		index++
102	}
103	plainUnit := index == 0
104	count := int64(1)
105	if !plainUnit {
106		var err error
107		count, err = strconv.ParseInt(value[:index], 10, 64)
108		if err != nil || count < 1 {
109			return 0, "", false
110		}
111	}
112
113	unit := value[index:]
114	multiplier, valid := blockSizeMultiplier(unit)
115	if !valid || count > int64(^uint64(0)>>1)/multiplier {
116		return 0, "", false
117	}
118	if plainUnit {
119		suffix = unit
120	}
121
122	return count * multiplier, suffix, true
123}
124
125func blockSizeMultiplier(unit string) (int64, bool) {
126	switch unit {
127	case "":
128		return 1, true
129	case "k", "K", "Ki", "KiB":
130		return 1 << 10, true
131	case "kB":
132		return 1e3, true
133	case "M", "Mi", "MiB":
134		return 1 << 20, true
135	case "MB":
136		return 1e6, true
137	case "G", "Gi", "GiB":
138		return 1 << 30, true
139	case "GB":
140		return 1e9, true
141	case "T", "Ti", "TiB":
142		return 1 << 40, true
143	case "TB":
144		return 1e12, true
145	case "P", "Pi", "PiB":
146		return 1 << 50, true
147	case "PB":
148		return 1e15, true
149	case "E", "Ei", "EiB":
150		return 1 << 60, true
151	case "EB":
152		return 1e18, true
153	default:
154		return 0, false
155	}
156}
157
158// binary multiplies prefixes (IEC)
159const (
160	_ float64 = 1 << (10 * iota)
161	Ki
162	Mi
163	Gi
164	Ti
165	Pi
166	Ei
167)
168
169// SI prefixes
170const (
171	K float64 = 1e3
172	M float64 = 1e6
173	G float64 = 1e9
174	T float64 = 1e12
175	P float64 = 1e15
176	E float64 = 1e18
177)
178
179// FormatNumber returns number as a string with thousands separator
180func FormatNumber(n int64) string {
181	in := []byte(strconv.FormatInt(n, 10))
182
183	var out []byte
184	if i := len(in) % 3; i != 0 {
185		if out, in = append(out, in[:i]...), in[i:]; len(in) > 0 {
186			out = append(out, ',')
187		}
188	}
189	for len(in) > 0 {
190		if out, in = append(out, in[:3]...), in[3:]; len(in) > 0 {
191			out = append(out, ',')
192		}
193	}
194	return string(out)
195}