checkpoint: can now open images from an image viewer

This commit is contained in:
Dan Anglin 2024-06-20 04:08:46 +01:00
parent a4872dff46
commit cf3b08e726
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 34 additions and 6 deletions

View file

@ -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),
),

View file

@ -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,
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
}

View file

@ -1,6 +1,13 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// 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
}