strip-beats/app/choose.go

85 lines
1.3 KiB
Go
Raw Normal View History

2023-09-04 08:54:00 -05:00
package app
import (
2023-09-05 22:09:55 -05:00
"fmt"
"os"
p "path"
2023-09-04 08:54:00 -05:00
"codeberg.org/danjones000/strip-beats/files"
2023-09-04 15:36:32 -05:00
"codeberg.org/danjones000/strip-beats/input/boolean"
2023-09-04 08:54:00 -05:00
"codeberg.org/danjones000/strip-beats/media"
2023-09-05 22:09:55 -05:00
"github.com/rkoesters/xdg/trash"
2023-09-04 08:54:00 -05:00
)
var file *media.Probe
2023-09-05 22:09:55 -05:00
var tmpfile *media.Probe
2023-09-04 08:54:00 -05:00
func PickNewFile() media.Probe {
path := files.PickRandomFile()
2023-09-04 09:36:00 -05:00
return SetFile(path)
}
func SetFile(path string) media.Probe {
2023-09-04 08:54:00 -05:00
f := media.ProbeFile(path)
file = &f
return f
}
2023-09-05 22:09:55 -05:00
func SetTmpFile(path string) media.Probe {
f := media.ProbeFile(path)
tmpfile = &f
return f
}
2023-09-04 15:36:32 -05:00
func PickFileWithConf() media.Probe {
var path string
var msg string
good := false
for !good {
path = files.PickRandomFile()
2023-09-05 22:09:55 -05:00
msg = fmt.Sprintf("We've selected %s\nIs that ok?", getShortPath(path))
2023-09-04 15:36:32 -05:00
good = boolean.Choose(msg)
}
return SetFile(path)
}
2023-09-04 08:54:00 -05:00
func GetFile() media.Probe {
if file == nil {
return PickNewFile()
}
return *file
}
2023-09-05 22:09:55 -05:00
func GetTmpFile() media.Probe {
if tmpfile == nil {
return GetFile()
}
return *tmpfile
}
func BailOut() {
if tmpfile != nil {
os.Remove(tmpfile.Format.Path)
}
2023-09-04 08:54:00 -05:00
file = nil
2023-09-05 22:09:55 -05:00
tmpfile = nil
}
func Finish() {
if file != nil {
err := trash.Trash(file.Format.Path)
if err != nil {
panic(err)
}
}
BailOut()
}
func getShortPath(path string) string {
base := p.Base(path)
dir := p.Dir(path)
dir = p.Base(dir)
return fmt.Sprintf("%s/%s", dir, base)
2023-09-04 08:54:00 -05:00
}