-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
318 lines (261 loc) · 7.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"fmt"
"gocleasy/files"
"gocleasy/guiutils"
"gocleasy/ignore"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"gioui.org/app"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
"gioui.org/widget"
"golang.design/x/clipboard"
)
var filesFromDirsBeingLoaded = make(chan string, 10) // To send files being scanned inside a directory that has been clicked to be expanded
func calculateDirSize(basepath string) (int64, int64, error) {
var size int64 = 0
var numchildren int64 = 0
err := filepath.WalkDir(basepath, func(path string, dire os.DirEntry, err error) error {
// Get the size if not a directory
fileinfo, err := os.Stat(path)
if err == nil {
size += fileinfo.Size()
numchildren++
}
// Continue even if you cannot read one specific file
return nil
})
numchildren--
return size, numchildren, err
}
func DeleteFiles(selected_files []*files.File) (int64, int64) {
var errslice []error
var err error
var numfiles, sizeliberated int64
// Loop over selected files and delete them
for _, file := range selected_files {
if file.IsDir {
numfiles += file.NumChildren
} else {
numfiles++
}
sizeliberated += file.Size
log.Print("WARNING: If you are testing, you may want to comment the following lines")
err = os.RemoveAll(file.FullPath)
if err != nil {
errslice = append(errslice, err)
}
}
for _, er1 := range errslice {
// If there is any error with any file/folder you can handle it here
log.Print(er1)
}
return numfiles, sizeliberated
}
func getRootPath() string {
switch runtime.GOOS {
case "windows":
return getWindowsRootPath()
case "darwin":
// macOS
return "/"
case "android", "linux":
// For Android apps you will need to request permission for reading from external folders.
// I was not able to perform that with gioui or golang, maybe you need to create a connector for JAVA
return "/"
case "ios":
// iOS apps are sandboxed too, so the root path will not be directly accessible
return getIOSRootPath()
default:
return "Unknown OS"
}
}
func getWindowsRootPath() string {
// On Windows, the root path is typically the drive where the OS is installed,
// so we need to get the current drive and concatenate it with the path separator.
return filepath.VolumeName(os.Getenv("SystemDrive")) + string(filepath.Separator)
}
func getIOSRootPath() string {
// In iOS, the application's root path is restricted, but you can use other directories like the Documents directory.
// This is just an example of how you could handle it, but it's not the actual root path.
documentsDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
return filepath.Join(documentsDir, "Documents")
}
// Deletes a string from a slice if exists
func deleteStringFromSlice(str string, slice []string) []string {
// Find and remove the string from the slide
for i, v := range slice {
if v == str {
slice = append(slice[:i], slice[i+1:]...)
break
}
}
return slice
}
func printTree(file *files.File, indent string) {
fmt.Printf("%s%s\n", indent, file.FullPath)
for _, subFile := range file.Files {
printTree(subFile, indent+"\t")
}
}
// func getSelectedFiles(children []*files.File, selfiles *[]*files.File) []*files.File {
//
// // create a pointer to every file
// var filep *files.File
//
// for index := range children {
// filep = children[index]
//
// // If it is selected add to selfiles
// if filep.IsSelected.Value {
// *selfiles = append(*selfiles, filep)
// }
//
// if (filep.IsDir) && // It is a directory
// ((filep.Files != nil) && (len(filep.Files) > 0)) && // It contains files inside
// !filep.IsSelected.Value { // It is not a selected folder
//
// *selfiles = getSelectedFiles(filep.Files, selfiles) // Look for selected files inside
// }
// }
//
// return *selfiles
// }
func copyFilesInClipboard(selfiles []*files.File) {
var result string = ""
for _, file := range selfiles {
result += "\"" + file.FullPath + "\" "
}
clipboard.Write(clipboard.FmtText, []byte(result))
}
func Run(win *app.Window) error {
var applogic *guiutils.AppLogic = guiutils.NewAppLogic()
// ops are the operations from the UI
var ops op.Ops
// Widget declarations
var scanButton widget.Clickable
var deleteButton widget.Clickable
var comeBackButton widget.Clickable
var copy2clipboard widget.Clickable
var nextButton widget.Clickable
var initialPathInput widget.Editor
var filelist widget.List = widget.List{
List: layout.List{
Axis: layout.Vertical,
},
}
var filedeletelist widget.List = widget.List{
List: layout.List{
Axis: layout.Vertical,
},
}
var scanfilesLoadingChann chan int = make(chan int) // Used to transmit how many files have been read
var numfilesdeleted int64 = 0
var sizeliberated int64 = 0
var initialpath string
var totalFilesReadShow int = 0 // Used to maintain a count of the files read
// Initialize clipboard so you can set the clipboard
err := clipboard.Init()
if err != nil {
panic(err)
}
// Listen for events in the window
for {
e := <-win.Events()
log.Print(e)
switch e := e.(type) {
// Window closed
case system.DestroyEvent:
return e.Err
// Actions in the window apart from closing
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
//
// ACTIONS TO CHANGE THE STATE OF THE APPLICATION ***
//
// Goes from homePage to selection of files
if scanButton.Clicked() {
// reset file directory
applogic.Files = nil
initialpath = initialPathInput.Text()
if initialpath == "" {
initialpath = getRootPath()
}
// Test the introduced path, if not good, use the root path
_, err := os.ReadDir(initialpath)
if err != nil {
initialPathInput.SetText("The path: \"%s\" does not exists or cannot be read; Introduce another path or leave blank for root")
} else {
// If there is no problem, continue
applogic.Appstate = guiutils.LoadingFilesS
go applogic.ReportProgress(win, &totalFilesReadShow, scanfilesLoadingChann)
go func() {
applogic.Files = files.WalkFolder(initialpath, ioutil.ReadDir, ignore.IgnoreBasedOnIgnoreFile(ignore.ReadIgnoreFile()), scanfilesLoadingChann)
// Add first level of files to be shown
applogic.FillFirstLayer2Show()
}()
applogic.ShowLoadingPage(gtx, totalFilesReadShow)
}
}
// Go to confirm deleting the files
if nextButton.Clicked() {
// applogic.Selfiles = getSelectedFiles(applogic.Files.Files, &applogic.Selfiles)
applogic.Appstate = guiutils.DelFilesS
}
// Go back to selecting the files
if comeBackButton.Clicked() {
applogic.Appstate = guiutils.SelFilesS
}
// copy files in clipboard
if copy2clipboard.Clicked() {
copyFilesInClipboard(applogic.Selfiles)
}
// Delete the files show a message of number of files deleted and amount of memory freed
if deleteButton.Clicked() {
numfilesdeleted, sizeliberated = DeleteFiles(applogic.Selfiles)
applogic.Appstate = guiutils.HomeS
}
// ACTIONS TO CHANGE THE STATE OF THE APPLICATION ***
//
// STATES OF THE APPLICATION ***
// What template to render based on applogic state
//
switch applogic.Appstate {
case guiutils.HomeS:
applogic.HomePage(gtx, &scanButton, &initialPathInput, numfilesdeleted, sizeliberated)
case guiutils.LoadingFilesS:
applogic.ShowLoadingPage(gtx, totalFilesReadShow)
case guiutils.SelFilesS:
applogic.ShowFiles(gtx, &nextButton, &filelist)
case guiutils.DelFilesS:
applogic.ShowDeletingPage(gtx, &comeBackButton, ©2clipboard, &deleteButton, &filedeletelist)
}
// STATES OF THE APPLICATION ***
e.Frame(gtx.Ops)
}
}
}
func main() {
go func() {
// create window
w := app.NewWindow(
app.Title("Gocleasy"),
app.Size(unit.Dp(550), unit.Dp(550)),
)
// Run main loop
if err := Run(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}