This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/file/directory.go

74 lines
1.5 KiB
Go
Raw Normal View History

2023-03-21 16:26:48 +00:00
package fileElements
import "io/fs"
import "path/filepath"
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
type ReadDirStatFS interface {
fs.ReadDirFS
fs.StatFS
}
type DirectoryView struct {
*basicElements.List
filesystem ReadDirStatFS
location string
onChoose func (file string)
}
func NewDirectoryView (
location string,
within ReadDirStatFS,
) (
element *DirectoryView,
err error,
) {
element = &DirectoryView {
List: basicElements.NewList(),
}
err = element.SetLocation(location, within)
return
}
func (element *DirectoryView) Location () (string, fs.ReadDirFS) {
return element.location, element.filesystem
}
func (element *DirectoryView) SetLocation (
location string,
within ReadDirStatFS,
) error {
if within == nil {
within = defaultFS { }
}
element.location = location
element.filesystem = within
return element.Update()
}
func (element *DirectoryView) Update () error {
entries, err := element.filesystem.ReadDir(element.location)
listEntries := make([]basicElements.ListEntry, len(entries))
for index, entry := range entries {
filePath := filepath.Join(element.location, entry.Name())
listEntries[index] = basicElements.NewListEntry (
entry.Name(),
func () {
filePath := filePath
if element.onChoose != nil {
element.onChoose(filePath)
}
})
}
element.Clear()
element.Append(listEntries...)
return err
}
func (element *DirectoryView) OnChoose (callback func (file string)) {
element.onChoose = callback
}