fork/gdu

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

Julia RyanMake -x respect subvolumes4d478cf

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