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