diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index d74d545..dcd8afd 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -32,6 +32,7 @@ func run() error { configDir string cacheDir string pager string + imageViewer string maxTerminalWidth int noColor *bool ) @@ -39,6 +40,7 @@ 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.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 { @@ -173,6 +175,7 @@ func run() error { printer, configDir, cacheDir, + imageViewer, executor.CommandShow, executor.CommandSummaryLookup(executor.CommandShow), ), diff --git a/internal/executor/show.go b/internal/executor/show.go index 1ee9a17..51ff058 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -36,16 +36,18 @@ type ShowExecutor struct { pollID string attachmentID string fromResourceType string + imageViewer string limit int } -func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, name, summary string) *ShowExecutor { +func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer, name, summary string) *ShowExecutor { showExe := ShowExecutor{ FlagSet: flag.NewFlagSet(name, flag.ExitOnError), - printer: printer, - configDir: configDir, - cacheRoot: cacheRoot, + printer: printer, + configDir: configDir, + cacheRoot: cacheRoot, + imageViewer: imageViewer, } showExe.BoolVar(&showExe.myAccount, flagMyAccount, false, "Set to true to lookup your account") @@ -455,6 +457,7 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error { return FlagNotSetError{flagText: flagStatusID} } + status, err := gtsClient.GetStatus(s.statusID) if err != nil { return fmt.Errorf("unable to retrieve the status: %w", err) @@ -501,8 +504,9 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error { } } - // TODO: display the file in the image viewer. - fmt.Println("checkpoint: the media file is now on disk; just need to open it in an image viewer...") + if err := utilities.OpenMedia(s.imageViewer, mediaFilePath); err != nil { + return fmt.Errorf("unable to open the image viewer: %w", err) + } return nil } diff --git a/internal/utilities/utilities.go b/internal/utilities/utilities.go index ab535fc..c078f10 100644 --- a/internal/utilities/utilities.go +++ b/internal/utilities/utilities.go @@ -1,6 +1,13 @@ +// SPDX-FileCopyrightText: 2024 Dan Anglin +// +// SPDX-License-Identifier: GPL-3.0-or-later + package utilities import ( + "errors" + "fmt" + "os/exec" "regexp" ) @@ -9,3 +16,17 @@ func GetFQDN(url string) string { return r.ReplaceAllString(url, "") } + +func OpenMedia(viewer, path string) error { + if viewer == "" { + return errors.New("the image viewer is not specified") + } + + command := exec.Command(viewer, path) + + if err := command.Start(); err != nil { + return fmt.Errorf("received an error after starting the image viewer: %w", err) + } + + return nil +}