From a41880dad5d19362d77da8020ae3f2956edee796 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 5 Sep 2023 22:09:55 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20temp=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For trimming/fading --- app/choose.go | 54 ++++++++++++++++++++++++++++++++++++++++----------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/app/choose.go b/app/choose.go index 8a4d213..28644b0 100644 --- a/app/choose.go +++ b/app/choose.go @@ -1,14 +1,18 @@ 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" - "fmt" - p "path" + "github.com/rkoesters/xdg/trash" ) var file *media.Probe +var tmpfile *media.Probe func PickNewFile() media.Probe { path := files.PickRandomFile() @@ -21,19 +25,19 @@ func SetFile(path string) media.Probe { 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 - var base string - var dir string good := false for !good { path = files.PickRandomFile() - base = p.Base(path) - dir = p.Dir(path) - dir = p.Base(dir) - - msg = fmt.Sprintf("We've selected %s%s%s\nIs that ok?", dir, "/", base) + msg = fmt.Sprintf("We've selected %s\nIs that ok?", getShortPath(path)) good = boolean.Choose(msg) } @@ -47,6 +51,34 @@ func GetFile() media.Probe { return *file } -func ForgetFile() { - file = nil +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) } diff --git a/go.mod b/go.mod index 303a4d8..6c45ff3 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/creack/pty v1.1.18 github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f github.com/rivo/tview v0.0.0-20230826224341-9754ab44dc1c + github.com/rkoesters/xdg v0.0.1 github.com/spf13/cobra v1.7.0 github.com/u2takey/ffmpeg-go v0.5.0 golang.org/x/term v0.11.0 diff --git a/go.sum b/go.sum index 4cf4fba..e633221 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ github.com/rivo/tview v0.0.0-20230826224341-9754ab44dc1c/go.mod h1:nVwGv4MP47T0j github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw= github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rkoesters/xdg v0.0.1 h1:RmfYxghVvIsb4d51u5LtNOcwqY5r3P44u6o86qqvBMA= +github.com/rkoesters/xdg v0.0.1/go.mod h1:5DcbjvJkY00fIOKkaBnylbC/rmc1NNJP5dmUcnlcm7U= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=