-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
188 lines (158 loc) · 4.31 KB
/
app.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
package main
import (
"context"
"exPL/util"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/sahilm/fuzzy"
)
// type Drive struct{
// name string
// used_gb int
// total_gb int
// letter string
// }
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) ProcessFile()(string) {
return "greetings for wails"
}
func (a *App) OpenFile(path string)() {
filePath := strings.ReplaceAll(path, `\`, `/`)
var cmd *exec.Cmd
cmd = exec.Command("cmd", "/c", "start", "", filePath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func (a *App) OpenDirectory(path string) ([]map[string]string, error){
var allFiles []map[string]string
fmt.Printf("Processing directory: %s\n", path)
// Perform operations on the current directory if needed
// List all files and subdirectories in the current directory
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
// fmt.Println(reflect.TypeOf(entries))
for _, entry := range entries {
fullPath := filepath.Join(path, entry.Name())
fullPath = strings.Replace(fullPath, "\\", "/", -1)
temp := make(map[string]string)
if entry.IsDir() {
temp["Directory"] = fullPath
}else {
temp["File"] = fullPath
}
allFiles = append(allFiles, temp)
}
// jsonData, err := json.Marshal(allFiles)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
// fmt.Println(string(jsonData))
return allFiles, nil
}
func (a *App) GetDisks() ([]map[string]interface{}, error) {
cmd := exec.Command("wmic", "logicaldisk", "get", "deviceid,freespace,size")
output, err := cmd.Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(output), "\n")
diskSpaces := []map[string]interface{}{}
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) == 3 {
used_gb, _ := strconv.ParseFloat(fields[1],64)
total_gb, _:= strconv.ParseFloat(fields[2],64)
diskSpace := map[string]interface{}{
"name": fields[0],
"used_gb": util.BytesToGB(used_gb),
"total_gb":util.BytesToGB(total_gb),
"letter":fields[0][0:1],
}
diskSpaces = append(diskSpaces, diskSpace)
}
}
return diskSpaces, nil
}
type DirectoryChild struct {
Name string
}
func (a *App) Search_Directory(query string, searchDirectory string, extension string, acceptFiles bool, acceptDirectories bool) []DirectoryChild {
var results []DirectoryChild
var fuzzyScores []fuzzy.Match
err := filepath.WalkDir(searchDirectory, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
fileName := d.Name()
if d.IsDir() {
if !acceptDirectories {
return nil
}
matches := fuzzy.Find(query, []string{fileName})
fmt.Printf("Dir: %s, Query: %s, Matches: %v\n", fileName, query, matches)
if len(matches) == 0 || matches[0].Score < 40 {
return nil
}
results = append(results, DirectoryChild{Name: fileName})
fuzzyScores = append(fuzzyScores, matches[0])
} else if acceptFiles {
if len(extension) > 0 && !strings.HasSuffix(fileName, extension) {
return nil
}
// Remove extension from file name.
cleanedFilename := strings.TrimSuffix(fileName, filepath.Ext(fileName))
matches := fuzzy.Find(query, []string{cleanedFilename})
fmt.Printf("File: %s, Query: %s, Matches: %v\n", fileName, query, matches)
if len(matches) == 0 || matches[0].Score < 20 {
return nil
}
results = append(results, DirectoryChild{Name: fileName})
fuzzyScores = append(fuzzyScores, matches[0])
}
return nil
})
if err != nil {
fmt.Println("Error:", err)
}
// Sort by fuzzy score
sort.Slice(fuzzyScores, func(i, j int) bool {
return fuzzyScores[i].Score > fuzzyScores[j].Score
})
// Reorder results based on sorted scores
for _, match := range fuzzyScores {
for i, result := range results {
if result.Name == match.Str {
results[i] = results[len(results)-1]
results = results[:len(results)-1]
break
}
}
}
return results
}