Skip to content

Commit

Permalink
Implemented --show-images flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 4, 2021
1 parent c55e11e commit 7d5c1bb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cli/rootCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

var server string
var accessToken string
var flagShowImages bool
// var clientID string
// var clientSecret string

Expand All @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions cli/tuiCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
4 changes: 2 additions & 2 deletions tui/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
20 changes: 11 additions & 9 deletions tui/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"))

Expand Down
8 changes: 7 additions & 1 deletion tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
InsertMode = 2
)

type TUIOptions struct {
ShowImages bool
}

type TUICore struct {
Client *mastodon.Client
App *tview.Application
Expand All @@ -29,6 +33,8 @@ type TUICore struct {

Timeline mast.Timeline
RenderedTimelineType mast.TimelineType

Options TUIOptions
}

func TUI(tuiCore TUICore) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7d5c1bb

Please sign in to comment.