Skip to content

Commit 94710cc

Browse files
committed
improve ux, fix deprecation warnings
1 parent 9566b1a commit 94710cc

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

main.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"regexp"
@@ -36,6 +35,7 @@ func main() {
3635
// Log box
3736
logBox := widget.NewMultiLineEntry()
3837
logBox.Disable()
38+
logScroll := container.NewVScroll(logBox)
3939

4040
// Games list
4141
gamesList := widget.NewList(
@@ -46,15 +46,18 @@ func main() {
4646
return len(selectedLibrary.Games)
4747
},
4848
func() fyne.CanvasObject {
49-
return container.NewHBox(widget.NewLabel("Game"), widget.NewLabel("Auto-update"))
49+
return widget.NewLabel("Game Name - Update Behavior")
5050
},
5151
func(id widget.ListItemID, item fyne.CanvasObject) {
5252
if selectedLibrary == nil {
5353
return
5454
}
5555
game := selectedLibrary.Games[id]
56-
item.(*fyne.Container).Objects[0].(*widget.Label).SetText(game.Name)
57-
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(game.AutoUpdateBehavior)
56+
updateBehavior := "Auto Update"
57+
if game.AutoUpdateBehavior == "1" {
58+
updateBehavior = "Update On Launch"
59+
}
60+
item.(*widget.Label).SetText(fmt.Sprintf("%s - %s", game.Name, updateBehavior))
5861
},
5962
)
6063

@@ -72,6 +75,11 @@ func main() {
7275
},
7376
)
7477

78+
// Automatically select the first library
79+
if len(libraries) > 0 {
80+
librarySelect.SetSelected(libraries[0].Path)
81+
}
82+
7583
// Update behavior selection
7684
updateBehavior := widget.NewSelect(
7785
[]string{"0 - Always keep this game updated", "1 - Only update this game when I launch it"},
@@ -85,7 +93,7 @@ func main() {
8593
return
8694
}
8795
go func() {
88-
updatedGames := updateLibrary(selectedLibrary, updateBehavior.Selected[:1], logBox)
96+
updatedGames := updateLibrary(selectedLibrary, updateBehavior.Selected[:1], logBox, logScroll)
8997
selectedLibrary.Games = updatedGames
9098
for i, lib := range libraries {
9199
if lib.Path == selectedLibrary.Path {
@@ -107,15 +115,14 @@ func main() {
107115
updateBehavior,
108116
updateButton,
109117
widget.NewLabel("Operation Log:"),
110-
container.NewVScroll(logBox),
118+
logScroll,
111119
)
112120

113121
// Set minimum sizes for scrollable areas
114122
gamesListScroll := content.Objects[3].(*container.Scroll)
115123
gamesListScroll.SetMinSize(fyne.NewSize(500, 200))
116124

117-
logBoxScroll := content.Objects[8].(*container.Scroll)
118-
logBoxScroll.SetMinSize(fyne.NewSize(500, 100))
125+
logScroll.SetMinSize(fyne.NewSize(500, 100))
119126

120127
w.SetContent(content)
121128
w.Resize(fyne.NewSize(600, 600))
@@ -154,12 +161,17 @@ func detectSteamLibraries() []SteamLibrary {
154161
}
155162

156163
func parseLibraryFoldersVDF(path string) []string {
157-
libraries := []string{}
164+
var libraries []string
158165
file, err := os.Open(path)
159166
if err != nil {
160167
return libraries
161168
}
162-
defer file.Close()
169+
defer func(file *os.File) {
170+
err := file.Close()
171+
if err != nil {
172+
fmt.Printf(err.Error())
173+
}
174+
}(file)
163175

164176
scanner := bufio.NewScanner(file)
165177
pathRegex := regexp.MustCompile(`^\s*"path"\s*"(.+)"`)
@@ -177,7 +189,7 @@ func parseLibraryFoldersVDF(path string) []string {
177189
func detectGames(libraryPath string) []Game {
178190
var games []Game
179191
steamappsPath := filepath.Join(libraryPath, "steamapps")
180-
files, err := ioutil.ReadDir(steamappsPath)
192+
files, err := os.ReadDir(steamappsPath)
181193
if err != nil {
182194
fmt.Println("Error reading steamapps directory:", err)
183195
return games
@@ -198,7 +210,7 @@ func detectGames(libraryPath string) []Game {
198210
}
199211

200212
func parseACF(filePath string) *Game {
201-
content, err := ioutil.ReadFile(filePath)
213+
content, err := os.ReadFile(filePath)
202214
if err != nil {
203215
fmt.Println("Error reading ACF file:", err)
204216
return nil
@@ -235,7 +247,7 @@ func parseACF(filePath string) *Game {
235247
return game
236248
}
237249

238-
func updateLibrary(lib *SteamLibrary, newBehavior string, logBox *widget.Entry) []Game {
250+
func updateLibrary(lib *SteamLibrary, newBehavior string, logBox *widget.Entry, logScroll *container.Scroll) []Game {
239251
steamappsPath := filepath.Join(lib.Path, "steamapps")
240252
var wg sync.WaitGroup
241253
updatedGames := make([]Game, len(lib.Games))
@@ -248,36 +260,36 @@ func updateLibrary(lib *SteamLibrary, newBehavior string, logBox *widget.Entry)
248260
// Find the correct appmanifest file
249261
files, err := filepath.Glob(filepath.Join(steamappsPath, "appmanifest_*.acf"))
250262
if err != nil {
251-
logBox.SetText(logBox.Text + fmt.Sprintf("Error searching for %s: %v\n", game.Name, err))
263+
appendToLog(logBox, logScroll, fmt.Sprintf("Error searching for %s: %v\n", game.Name, err))
252264
return
253265
}
254266
for _, file := range files {
255-
content, err := ioutil.ReadFile(file)
267+
content, err := os.ReadFile(file)
256268
if err != nil {
257269
continue
258270
}
259271
if strings.Contains(string(content), fmt.Sprintf(`"name" "%s"`, game.Name)) {
260272
err := updateACF(file, newBehavior)
261273
if err != nil {
262-
logBox.SetText(logBox.Text + fmt.Sprintf("Failed to update %s: %v\n", game.Name, err))
274+
appendToLog(logBox, logScroll, fmt.Sprintf("Failed to update %s: %v\n", game.Name, err))
263275
} else {
264-
logBox.SetText(logBox.Text + fmt.Sprintf("Successfully updated %s\n", game.Name))
276+
appendToLog(logBox, logScroll, fmt.Sprintf("Successfully updated %s\n", game.Name))
265277
updatedGames[i].AutoUpdateBehavior = newBehavior
266278
}
267279
return
268280
}
269281
}
270-
logBox.SetText(logBox.Text + fmt.Sprintf("Failed to find appmanifest for %s\n", game.Name))
282+
appendToLog(logBox, logScroll, fmt.Sprintf("Failed to find appmanifest for %s\n", game.Name))
271283
}(i, game)
272284
}
273285
wg.Wait()
274-
logBox.SetText(logBox.Text + "Update complete.\n")
286+
appendToLog(logBox, logScroll, "Update complete.\n")
275287

276288
return updatedGames
277289
}
278290

279291
func updateACF(filePath, newBehavior string) error {
280-
content, err := ioutil.ReadFile(filePath)
292+
content, err := os.ReadFile(filePath)
281293
if err != nil {
282294
return err
283295
}
@@ -310,7 +322,7 @@ func updateACF(filePath, newBehavior string) error {
310322
"}")
311323
}
312324

313-
return ioutil.WriteFile(filePath, []byte(strings.Join(newLines, "\n")), 0644)
325+
return os.WriteFile(filePath, []byte(strings.Join(newLines, "\n")), 0644)
314326
}
315327

316328
func getLibraryPaths(libraries []SteamLibrary) []string {
@@ -320,3 +332,9 @@ func getLibraryPaths(libraries []SteamLibrary) []string {
320332
}
321333
return paths
322334
}
335+
336+
func appendToLog(logBox *widget.Entry, logScroll *container.Scroll, message string) {
337+
logBox.SetText(logBox.Text + message)
338+
logScroll.ScrollToBottom()
339+
logScroll.Refresh()
340+
}

0 commit comments

Comments
 (0)