From 1d337c417a2a416aa121b68be9c40a9bc467505a Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Sat, 29 Feb 2020 03:46:39 -0500 Subject: [PATCH 1/2] Add image viewing --- shortcuts/shortcuts.go | 2 ++ ui/window.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/shortcuts/shortcuts.go b/shortcuts/shortcuts.go index f7f5c442..1e6e867b 100644 --- a/shortcuts/shortcuts.go +++ b/shortcuts/shortcuts.go @@ -31,6 +31,8 @@ var ( chatview, tcell.NewEventKey(tcell.KeyRune, 's', tcell.ModNone)) DeleteSelectedMessage = addShortcut("toggle_selected_message_spoilers", "Toggle spoilers in selected message", chatview, tcell.NewEventKey(tcell.KeyDelete, 0, tcell.ModNone)) + ViewSelectedMessageImages = addShortcut("view_selected_message_images", "Open selected message's attached images in feh", + chatview, tcell.NewEventKey(tcell.KeyRune, 'o', tcell.ModNone)) ExpandSelectionToLeft = addShortcut("expand_selection_word_to_left", "Expand selection word to left", multilineTextInput, tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModShift)) diff --git a/ui/window.go b/ui/window.go index 89359454..2a4a8551 100644 --- a/ui/window.go +++ b/ui/window.go @@ -7,6 +7,7 @@ import ( "strings" "time" "unicode" + "os/exec" "github.com/mattn/go-runewidth" "github.com/mdp/qrterminal/v3" @@ -385,6 +386,22 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S return nil } + if shortcuts.ViewSelectedMessageImages.Equals(event) { + links := make([]string, 0, len(message.Attachments)) + for _, file := range message.Attachments { + links = append(links, file.URL) + } + if len(links) > 0 { + cmd := exec.Command("feh", links...) + err := cmd.Start() + if err != nil { + window.ShowErrorDialog(err.Error()) + } + } + + return nil + } + return event }) window.messageContainer = window.chatView.GetPrimitive() From b5073e084ec9ea282c575e95e019768b6d120671 Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Sat, 29 Feb 2020 13:36:06 -0500 Subject: [PATCH 2/2] Make image viewer user-configurable --- config/config.go | 7 +++++++ shortcuts/shortcuts.go | 2 +- ui/window.go | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 31e0c981..01cf58de 100644 --- a/config/config.go +++ b/config/config.go @@ -56,6 +56,7 @@ var ( ShowUpdateNotifications: true, IndicateChannelAccessRestriction: false, ShowBottomBar: true, + ImageViewer: "feh", } ) @@ -133,6 +134,12 @@ type Config struct { // ShowBottomBar decides whether an informational line is shown at the // bottom of cordless or not. ShowBottomBar bool + + // The image viewer to open when the user uses the "view attached images" + // shortcut. This program will be passed a list of 1 or more image links + // as if it were called from the command line, so the selected program + // must be capable of opening image links. + ImageViewer string } // Account has a name and a token. The name is just for the users recognition. diff --git a/shortcuts/shortcuts.go b/shortcuts/shortcuts.go index 1e6e867b..430286bc 100644 --- a/shortcuts/shortcuts.go +++ b/shortcuts/shortcuts.go @@ -31,7 +31,7 @@ var ( chatview, tcell.NewEventKey(tcell.KeyRune, 's', tcell.ModNone)) DeleteSelectedMessage = addShortcut("toggle_selected_message_spoilers", "Toggle spoilers in selected message", chatview, tcell.NewEventKey(tcell.KeyDelete, 0, tcell.ModNone)) - ViewSelectedMessageImages = addShortcut("view_selected_message_images", "Open selected message's attached images in feh", + ViewSelectedMessageImages = addShortcut("view_selected_message_images", "View selected message's attached images", chatview, tcell.NewEventKey(tcell.KeyRune, 'o', tcell.ModNone)) ExpandSelectionToLeft = addShortcut("expand_selection_word_to_left", "Expand selection word to left", diff --git a/ui/window.go b/ui/window.go index 2a4a8551..cd4cbd06 100644 --- a/ui/window.go +++ b/ui/window.go @@ -392,7 +392,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S links = append(links, file.URL) } if len(links) > 0 { - cmd := exec.Command("feh", links...) + cmd := exec.Command(config.Current.ImageViewer, links...) err := cmd.Start() if err != nil { window.ShowErrorDialog(err.Error())