separate command
This commit is contained in:
parent
094a888b1d
commit
209a15a4d7
4 changed files with 100 additions and 76 deletions
42
cmd/mstdn/cmd_stream.go
Normal file
42
cmd/mstdn/cmd_stream.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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())
|
||||
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)
|
||||
fmt.Println(t.Status.Account.Username)
|
||||
color.Set(color.Reset)
|
||||
fmt.Println(textContent(t.Status.Content))
|
||||
case *mastodon.ErrorEvent:
|
||||
color.Set(color.FgYellow)
|
||||
fmt.Println(t.Error())
|
||||
color.Set(color.Reset)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue