go-mastodon/cmd/mstdn/cmd_stream.go

44 lines
890 B
Go
Raw Normal View History

2017-04-15 14:52:11 +02:00
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/fatih/color"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
func cmdStream(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
ctx, cancel := context.WithCancel(context.Background())
2017-04-16 16:38:53 +02:00
defer cancel()
2017-04-15 14:52:11 +02:00
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
q, err := client.StreamingPublic(ctx)
if err != nil {
return err
}
go func() {
<-sc
cancel()
close(q)
}()
for e := range q {
switch t := e.(type) {
case *mastodon.UpdateEvent:
color.Set(color.FgHiRed)
2017-04-16 16:04:31 +02:00
fmt.Fprintln(c.App.Writer, t.Status.Account.Username)
2017-04-15 14:52:11 +02:00
color.Set(color.Reset)
2017-04-16 16:04:31 +02:00
fmt.Fprintln(c.App.Writer, textContent(t.Status.Content))
2017-04-15 14:52:11 +02:00
case *mastodon.ErrorEvent:
color.Set(color.FgYellow)
2017-04-16 16:04:31 +02:00
fmt.Fprintln(c.App.Writer, t.Error())
2017-04-15 14:52:11 +02:00
color.Set(color.Reset)
}
}
return nil
}