From 5c1ed3e47e06935284ac897428d8ae6d3cd8745e Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Mon, 23 Jan 2023 19:05:40 +0100 Subject: [PATCH] add delete-temp-files --- config.example.toml | 6 ++++++ config/config.go | 38 ++++++++++++++++++++------------------ config/default_config.go | 6 ++++++ config/toml.go | 9 +++++---- config/toml_default.go | 1 + docs/man/tut.5 | 10 ++++++++++ docs/man/tut.5.md | 4 ++++ ui/media.go | 14 +++++++++----- ui/tutview.go | 12 +++++++++++- 9 files changed, 72 insertions(+), 28 deletions(-) diff --git a/config.example.toml b/config.example.toml index c3bebcc..c7e96f7 100644 --- a/config.example.toml +++ b/config.example.toml @@ -253,6 +253,12 @@ leader-timeout=1000 # shortcut="" [media] +# Media files will be removed directly after they've been opened. Some programs +# doesn't like this, so if your media doesn't open, try set this to false. Tut +# will remove all files once you close the program. +# default=true +delete-temp-files=true + [media.image] # The program to open images. TUT_OS_DEFAULT equals xdg-open on Linux, open on # MacOS and start on Windows. diff --git a/config/config.go b/config/config.go index 769b9ba..894e556 100644 --- a/config/config.go +++ b/config/config.go @@ -248,24 +248,25 @@ type Style struct { } type Media struct { - ImageViewer string - ImageArgs []string - ImageTerminal bool - ImageSingle bool - ImageReverse bool - VideoViewer string - VideoArgs []string - VideoTerminal bool - VideoSingle bool - VideoReverse bool - AudioViewer string - AudioArgs []string - AudioTerminal bool - AudioSingle bool - AudioReverse bool - LinkViewer string - LinkArgs []string - LinkTerminal bool + DeleteTmpFiles bool + ImageViewer string + ImageArgs []string + ImageTerminal bool + ImageSingle bool + ImageReverse bool + VideoViewer string + VideoArgs []string + VideoTerminal bool + VideoSingle bool + VideoReverse bool + AudioViewer string + AudioArgs []string + AudioTerminal bool + AudioSingle bool + AudioReverse bool + LinkViewer string + LinkArgs []string + LinkTerminal bool } type Pattern struct { @@ -770,6 +771,7 @@ func getViewer(v *ViewerTOML, def *ViewerTOML) (program, args string, terminal, func parseMedia(cfg MediaTOML) Media { media := Media{} + media.DeleteTmpFiles = NilDefaultBool(cfg.DeleteTmpFiles, ConfigDefault.Media.DeleteTmpFiles) var program, args string var terminal, single, reverse bool diff --git a/config/default_config.go b/config/default_config.go index 8c5fd2a..6c307b0 100644 --- a/config/default_config.go +++ b/config/default_config.go @@ -255,6 +255,12 @@ leader-timeout=1000 # shortcut="" [media] +# Media files will be removed directly after they've been opened. Some programs +# doesn't like this, so if your media doesn't open, try set this to false. Tut +# will remove all files once you close the program. +# default=true +delete-temp-files=true + [media.image] # The program to open images. TUT_OS_DEFAULT equals xdg-open on Linux, open on # MacOS and start on Windows. diff --git a/config/toml.go b/config/toml.go index 48bc2db..6270978 100644 --- a/config/toml.go +++ b/config/toml.go @@ -119,10 +119,11 @@ type ViewerTOML struct { } type MediaTOML struct { - Image *ViewerTOML `toml:"image"` - Video *ViewerTOML `toml:"video"` - Audio *ViewerTOML `toml:"audio"` - Link *ViewerTOML `toml:"link"` + DeleteTmpFiles *bool `toml:"delete-temp-files"` + Image *ViewerTOML `toml:"image"` + Video *ViewerTOML `toml:"video"` + Audio *ViewerTOML `toml:"audio"` + Link *ViewerTOML `toml:"link"` } type PatternTOML struct { diff --git a/config/toml_default.go b/config/toml_default.go index 8003e11..bdaa5fc 100644 --- a/config/toml_default.go +++ b/config/toml_default.go @@ -90,6 +90,7 @@ var ConfigDefault = ConfigTOML{ TimelineNameText: sp("#808080"), }, Media: MediaTOML{ + DeleteTmpFiles: bt, Image: &ViewerTOML{ Program: sp("TUT_OS_DEFAULT"), Args: sp(""), diff --git a/docs/man/tut.5 b/docs/man/tut.5 index 192362b..6463d9e 100644 --- a/docs/man/tut.5 +++ b/docs/man/tut.5 @@ -506,6 +506,16 @@ A shortcut to run this action with your leader-key + this shortcut. .SH MEDIA .PP This section is [media] in your configuration file +.SS delete-temp-files +.PP +Media files will be removed directly after they\[aq]ve been opened. +Some programs doesn\[aq]t like this, so if your media doesn\[aq]t open, +try set this to false. +Tut will remove all files once you close the program. +.PD 0 +.P +.PD +\f[B]delete-temp-files\f[R]=\f[I]true\f[R] .SH MEDIA.IMAGE .PP This section is [media.image] in your configuration file diff --git a/docs/man/tut.5.md b/docs/man/tut.5.md index 1982101..1f922b2 100644 --- a/docs/man/tut.5.md +++ b/docs/man/tut.5.md @@ -265,6 +265,10 @@ A shortcut to run this action with your leader-key + this shortcut. # MEDIA This section is \[media\] in your configuration file +## delete-temp-files +Media files will be removed directly after they\'ve been opened. Some programs doesn\'t like this, so if your media doesn\'t open, try set this to false. Tut will remove all files once you close the program. +**delete-temp-files**=*true* + # MEDIA.IMAGE This section is \[media.image\] in your configuration file diff --git a/ui/media.go b/ui/media.go index e871308..c97813c 100644 --- a/ui/media.go +++ b/ui/media.go @@ -155,18 +155,22 @@ func openMediaType(tv *TutView, filenames []string, mediaType string) { go func() { for _, ext := range external { exec.Command(ext.Name, ext.Args...).Run() - deleteFiles(ext.Filenames) + deleteFiles(tv, ext.Filenames) } }() for _, term := range terminal { openInTerminal(tv, term.Name, term.Args...) - deleteFiles(term.Filenames) + deleteFiles(tv, term.Filenames) } } -func deleteFiles(filenames []string) { - for _, filename := range filenames { - os.Remove(filename) +func deleteFiles(tv *TutView, filenames []string) { + if tv.tut.Config.Media.DeleteTmpFiles { + for _, filename := range filenames { + os.Remove(filename) + } + } else { + tv.FileList = append(tv.FileList, filenames...) } } diff --git a/ui/tutview.go b/ui/tutview.go index 7c93cb5..f62996c 100644 --- a/ui/tutview.go +++ b/ui/tutview.go @@ -71,9 +71,18 @@ type TutView struct { HelpView *HelpView EditorView *EditorView ModalView *ModalView + + FileList []string } func (tv *TutView) CleanExit(code int) { + if !tv.tut.Config.Media.DeleteTmpFiles { + for _, t := range TutViews.Views { + for _, f := range t.FileList { + os.Remove(f) + } + } + } os.Exit(code) } @@ -123,7 +132,8 @@ func NewTutView(selectedUser string) { App: App, Config: Config, }, - View: tview.NewPages(), + View: tview.NewPages(), + FileList: []string{}, } tv.Leader = NewLeader(tv) tv.Shared = NewShared(tv)