display status/reblog in stream
parent
f40839b98d
commit
eb1c1cf0ae
|
@ -4,12 +4,10 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/mattn/go-mastodon"
|
"github.com/mattn/go-mastodon"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
@ -25,7 +23,10 @@ type SimpleJSON struct {
|
||||||
func cmdStream(c *cli.Context) error {
|
func cmdStream(c *cli.Context) error {
|
||||||
asJSON := c.Bool("json")
|
asJSON := c.Bool("json")
|
||||||
asSimpleJSON := c.Bool("simplejson")
|
asSimpleJSON := c.Bool("simplejson")
|
||||||
|
|
||||||
client := c.App.Metadata["client"].(*mastodon.Client)
|
client := c.App.Metadata["client"].(*mastodon.Client)
|
||||||
|
config := c.App.Metadata["config"].(*mastodon.Config)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
sc := make(chan os.Signal, 1)
|
sc := make(chan os.Signal, 1)
|
||||||
|
@ -54,6 +55,8 @@ func cmdStream(c *cli.Context) error {
|
||||||
cancel()
|
cancel()
|
||||||
close(q)
|
close(q)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
s := newScreen(config)
|
||||||
for e := range q {
|
for e := range q {
|
||||||
if asJSON {
|
if asJSON {
|
||||||
json.NewEncoder(c.App.Writer).Encode(e)
|
json.NewEncoder(c.App.Writer).Encode(e)
|
||||||
|
@ -70,14 +73,11 @@ func cmdStream(c *cli.Context) error {
|
||||||
} else {
|
} else {
|
||||||
switch t := e.(type) {
|
switch t := e.(type) {
|
||||||
case *mastodon.UpdateEvent:
|
case *mastodon.UpdateEvent:
|
||||||
color.Set(color.FgHiRed)
|
s.displayStatus(c.App.Writer, t.Status)
|
||||||
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
|
case *mastodon.NotificationEvent:
|
||||||
color.Set(color.Reset)
|
s.displayStatus(c.App.Writer, t.Notification.Status)
|
||||||
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
|
|
||||||
case *mastodon.ErrorEvent:
|
case *mastodon.ErrorEvent:
|
||||||
color.Set(color.FgYellow)
|
s.displayError(c.App.Writer, t)
|
||||||
fmt.Fprintln(c.App.Writer, t.Error())
|
|
||||||
color.Set(color.Reset)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,22 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/mattn/go-mastodon"
|
"github.com/mattn/go-mastodon"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func acct(acct, host string) string {
|
|
||||||
if !strings.Contains(acct, "@") {
|
|
||||||
acct += "@" + host
|
|
||||||
}
|
|
||||||
return acct
|
|
||||||
}
|
|
||||||
|
|
||||||
func cmdTimeline(c *cli.Context) error {
|
func cmdTimeline(c *cli.Context) error {
|
||||||
client := c.App.Metadata["client"].(*mastodon.Client)
|
client := c.App.Metadata["client"].(*mastodon.Client)
|
||||||
config := c.App.Metadata["config"].(*mastodon.Config)
|
config := c.App.Metadata["config"].(*mastodon.Config)
|
||||||
|
@ -25,27 +14,9 @@ func cmdTimeline(c *cli.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
u, err := url.Parse(config.Server)
|
s := newScreen(config)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for i := len(timeline) - 1; i >= 0; i-- {
|
for i := len(timeline) - 1; i >= 0; i-- {
|
||||||
t := timeline[i]
|
s.displayStatus(c.App.Writer, 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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,15 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/mattn/go-mastodon"
|
"github.com/mattn/go-mastodon"
|
||||||
"github.com/mattn/go-tty"
|
"github.com/mattn/go-tty"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
@ -259,6 +262,50 @@ func makeApp() *cli.App {
|
||||||
return 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 {
|
func run() int {
|
||||||
app := makeApp()
|
app := makeApp()
|
||||||
|
|
||||||
|
|
14
streaming.go
14
streaming.go
|
@ -20,7 +20,9 @@ type UpdateEvent struct {
|
||||||
func (e *UpdateEvent) event() {}
|
func (e *UpdateEvent) event() {}
|
||||||
|
|
||||||
// NotificationEvent is struct for passing notification event to app.
|
// NotificationEvent is struct for passing notification event to app.
|
||||||
type NotificationEvent struct{}
|
type NotificationEvent struct {
|
||||||
|
Notification *Notification `json:"notification"`
|
||||||
|
}
|
||||||
|
|
||||||
func (e *NotificationEvent) event() {}
|
func (e *NotificationEvent) event() {}
|
||||||
|
|
||||||
|
@ -61,7 +63,17 @@ func handleReader(ctx context.Context, q chan Event, r io.Reader) error {
|
||||||
q <- &UpdateEvent{&status}
|
q <- &UpdateEvent{&status}
|
||||||
}
|
}
|
||||||
case "notification":
|
case "notification":
|
||||||
|
var notification Notification
|
||||||
|
err := json.Unmarshal([]byte(token[1]), ¬ification)
|
||||||
|
if err == nil {
|
||||||
|
q <- &NotificationEvent{¬ification}
|
||||||
|
}
|
||||||
case "delete":
|
case "delete":
|
||||||
|
var id int64
|
||||||
|
err := json.Unmarshal([]byte(token[1]), &id)
|
||||||
|
if err == nil {
|
||||||
|
q <- &DeleteEvent{id}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue