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"
"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)