From 7d5c1bba2dc10c54ad006105527396a5b441afd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sat, 3 Apr 2021 21:37:17 -0500 Subject: [PATCH] Implemented --show-images flag --- cli/rootCmd.go | 2 ++ cli/tuiCmd.go | 3 +++ tui/timeline.go | 4 ++-- tui/toot.go | 20 +++++++++++--------- tui/tui.go | 8 +++++++- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/cli/rootCmd.go b/cli/rootCmd.go index 1521396..237da8f 100644 --- a/cli/rootCmd.go +++ b/cli/rootCmd.go @@ -10,6 +10,7 @@ import ( var server string var accessToken string +var flagShowImages bool // var clientID string // var clientSecret string @@ -34,6 +35,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&server, "server", LookupStrEnv("GOMPHOTHERIUM_SERVER",""), "Mastodon server") rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", LookupStrEnv("GOMPHOTHERIUM_ACCESS_TOKEN",""), "Mastodon access token") + rootCmd.PersistentFlags().BoolVarP(&flagShowImages, "show-images", "i", true, "Show images in timeline") } func initConfig() { diff --git a/cli/tuiCmd.go b/cli/tuiCmd.go index 1879440..df8c8a6 100644 --- a/cli/tuiCmd.go +++ b/cli/tuiCmd.go @@ -12,6 +12,9 @@ var tuiCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { tuiCore := tui.TUICore{ Client: MastodonClient, + Options: tui.TUIOptions{ + ShowImages: flagShowImages, + }, } tui.TUI(tuiCore) }, diff --git a/tui/timeline.go b/tui/timeline.go index 78092b0..0d386c0 100644 --- a/tui/timeline.go +++ b/tui/timeline.go @@ -6,14 +6,14 @@ import ( "github.com/mrusme/gomphotherium/mast" ) -func RenderTimeline(timeline *mast.Timeline, width int) (string, error) { +func RenderTimeline(timeline *mast.Timeline, width int, showImages bool) (string, error) { var output string = "" var err error = nil var tootOutput string = "" newRenderedIndex := len(timeline.Toots) for i := (timeline.LastRenderedIndex + 1); i < newRenderedIndex; i++ { - tootOutput, err = RenderToot(&timeline.Toots[i], width) + tootOutput, err = RenderToot(&timeline.Toots[i], width, showImages) output = fmt.Sprintf("%s%s\n", output, tootOutput) } diff --git a/tui/toot.go b/tui/toot.go index 6423403..14b22ad 100644 --- a/tui/toot.go +++ b/tui/toot.go @@ -9,14 +9,14 @@ import ( "github.com/grokify/html-strip-tags-go" "html" - // "image/color" - // "github.com/eliukblau/pixterm/pkg/ansimage" + "image/color" + "github.com/eliukblau/pixterm/pkg/ansimage" // "github.com/mattn/go-mastodon" "github.com/mrusme/gomphotherium/mast" ) -func RenderToot(toot *mast.Toot, width int) (string, error) { +func RenderToot(toot *mast.Toot, width int, showImages bool) (string, error) { var output string = "" var err error = nil @@ -39,12 +39,14 @@ func RenderToot(toot *mast.Toot, width int) (string, error) { output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-][magenta]%s[-][grey]%*d[-]\n", output, status.Account.DisplayName, account, inReplyTo, (width - len(string(toot.ID)) - runewidth.StringWidth(status.Account.DisplayName) - len(account) - inReplyToLen), toot.ID) output = fmt.Sprintf("%s%s\n", output, html.UnescapeString(strip.StripTags(status.Content))) - // for _, attachment := range status.MediaAttachments { - // pix, err := ansimage.NewScaledFromURL(attachment.PreviewURL, int((float64(width) * 0.75)), width, color.Transparent, ansimage.ScaleModeResize, ansimage.NoDithering) - // if err == nil { - // output = fmt.Sprintf("%s\n%s\n", output, pix.RenderExt(false, false)) - // } - // } + if showImages == true { + for _, attachment := range status.MediaAttachments { + pix, err := ansimage.NewScaledFromURL(attachment.PreviewURL, int((float64(width) * 0.75)), width, color.Transparent, ansimage.ScaleModeResize, ansimage.NoDithering) + if err == nil { + output = fmt.Sprintf("%s\n%s\n", output, pix.RenderExt(false, false)) + } + } + } output = fmt.Sprintf("%s[magenta]\xe2\x86\xab %d[-] [green]\xe2\x86\xbb %d[-] [yellow]\xe2\x98\x85 %d[-] [grey]on %s at %s[-]\n", output, status.RepliesCount, status.ReblogsCount, status.FavouritesCount, createdAt.Format("Jan 2"), createdAt.Format("15:04")) diff --git a/tui/tui.go b/tui/tui.go index 0c6d07d..dfc67bc 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -17,6 +17,10 @@ const ( InsertMode = 2 ) +type TUIOptions struct { + ShowImages bool +} + type TUICore struct { Client *mastodon.Client App *tview.Application @@ -29,6 +33,8 @@ type TUICore struct { Timeline mast.Timeline RenderedTimelineType mast.TimelineType + + Options TUIOptions } func TUI(tuiCore TUICore) { @@ -122,7 +128,7 @@ func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool { return false } - output, err := RenderTimeline(&tuiCore.Timeline, w) + output, err := RenderTimeline(&tuiCore.Timeline, w, tuiCore.Options.ShowImages) if err != nil { // TODO: Display errors somewhere