display status/reblog in stream

pull/32/head
Yasuhiro Matsumoto 2017-04-19 23:21:19 +09:00
parent f40839b98d
commit eb1c1cf0ae
4 changed files with 71 additions and 41 deletions

View File

@ -4,12 +4,10 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
@ -25,7 +23,10 @@ type SimpleJSON struct {
func cmdStream(c *cli.Context) error {
asJSON := c.Bool("json")
asSimpleJSON := c.Bool("simplejson")
client := c.App.Metadata["client"].(*mastodon.Client)
config := c.App.Metadata["config"].(*mastodon.Config)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sc := make(chan os.Signal, 1)
@ -54,6 +55,8 @@ func cmdStream(c *cli.Context) error {
cancel()
close(q)
}()
s := newScreen(config)
for e := range q {
if asJSON {
json.NewEncoder(c.App.Writer).Encode(e)
@ -70,14 +73,11 @@ func cmdStream(c *cli.Context) error {
} else {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
color.Set(color.Reset)
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
s.displayStatus(c.App.Writer, t.Status)
case *mastodon.NotificationEvent:
s.displayStatus(c.App.Writer, t.Notification.Status)
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
fmt.Fprintln(c.App.Writer, t.Error())
color.Set(color.Reset)
s.displayError(c.App.Writer, t)
}
}
}

View File

@ -2,22 +2,11 @@ package main
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
func acct(acct, host string) string {
if !strings.Contains(acct, "@") {
acct += "@" + host
}
return acct
}
func cmdTimeline(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
config := c.App.Metadata["config"].(*mastodon.Config)
@ -25,27 +14,9 @@ func cmdTimeline(c *cli.Context) error {
if err != nil {
return err
}
u, err := url.Parse(config.Server)
if err != nil {
return err
}
s := newScreen(config)
for i := len(timeline) - 1; i >= 0; i-- {
t := timeline[i]
if t.Reblog != nil {
color.Set(color.FgHiRed)
fmt.Fprint(c.App.Writer, acct(t.Account.Acct, u.Host))
color.Set(color.Reset)
fmt.Fprint(c.App.Writer, " rebloged ")
color.Set(color.FgHiBlue)
fmt.Fprintln(c.App.Writer, acct(t.Reblog.Account.Acct, u.Host))
fmt.Fprintln(c.App.Writer, textContent(t.Reblog.Content))
color.Set(color.Reset)
} else {
color.Set(color.FgHiRed)
fmt.Fprintln(c.App.Writer, acct(t.Account.Acct, u.Host))
color.Set(color.Reset)
fmt.Fprintln(c.App.Writer, textContent(t.Content))
}
s.displayStatus(c.App.Writer, timeline[i])
}
return nil
}

View File

@ -6,12 +6,15 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/mattn/go-tty"
"github.com/urfave/cli"
@ -259,6 +262,50 @@ func makeApp() *cli.App {
return app
}
func acct(acct, host string) string {
if !strings.Contains(acct, "@") {
acct += "@" + host
}
return acct
}
type screen struct {
host string
}
func newScreen(config *mastodon.Config) *screen {
var host string
u, err := url.Parse(config.Server)
if err == nil {
host = u.Host
}
return &screen{host}
}
func (s *screen) displayError(w io.Writer, e error) {
color.Set(color.FgYellow)
fmt.Fprintln(w, e.Error())
color.Set(color.Reset)
}
func (s *screen) displayStatus(w io.Writer, t *mastodon.Status) {
if t.Reblog != nil {
color.Set(color.FgHiRed)
fmt.Fprint(w, acct(t.Account.Acct, s.host))
color.Set(color.Reset)
fmt.Fprint(w, " reblogged ")
color.Set(color.FgHiBlue)
fmt.Fprintln(w, acct(t.Reblog.Account.Acct, s.host))
fmt.Fprintln(w, textContent(t.Reblog.Content))
color.Set(color.Reset)
} else {
color.Set(color.FgHiRed)
fmt.Fprintln(w, acct(t.Account.Acct, s.host))
color.Set(color.Reset)
fmt.Fprintln(w, textContent(t.Content))
}
}
func run() int {
app := makeApp()

View File

@ -20,7 +20,9 @@ type UpdateEvent struct {
func (e *UpdateEvent) event() {}
// NotificationEvent is struct for passing notification event to app.
type NotificationEvent struct{}
type NotificationEvent struct {
Notification *Notification `json:"notification"`
}
func (e *NotificationEvent) event() {}
@ -61,7 +63,17 @@ func handleReader(ctx context.Context, q chan Event, r io.Reader) error {
q <- &UpdateEvent{&status}
}
case "notification":
var notification Notification
err := json.Unmarshal([]byte(token[1]), &notification)
if err == nil {
q <- &NotificationEvent{&notification}
}
case "delete":
var id int64
err := json.Unmarshal([]byte(token[1]), &id)
if err == nil {
q <- &DeleteEvent{id}
}
}
default:
}