strip-beats/app/choose.go
Dan Jones a41880dad5 Add temp file
For trimming/fading
2023-09-05 22:09:55 -05:00

84 lines
1.3 KiB
Go

package app
import (
"fmt"
"os"
p "path"
"codeberg.org/danjones000/strip-beats/files"
"codeberg.org/danjones000/strip-beats/input/boolean"
"codeberg.org/danjones000/strip-beats/media"
"github.com/rkoesters/xdg/trash"
)
var file *media.Probe
var tmpfile *media.Probe
func PickNewFile() media.Probe {
path := files.PickRandomFile()
return SetFile(path)
}
func SetFile(path string) media.Probe {
f := media.ProbeFile(path)
file = &f
return f
}
func SetTmpFile(path string) media.Probe {
f := media.ProbeFile(path)
tmpfile = &f
return f
}
func PickFileWithConf() media.Probe {
var path string
var msg string
good := false
for !good {
path = files.PickRandomFile()
msg = fmt.Sprintf("We've selected %s\nIs that ok?", getShortPath(path))
good = boolean.Choose(msg)
}
return SetFile(path)
}
func GetFile() media.Probe {
if file == nil {
return PickNewFile()
}
return *file
}
func GetTmpFile() media.Probe {
if tmpfile == nil {
return GetFile()
}
return *tmpfile
}
func BailOut() {
if tmpfile != nil {
os.Remove(tmpfile.Format.Path)
}
file = nil
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)
}