From 05446c758b556995949b8a5d2239060a8c3bdc09 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sun, 11 May 2025 14:01:57 -0400 Subject: [PATCH] maintain dir state as int, not string --- main.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 5c1937e..18b524e 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "log" "os" "path/filepath" - "slices" "strconv" "strings" @@ -19,9 +18,9 @@ import ( ) type state struct { - currentDirectory string - currentFile string - images []string + dir string + i int + images []string } //go:embed noise.png @@ -40,7 +39,7 @@ var fileListBindVar = tk.Variable("FileList") var directoryState state func (d state) pathToImageAtIndex(i int) string { - return filepath.Join(directoryState.currentDirectory, directoryState.images[i]) + return filepath.Join(directoryState.dir, directoryState.images[i]) } func must[T any](t T, err error) T { @@ -86,9 +85,8 @@ func newDirectory() { } func newBrowse(file string) { - directoryState.currentFile = filepath.Base(file) - directoryState.currentDirectory = filepath.Dir(file) - dirfiles, err := os.ReadDir(directoryState.currentDirectory) + directoryState.dir = filepath.Dir(file) + dirfiles, err := os.ReadDir(directoryState.dir) if err != nil { log.Printf("could not open dir: %v", err) return @@ -131,7 +129,6 @@ func updateImage(file string) { imaging.CatmullRom, ) repaint(filepath.Base(file), tk.Data(i)) - directoryState.currentFile = filepath.Base(file) } func repaint(name string, data tk.Opt) { @@ -148,6 +145,7 @@ func repaint(name string, data tk.Opt) { tk.App.Center() } +// TODO: make this use withdraw/deiconify func destroyFileList(destroy bool) { if destroy { tk.Destroy(fileList) @@ -163,7 +161,7 @@ func constructFileList() { lb = fileList.Listbox(tk.Height(0)) for i := range directoryState.images { lb.Insert("end", directoryState.images[i]) - if i == slices.Index(directoryState.images, directoryState.currentFile) { + if i == directoryState.i { lb.SelectionSet(i) } } @@ -188,6 +186,7 @@ func menuSelect() { } func moveSelectInFileList(target int) { + directoryState.i = target if lb != nil { lb.SelectionClear("0", "end") lb.SelectionSet(target) @@ -196,7 +195,7 @@ func moveSelectInFileList(target int) { } func keyPress(e *tk.Event) { - curr := slices.Index(directoryState.images, directoryState.currentFile) + curr := directoryState.i switch e.Keysym { case ".": log.Printf("state: %+v", directoryState)