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