Skip to content

Commit

Permalink
Merge branch 'd5xtgr-media-handling'
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusLindroth committed May 8, 2020
2 parents 1345d9f + 505ae2b commit fbab92c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 26 deletions.
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type App struct {
API *API
Config *Config
HaveAccount bool
FileList []string
}
2 changes: 1 addition & 1 deletion authoverlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (a *AuthOverlay) GotInput() {
return
}
a.account = acc
openURL(acc.AuthURI)
openURL(a.app.Config.Media, acc.AuthURI)
a.Input.SetText("")
a.authStep = authCodeStep
a.Draw()
Expand Down
52 changes: 40 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"strings"

"github.com/gdamore/tcell"
"github.com/kyoh86/xdg"
Expand Down Expand Up @@ -44,11 +45,16 @@ type StyleConfig struct {

type MediaConfig struct {
ImageViewer string
ImageArgs []string
ImageSingle bool
VideoViewer string
VideoArgs []string
VideoSingle bool
AudioViewer string
AudioArgs []string
AudioSingle bool
LinkViewer string
LinkArgs []string
}

func parseColor(input string, def string) tcell.Color {
Expand Down Expand Up @@ -139,27 +145,45 @@ func parseGeneral(cfg *ini.File) GeneralConfig {

func parseMedia(cfg *ini.File) MediaConfig {
media := MediaConfig{}
imageViewer := cfg.Section("media").Key("image-viewer").String()
if imageViewer == "" {
imageViewer = "xdg-open"
imageViewerComponents := strings.Fields(cfg.Section("media").Key("image-viewer").String())
if len(imageViewerComponents) == 0 {
media.ImageViewer = "xdg-open"
media.ImageArgs = []string{}
} else {
media.ImageViewer = imageViewerComponents[0]
media.ImageArgs = imageViewerComponents[1:]
}
media.ImageViewer = imageViewer
media.ImageSingle = cfg.Section("media").Key("image-single").MustBool(true)

videoViewer := cfg.Section("media").Key("video-viewer").String()
if videoViewer == "" {
videoViewer = "xdg-open"
videoViewerComponents := strings.Fields(cfg.Section("media").Key("video-viewer").String())
if len(videoViewerComponents) == 0 {
media.VideoViewer = "xdg-open"
media.VideoArgs = []string{}
} else {
media.VideoViewer = videoViewerComponents[0]
media.VideoArgs = videoViewerComponents[1:]
}
media.VideoViewer = videoViewer
media.VideoSingle = cfg.Section("media").Key("video-single").MustBool(true)

audioViewer := cfg.Section("media").Key("audio-viewer").String()
if audioViewer == "" {
videoViewer = "xdg-open"
audioViewerComponents := strings.Fields(cfg.Section("media").Key("audio-viewer").String())
if len(audioViewerComponents) == 0 {
media.AudioViewer = "xdg-open"
media.AudioArgs = []string{}
} else {
media.AudioViewer = audioViewerComponents[0]
media.AudioArgs = audioViewerComponents[1:]
}
media.AudioViewer = audioViewer
media.AudioSingle = cfg.Section("media").Key("audio-single").MustBool(true)

linkViewerComponents := strings.Fields(cfg.Section("media").Key("link-viewer").String())
if len(linkViewerComponents) == 0 {
media.LinkViewer = "xdg-open"
media.LinkArgs = []string{}
} else {
media.LinkViewer = linkViewerComponents[0]
media.LinkArgs = linkViewerComponents[1:]
}

return media
}

Expand Down Expand Up @@ -254,6 +278,10 @@ audio-viewer=xdg-open
# default=true
audio-single=true
# Your web browser
# default=xdg-open
link-viewer=xdg-open
[style]
# All styles can be represented in their HEX value like #ffffff or
# with their name, so in this case white.
Expand Down
2 changes: 1 addition & 1 deletion linkoverlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (l *LinkOverlay) Open() {
return
}
if index < len(l.urls) {
openURL(l.urls[index].URL)
openURL(l.app.Config.Media, l.urls[index].URL)
return
}
mIndex := index - len(l.urls)
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strings"
"os"

"github.com/gdamore/tcell"
)
Expand Down Expand Up @@ -78,6 +79,8 @@ func main() {
app.UI.LoggedIn()
}

app.FileList = []string{}

app.UI.Root.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if !app.HaveAccount {
if event.Key() == tcell.KeyRune {
Expand Down Expand Up @@ -242,4 +245,8 @@ func main() {
if err := app.UI.Root.SetRoot(app.UI.Pages, true).Run(); err != nil {
panic(err)
}

for _, f := range app.FileList {
os.Remove(f)
}
}
1 change: 1 addition & 0 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func (ui *UI) OpenMedia(status *mastodon.Status) {
files = append(files, f)
}
go openMediaType(ui.app.Config.Media, files, key)
ui.app.FileList = append(ui.app.FileList, files...)
}
}

Expand Down
27 changes: 15 additions & 12 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,41 +100,44 @@ func openEditor(app *tview.Application, content string) (string, error) {
return strings.TrimSpace(string(text)), nil
}

func openURL(url string) {
exec.Command("xdg-open", url).Start()
func openURL(conf MediaConfig, url string) {
args := append(conf.LinkArgs, url)
exec.Command(conf.LinkViewer, args...).Start()
}

func openMediaType(conf MediaConfig, filenames []string, mediaType string) {
switch mediaType {
case "image":
if conf.ImageSingle {
for _, f := range filenames {
exec.Command(conf.ImageViewer, f).Run()
args := append(conf.ImageArgs, f)
exec.Command(conf.ImageViewer, args...).Run()
}
} else {
exec.Command(conf.ImageViewer, filenames...).Run()
args := append(conf.ImageArgs, filenames...)
exec.Command(conf.ImageViewer, args...).Run()
}
case "video", "gifv":
if conf.VideoSingle {
for _, f := range filenames {
exec.Command(conf.VideoViewer, f).Run()
args := append(conf.VideoArgs, f)
exec.Command(conf.VideoViewer, args...).Run()
}
} else {
exec.Command(conf.VideoViewer, filenames...).Run()
args := append(conf.VideoArgs, filenames...)
exec.Command(conf.VideoViewer, args...).Run()
}
case "audio":
if conf.AudioSingle {
for _, f := range filenames {
exec.Command(conf.AudioViewer, f).Run()
args := append(conf.AudioArgs, f)
exec.Command(conf.AudioViewer, args...).Run()
}
} else {
exec.Command(conf.AudioViewer, filenames...).Run()
args := append(conf.AudioArgs, filenames...)
exec.Command(conf.AudioViewer, args...).Run()
}
}

for _, f := range filenames {
os.Remove(f)
}
}

func downloadFile(url string) (string, error) {
Expand Down

0 comments on commit fbab92c

Please sign in to comment.