From eb1c1cf0ae985ad92105398397b17aac18e4543e Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 19 Apr 2017 23:21:19 +0900 Subject: [PATCH] display status/reblog in stream --- cmd/mstdn/cmd_stream.go | 18 +++++++-------- cmd/mstdn/cmd_timeline.go | 33 ++------------------------- cmd/mstdn/main.go | 47 +++++++++++++++++++++++++++++++++++++++ streaming.go | 14 +++++++++++- 4 files changed, 71 insertions(+), 41 deletions(-) diff --git a/cmd/mstdn/cmd_stream.go b/cmd/mstdn/cmd_stream.go index 1c89acb..6109349 100644 --- a/cmd/mstdn/cmd_stream.go +++ b/cmd/mstdn/cmd_stream.go @@ -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) } } } diff --git a/cmd/mstdn/cmd_timeline.go b/cmd/mstdn/cmd_timeline.go index 99689f0..d634ebf 100644 --- a/cmd/mstdn/cmd_timeline.go +++ b/cmd/mstdn/cmd_timeline.go @@ -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 } diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index f266911..e25053b 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -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() diff --git a/streaming.go b/streaming.go index 2a5295d..0bff3e3 100644 --- a/streaming.go +++ b/streaming.go @@ -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]), ¬ification) + if err == nil { + q <- &NotificationEvent{¬ification} + } case "delete": + var id int64 + err := json.Unmarshal([]byte(token[1]), &id) + if err == nil { + q <- &DeleteEvent{id} + } } default: }