Skip to content

Commit a1d3891

Browse files
author
Lord of Scripts
committed
Resolved GH-2
1 parent 38d695b commit a1d3891

File tree

7 files changed

+202
-53
lines changed

7 files changed

+202
-53
lines changed

browsers/chromium/chromium.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,7 @@ func NewChromiumCleaner(profile string, smode cmn.SizeMode, logger ...cmn.ILogge
106106
*-----------------------------------------------------------------*/
107107

108108
func (c *ChromiumCleaner) String() string {
109-
var reportedSize string = ""
110-
switch c.sizeMode {
111-
case cmn.SizeModeSI:
112-
reportedSize = cmn.ByteCountSI(c.cleanedSize)
113-
break
114-
case cmn.SizeModeIEC:
115-
reportedSize = cmn.ByteCountIEC(c.cleanedSize)
116-
break
117-
case cmn.SizeModeStd:
118-
fallthrough
119-
default:
120-
reportedSize = cmn.AddThousands(c.cleanedSize, ',')
121-
break
122-
}
109+
reportedSize := cmn.ReportByteCount(c.cleanedSize, c.sizeMode)
123110

124111
c.logx.Print("Cleaned ", cmn.AddThousands(c.cleanedSize, ','))
125112
c.logx.Print("Cleaned ", cmn.ByteCountSI(c.cleanedSize))
@@ -173,9 +160,16 @@ func (c *ChromiumCleaner) Tell() bool {
173160
ChromiumDataDir, ChromiumCachesDir := GetChromiumDirs()
174161
dataExists := cmn.IsDirectory(ChromiumDataDir)
175162
cacheExists := cmn.IsDirectory(ChromiumCachesDir)
163+
var sizeD, sizeC int64
164+
if dataExists {
165+
sizeD, _ = cmn.GetDirectorySize(ChromiumDataDir)
166+
}
167+
if cacheExists {
168+
sizeC, _ = cmn.GetDirectorySize(ChromiumCachesDir)
169+
}
176170
fmt.Println("Chromium Directories:")
177-
fmt.Println("\tData : ", ChromiumDataDir, dataExists)
178-
fmt.Println("\tCache: ", ChromiumCachesDir, cacheExists)
171+
fmt.Printf("\tData : %5t %s %s\n", dataExists, ChromiumDataDir, cmn.ReportByteCount(sizeD, c.sizeMode))
172+
fmt.Printf("\tCache: %5t %s %s\n", cacheExists, ChromiumCachesDir, cmn.ReportByteCount(sizeC, c.sizeMode))
179173
return dataExists && cacheExists
180174
}
181175

@@ -232,7 +226,7 @@ func (c *ChromiumCleaner) clearCache() error {
232226
}
233227

234228
c.cleanedSize += cacheSize
235-
fmt.Printf("\tDeleted %s bytes from cache\n", cmn.AddThousands(cacheSize, ','))
229+
fmt.Printf("\tDeleted %s bytes from cache\n", cmn.ReportByteCount(cacheSize, c.sizeMode))
236230
c.logx.Print("clearCache DONE")
237231
return nil
238232
}
@@ -249,7 +243,7 @@ func (c *ChromiumCleaner) eraseProfile() error {
249243

250244
// (b) we are going to clean the profile's top level
251245
var filter cmn.IDirCleaner
252-
filter = cmn.NewDirCleaner(c.ProfileRoot)
246+
filter = cmn.NewDirCleaner(c.ProfileRoot, c.sizeMode)
253247

254248
// (c) except these important profile items
255249
err := filter.CleanUp(ProfileExceptions)
@@ -259,7 +253,7 @@ func (c *ChromiumCleaner) eraseProfile() error {
259253
}
260254

261255
c.cleanedSize += filter.CleanedSize()
262-
fmt.Printf("\t...Erased %d bytes\n", c.cleanedSize)
256+
fmt.Printf("\t...Erased %s bytes\n", cmn.ReportByteCount(c.cleanedSize, c.sizeMode))
263257
return nil
264258
}
265259

byte_count.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ import (
1414
* F u n c t i o n s
1515
*-----------------------------------------------------------------*/
1616

17+
// Given a total size return a string with that value formatted in
18+
// either of the size formats (Standard, International, Binary)
19+
func ReportByteCount(count int64, mode SizeMode) string {
20+
var output string
21+
switch mode {
22+
case SizeModeSI:
23+
output = ByteCountSI(count)
24+
break
25+
case SizeModeIEC:
26+
output = ByteCountIEC(count)
27+
break
28+
case SizeModeStd:
29+
fallthrough
30+
default:
31+
output = AddThousands(count, ',')
32+
}
33+
return output
34+
}
35+
1736
// Byte count formatted using International System (1K = 1000)
1837
func ByteCountSI(b int64) string {
1938
const UNIT = 1000

cmd/wiper/wiper.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
FLAG_HELP_ME string = "This help"
3131
FLAG_HELP_CACHE string = "Erase cache only"
3232
FLAG_HELP_PROFILE string = "Erase profile junk only"
33+
FLAG_HELP_SIZE string = "Select size reporting mode (Std, SI, IEC)"
3334
FLAG_HELP_LOG string = "Enable log output"
3435
)
3536

@@ -51,7 +52,8 @@ var (
5152
*-----------------------------------------------------------------*/
5253

5354
type BrowserWipe struct {
54-
cleaner browsers.IBrowsers
55+
cleaner browsers.IBrowsers
56+
SizeMode cmn.SizeMode
5557
}
5658

5759
/* ----------------------------------------------------------------
@@ -72,16 +74,16 @@ func (b *BrowserWipe) Scan() {
7274
}
7375

7476
for _, br := range supportedBrowsers {
75-
b.GetCleaner(br, "")
77+
b.GetCleaner(br, "", b.SizeMode)
7678
b.cleaner.Tell()
7779
installed := b.cleaner.IdentifyAppDataRoot()
7880
fmt.Printf("\tInstalled: %t\n", installed)
7981
}
8082
}
8183

82-
func (b *BrowserWipe) GetCleaner(which browsers.Browser, profile string) error {
84+
func (b *BrowserWipe) GetCleaner(which browsers.Browser, profile string, mode cmn.SizeMode) error {
8385
if which == browsers.ChromiumBrowser {
84-
b.cleaner = chromium.NewChromiumCleaner(profile, cmn.SizeModeIEC, logx)
86+
b.cleaner = chromium.NewChromiumCleaner(profile, mode, logx)
8587
return nil
8688
}
8789

@@ -128,6 +130,7 @@ func help() {
128130
fmt.Printf(HELP_TEMPLATE, "-c", "-cache", "", FLAG_HELP_CACHE)
129131
fmt.Printf(HELP_TEMPLATE, "-p", "-profile", "", FLAG_HELP_PROFILE)
130132
fmt.Printf(HELP_TEMPLATE, "-b", "-browser", "BROWSER", FLAG_HELP_BROWSER)
133+
fmt.Printf(HELP_TEMPLATE, "-z", "-size", "Std", FLAG_HELP_SIZE)
131134
fmt.Printf(HELP_TEMPLATE, "-s", "-scan", "", FLAG_HELP_SCAN)
132135
fmt.Printf(HELP_TEMPLATE, "", "-log", "", FLAG_HELP_LOG)
133136

@@ -155,7 +158,7 @@ func die(exitCode int, msgformat string, v ...any) {
155158
// Usage: wipechromium -p 'Profile 1'
156159
func main() {
157160
// A. Command-line options
158-
var profile, browserName string
161+
var profile, browserName, szmodeS string
159162
var cacheOnly, profileOnly, logging, scanOnly, helpme bool
160163
flag.StringVar(&browserName, "b", browsers.ChromiumBrowser.String(), FLAG_HELP_BROWSER)
161164
flag.StringVar(&browserName, "browser", browsers.ChromiumBrowser.String(), FLAG_HELP_BROWSER)
@@ -169,6 +172,8 @@ func main() {
169172
flag.BoolVar(&cacheOnly, "cache", false, FLAG_HELP_CACHE)
170173
flag.BoolVar(&profileOnly, "p", false, FLAG_HELP_PROFILE)
171174
flag.BoolVar(&profileOnly, "profile", false, FLAG_HELP_PROFILE)
175+
flag.StringVar(&szmodeS, "size", "Std", FLAG_HELP_SIZE)
176+
flag.StringVar(&szmodeS, "z", "Std", FLAG_HELP_SIZE)
172177
flag.BoolVar(&logging, "log", false, FLAG_HELP_LOG)
173178
flag.Parse()
174179

@@ -202,29 +207,47 @@ func main() {
202207
die(2, "Not a supported browser %q", browserName)
203208
}
204209

205-
// (b.5) Conditional Logging
210+
// (b.5) Size reporting mode
211+
var sizeMode cmn.SizeMode
212+
switch strings.ToLower(szmodeS) {
213+
case "si":
214+
sizeMode = cmn.SizeModeSI
215+
break
216+
case "iec":
217+
sizeMode = cmn.SizeModeIEC
218+
break
219+
case "std":
220+
sizeMode = cmn.SizeModeStd
221+
break
222+
default:
223+
die(3, "Invalid size mode (SI|IEC|STD) %q", szmodeS)
224+
}
225+
226+
// (b.6) Conditional Logging
206227
logx = cmn.NewConditionalLogger(logging, "Main")
207228

208-
// (b.6) Prologue
229+
// (b.7) Prologue
209230
if !scanOnly {
210231
fmt.Printf("Browser name : %s\n", browser)
211232
fmt.Printf("Profile name : %s\n", profile)
212233
fmt.Printf("Erase cache : %t\n", cacheOnly)
213234
fmt.Printf("Erase profile : %t\n", profileOnly)
235+
fmt.Printf("Size mode : %s\n", sizeMode)
214236
fmt.Printf("Logging enable: %t\n", logging)
215237
}
216238

217239
// C. Execute
218240
runner := &BrowserWipe{}
241+
runner.SizeMode = sizeMode
219242
if scanOnly {
220243
runner.Scan()
221244
} else {
222-
if err := runner.GetCleaner(browser, profile); err == nil {
245+
if err := runner.GetCleaner(browser, profile, sizeMode); err == nil {
223246
if code, err := runner.Run(cacheOnly, profileOnly); err != nil {
224247
die(code, err.Error())
225248
}
226249
} else {
227-
die(3, err.Error())
250+
die(4, err.Error())
228251
}
229252
}
230253

dircleaner.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ import (
1717
* G l o b a l s
1818
*-----------------------------------------------------------------*/
1919

20-
const (
21-
SizeModeStd SizeMode = iota // full numeric size
22-
SizeModeSI // Sistema Internacional: 1KB = 1000
23-
SizeModeIEC // Binary System: 1KB = 1024
24-
)
25-
2620
var _ IDirCleaner = (*DirCleaner)(nil)
2721

2822
/* ----------------------------------------------------------------
@@ -44,48 +38,35 @@ type DirCleaner struct {
4438
cleanedSize int64
4539
removedQty int
4640
skippedQty int
41+
sizeMode SizeMode
4742
logx ILogger
4843
}
4944

50-
type SizeMode uint
51-
5245
/* ----------------------------------------------------------------
5346
* C o n s t r u c t o r s
5447
*-----------------------------------------------------------------*/
5548

56-
func NewDirCleaner(root string, logger ...ILogger) *DirCleaner {
49+
func NewDirCleaner(root string, sizing SizeMode, logger ...ILogger) *DirCleaner {
5750
const cName = "DirCleaner"
5851
var logCtx ILogger
5952
if len(logger) == 0 {
6053
logCtx = NewConditionalLogger(false, cName)
6154
} else {
6255
logCtx = logger[0].InheritAs(cName)
6356
}
64-
return &DirCleaner{root, 0, 0, 0, logCtx}
57+
return &DirCleaner{root, 0, 0, 0, sizing, logCtx}
6558
}
6659

6760
/* ----------------------------------------------------------------
6861
* M e t h o d s
6962
*-----------------------------------------------------------------*/
7063

71-
func (s SizeMode) String() string {
72-
switch s {
73-
case SizeModeStd:
74-
return "Standard"
75-
case SizeModeSI:
76-
return "International"
77-
case SizeModeIEC:
78-
return "Binary"
79-
default:
80-
panic("Unknown size mode")
81-
}
82-
}
83-
64+
// Stringer interface
8465
func (d *DirCleaner) String() string {
8566
return fmt.Sprintf("DirCleaner %q del:%d skip:%d size:%s", d.Root,
8667
d.removedQty,
8768
d.skippedQty,
88-
AddThousands(d.cleanedSize, ','))
69+
ReportByteCount(d.cleanedSize, d.sizeMode))
8970
}
9071

9172
func (d *DirCleaner) CleanUp(exceptions []string) error {

sizemode.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* -----------------------------------------------------------------
2+
* L o r d O f S c r i p t s (tm)
3+
* Copyright (C)2024 Dídimo Grimaldo T.
4+
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5+
* Size Reporting Mode enumeration
6+
*-----------------------------------------------------------------*/
7+
package wipechromium
8+
9+
import (
10+
"errors"
11+
)
12+
13+
/* ----------------------------------------------------------------
14+
* G l o b a l s
15+
*-----------------------------------------------------------------*/
16+
17+
const (
18+
SizeModeStd SizeMode = iota // full numeric size
19+
SizeModeSI // Sistema Internacional: 1KB = 1000
20+
SizeModeIEC // Binary System: 1KB = 1024
21+
)
22+
23+
var (
24+
ErrUnknownSizeMode = errors.New("Unknown size mode (Std|SI|IEC")
25+
)
26+
27+
/* ----------------------------------------------------------------
28+
* T y p e s
29+
*-----------------------------------------------------------------*/
30+
31+
type SizeMode uint
32+
33+
/* ----------------------------------------------------------------
34+
* C o n s t r u c t o r s
35+
*-----------------------------------------------------------------*/
36+
37+
/* ----------------------------------------------------------------
38+
* M e t h o d s
39+
*-----------------------------------------------------------------*/
40+
41+
// Stringer interface
42+
func (s SizeMode) String() string {
43+
switch s {
44+
case SizeModeStd:
45+
return "Standard"
46+
case SizeModeSI:
47+
return "International"
48+
case SizeModeIEC:
49+
return "Binary"
50+
default:
51+
panic(ErrUnknownSizeMode)
52+
}
53+
}
54+
55+
func (s SizeMode) ShortString() string {
56+
switch s {
57+
case SizeModeStd:
58+
return "Std"
59+
case SizeModeSI:
60+
return "SI"
61+
case SizeModeIEC:
62+
return "IEC"
63+
default:
64+
panic(ErrUnknownSizeMode)
65+
}
66+
}

0 commit comments

Comments
 (0)