maintain dir state as int, not string

This commit is contained in:
2025-05-11 14:01:57 -04:00
parent ef8bf980ce
commit 05446c758b

21
main.go
View File

@@ -7,7 +7,6 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"strconv" "strconv"
"strings" "strings"
@@ -19,9 +18,9 @@ import (
) )
type state struct { type state struct {
currentDirectory string dir string
currentFile string i int
images []string images []string
} }
//go:embed noise.png //go:embed noise.png
@@ -40,7 +39,7 @@ var fileListBindVar = tk.Variable("FileList")
var directoryState state var directoryState state
func (d state) pathToImageAtIndex(i int) string { 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 { func must[T any](t T, err error) T {
@@ -86,9 +85,8 @@ func newDirectory() {
} }
func newBrowse(file string) { func newBrowse(file string) {
directoryState.currentFile = filepath.Base(file) directoryState.dir = filepath.Dir(file)
directoryState.currentDirectory = filepath.Dir(file) dirfiles, err := os.ReadDir(directoryState.dir)
dirfiles, err := os.ReadDir(directoryState.currentDirectory)
if err != nil { if err != nil {
log.Printf("could not open dir: %v", err) log.Printf("could not open dir: %v", err)
return return
@@ -131,7 +129,6 @@ func updateImage(file string) {
imaging.CatmullRom, imaging.CatmullRom,
) )
repaint(filepath.Base(file), tk.Data(i)) repaint(filepath.Base(file), tk.Data(i))
directoryState.currentFile = filepath.Base(file)
} }
func repaint(name string, data tk.Opt) { func repaint(name string, data tk.Opt) {
@@ -148,6 +145,7 @@ func repaint(name string, data tk.Opt) {
tk.App.Center() tk.App.Center()
} }
// TODO: make this use withdraw/deiconify
func destroyFileList(destroy bool) { func destroyFileList(destroy bool) {
if destroy { if destroy {
tk.Destroy(fileList) tk.Destroy(fileList)
@@ -163,7 +161,7 @@ func constructFileList() {
lb = fileList.Listbox(tk.Height(0)) lb = fileList.Listbox(tk.Height(0))
for i := range directoryState.images { for i := range directoryState.images {
lb.Insert("end", directoryState.images[i]) lb.Insert("end", directoryState.images[i])
if i == slices.Index(directoryState.images, directoryState.currentFile) { if i == directoryState.i {
lb.SelectionSet(i) lb.SelectionSet(i)
} }
} }
@@ -188,6 +186,7 @@ func menuSelect() {
} }
func moveSelectInFileList(target int) { func moveSelectInFileList(target int) {
directoryState.i = target
if lb != nil { if lb != nil {
lb.SelectionClear("0", "end") lb.SelectionClear("0", "end")
lb.SelectionSet(target) lb.SelectionSet(target)
@@ -196,7 +195,7 @@ func moveSelectInFileList(target int) {
} }
func keyPress(e *tk.Event) { func keyPress(e *tk.Event) {
curr := slices.Index(directoryState.images, directoryState.currentFile) curr := directoryState.i
switch e.Keysym { switch e.Keysym {
case ".": case ".":
log.Printf("state: %+v", directoryState) log.Printf("state: %+v", directoryState)