From 196a4709c61ba2e21fea5de49b936393b10a4aaa Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 21 Jun 2024 21:47:18 +0100 Subject: [PATCH] support opening video media --- cmd/enbas/main.go | 5 +++- internal/executor/show.go | 56 +++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index dcd8afd..b85f639 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -33,6 +33,7 @@ func run() error { cacheDir string pager string imageViewer string + videoPlayer string maxTerminalWidth int noColor *bool ) @@ -40,7 +41,8 @@ func run() error { flag.StringVar(&configDir, "config-dir", "", "Specify your config directory") flag.StringVar(&cacheDir, "cache-dir", "", "Specify your cache directory") flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.") - flag.StringVar(&imageViewer, "image-viewer", "", "Specify your preferred image viewer for viewing images attached to statuses.") + flag.StringVar(&imageViewer, "image-viewer", "", "Specify your favourite image viewers.") + flag.StringVar(&videoPlayer, "video-player", "", "Specify your favourite video player.") flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.") flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error { @@ -176,6 +178,7 @@ func run() error { configDir, cacheDir, imageViewer, + videoPlayer, executor.CommandShow, executor.CommandSummaryLookup(executor.CommandShow), ), diff --git a/internal/executor/show.go b/internal/executor/show.go index ea84c83..2f09a5f 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -35,11 +35,12 @@ type ShowExecutor struct { pollID string fromResourceType string imageViewer string + videoPlayer string limit int attachmentIDs MultiStringFlagValue } -func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer, name, summary string) *ShowExecutor { +func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer, videoPlayer, name, summary string) *ShowExecutor { showExe := ShowExecutor{ FlagSet: flag.NewFlagSet(name, flag.ExitOnError), @@ -47,6 +48,7 @@ func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer configDir: configDir, cacheRoot: cacheRoot, imageViewer: imageViewer, + videoPlayer: videoPlayer, } showExe.BoolVar(&showExe.myAccount, flagMyAccount, false, "Set to true to lookup your account") @@ -477,20 +479,29 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error { return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err) } - attachmentsHashMap := make(map[string]string) - filepaths := make([]string, len(s.attachmentIDs)) - - for _, statusAttachment := range status.MediaAttachments { - attachmentsHashMap[statusAttachment.ID] = statusAttachment.URL + type media struct { + url string + mediaType string } - for ind, attachmentID := range s.attachmentIDs { - mediaURL, ok := attachmentsHashMap[attachmentID] + attachmentsHashMap := make(map[string]media) + imageFiles := make([]string, 0) + videoFiles := make([]string, 0) + + for _, statusAttachment := range status.MediaAttachments { + attachmentsHashMap[statusAttachment.ID] = media{ + url: statusAttachment.URL, + mediaType: statusAttachment.Type, + } + } + + for _, attachmentID := range s.attachmentIDs { + mediaObj, ok := attachmentsHashMap[attachmentID] if !ok { return fmt.Errorf("unknown media attachment: %s", attachmentID) } - split := strings.Split(mediaURL, "/") + split := strings.Split(mediaObj.url, "/") filename := split[len(split)-1] filePath := filepath.Join(cacheDir, filename) @@ -504,16 +515,33 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error { } if !fileExists { - if err := gtsClient.DownloadMedia(mediaURL, filePath); err != nil { - return fmt.Errorf("unable to download the media attachment for %s: %w", attachmentID, err) + if err := gtsClient.DownloadMedia(mediaObj.url, filePath); err != nil { + return fmt.Errorf( + "unable to download the media attachment for %s: %w", + attachmentID, + err, + ) } } - filepaths[ind] = filePath + switch mediaObj.mediaType { + case "image": + imageFiles = append(imageFiles, filePath) + case "video": + videoFiles = append(videoFiles, filePath) + } } - if err := utilities.OpenMedia(s.imageViewer, filepaths); err != nil { - return fmt.Errorf("unable to open the image viewer: %w", err) + if len(imageFiles) > 0 { + if err := utilities.OpenMedia(s.imageViewer, imageFiles); err != nil { + return fmt.Errorf("unable to open the image viewer: %w", err) + } + } + + if len(videoFiles) > 0 { + if err := utilities.OpenMedia(s.videoPlayer, videoFiles); err != nil { + return fmt.Errorf("unable to open the video player: %w", err) + } } return nil