-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (153 loc) · 4.29 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
package main
import (
walkMain "github.com/lxn/walk"
walk "github.com/lxn/walk/declarative"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"time"
"github.com/ncruces/zenity"
)
var (
globalFiles []string
)
func writeCrashLog(err string) {
currDir, _ := os.Getwd()
path := path.Join(currDir, fmt.Sprintf("costCalculatorCrash%v.txt", time.Now().Unix()))
content := fmt.Sprintf("Crash Log: %v\n%v", time.Now().Format(time.RFC850), err)
os.WriteFile(path, []byte(content), 0644)
}
func panicAndLog(err string) {
writeCrashLog(err)
panic(err)
}
func fatalDialog(err string) {
zenity.Error(fmt.Sprintf("%v", err), zenity.Title("Fatal Error"))
}
func fatalDialogAndLog(err string) {
fatalDialog(err)
panicAndLog(err)
}
// scanFolder returns a slice of all valid file paths in the given folder
func scanFolder(path string) ([]string, error) {
var validPaths []string
pattern := regexp.MustCompile(`[ _A-Za-z0-9]+_[0-9]*[.0-9]+[a-z]{3}`)
var walkFunc = func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if pattern.MatchString(info.Name()) { // add only if valid file name
//fmt.Printf("Matched: %s\n", info.Name())
if !info.IsDir() { // do not add directory
validPaths = append(validPaths, path)
}
}
return nil
}
err := filepath.Walk(path, walkFunc)
if err != nil {
return nil, err
}
return validPaths, nil
}
// calculate total of all names given in the array
func costTotal(paths *[]string, textbox *walkMain.TextEdit) float64 {
total := 0.0
textbox.SetText("")
for _, filename := range *paths {
cost,err := calcCost(filename, "_")
if err != nil {
fatalDialogAndLog(err.Error())
}
total += cost
_, name := filepath.Split(filename)
textbox.AppendText(fmt.Sprintf("%s **Cost: %.2f**\r\n", name, cost))
}
return total
}
// calcCost returns the cost of a single name
func calcCost(filename string, sep string) (float64,error) {
filename = strings.TrimRight(filename, filepath.Ext(filename))
split := strings.Split(filename, sep)
end := split[len(split)-1]
cost, err := strconv.ParseFloat(end, 64)
if err != nil {
return 0.0, fmt.Errorf("calcCost>strconv.ParseFloat: %v", err)
}
return cost,nil
}
func openFolderDialog(textBox *walkMain.TextEdit) {
selectedFolder, err := zenity.SelectFile(
zenity.Title("Select Folder"),
zenity.Directory())
if err == zenity.ErrCanceled { // cancel then return
return
} else if err != nil { // any other error crash
panicAndLog("openFolderDialog>zenitySelectFile" + err.Error())
}
err = zenity.Question(fmt.Sprintf("Selected Folder: %s", selectedFolder), zenity.Title("Confirm"))
if err == zenity.ErrCanceled { // also cancel if wrong folder selected
return
} else if err != nil {
panicAndLog("openFolderDialog>zenityQuestion" + err.Error())
}
files,err := scanFolder(selectedFolder)
if err != nil {
panicAndLog(fmt.Sprint("openFolderDialog>scanFolder: %v", err))
}
textBox.SetText("")
for _, file := range files {
if !slices.Contains(globalFiles, file) {
globalFiles = append(globalFiles, file) // add any new files
}
}
for _, file := range globalFiles {
textBox.AppendText(file + "\r\n")
}
}
func main() {
var inputFileBox *walkMain.TextEdit
defer func() {
if err := recover(); err != nil {
panicAndLog(fmt.Sprintf("main>panic: %v", err))
}
}()
walk.MainWindow{
Title: "Cost Calculator",
MinSize: walk.Size{Width: 600, Height: 400},
Size: walk.Size{Width: 640, Height: 360},
Layout: walk.VBox{},
Children: []walk.Widget{walk.TextLabel{Text: "Input Files"},
walk.HSplitter{
Children: []walk.Widget{
walk.TextEdit{AssignTo: &inputFileBox, ReadOnly: true, Text: "Use add folder button to add files", Font: walk.Font{PointSize: 10}},
},
},
walk.HSplitter{Children: []walk.Widget{
walk.PushButton{
Text: "Add Folder",
OnClicked: func() {
go openFolderDialog(inputFileBox)
},
},
walk.PushButton{
Text: "Calculate",
OnClicked: func() {
cost := costTotal(&globalFiles, inputFileBox)
zenity.Info(fmt.Sprintf("Total Cost: %.2f", cost), zenity.Title("Total Cost"))
},
}, walk.PushButton{
Text: "Clear files",
OnClicked: func() {
inputFileBox.SetText("Use select folder button to add files")
globalFiles = []string{}
}},
}},
},
}.Run()
}