ref: 00f57e46e617dd29b735f787060ae98f7007ed5b
dir: /hellclient.go/
package main
import (
"context"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/ergochat/readline"
"github.com/mattn/go-mastodon"
)
var (
ErrInterrupt = readline.ErrInterrupt
EOF = io.EOF
)
type postref struct {
prefix string
ref string
postmap map[string]*mastodon.Status
}
type Hellclient struct {
//if you're gonna touch or read anything here lock the mutex with hc.lock()
isPaused bool
rl *readline.Instance
client *mastodon.Client
currentuser *mastodon.Account
dispatch chan *mastodon.Toot
block sync.Mutex
recentpost *mastodon.Status
preferences *Hellprefs
//Global status map for status indexes
//Needs to be converted to a postref struct
homeMap map[string]*mastodon.Status
homeref *postref
//Contextual indexes for commands
ctxref *postref
urlMap map[string][]string
debugMap map[string]interface{}
actionBuffer []func()
}
type Hellprefs struct {
apidelay time.Duration
}
func NewHellclient() (*Hellclient, error) {
rl, err := readline.New("Hell> ")
rl.CaptureExitSignal()
if err != nil {
return nil, err
}
account, err := loadConfig()
if err != nil {
return nil, err
}
client := initClient(account)
currentuser, err := client.GetAccountCurrentUser(context.Background())
if err != nil {
return nil, err
}
dispatch := make(chan *mastodon.Toot, 15)
var hc Hellclient
defer func() {
//Got some stuff to do when we're done
hc.updatePrompt()
//start up post dispatcher
go hc.clientDispatch()
}()
homeMap := make(map[string]*mastodon.Status)
ctxref := &postref{
prefix: "?",
ref: "a",
postmap: homeMap,
}
homeref := &postref{
ref: "a",
postmap: homeMap,
}
debugMap := make(map[string]interface{})
urlMap := make(map[string][]string)
prefs := &Hellprefs{apidelay: time.Second * 3}
hc = Hellclient{rl: rl,
homeMap: homeMap,
ctxref: ctxref,
homeref: homeref,
debugMap: debugMap,
isPaused: false,
urlMap: urlMap,
client: client,
currentuser: currentuser,
dispatch: dispatch,
preferences: prefs,
}
return &hc, nil
}
func (hc *Hellclient) updatePrompt() {
var sb strings.Builder
unread, err := hc.client.GetUnreadNotifications(context.Background(), nil, nil, 0)
if err == nil {
sb.WriteString(fmt.Sprintf("ur:%v ", unread.Count))
}
if hc.isPaused {
sb.WriteString("STREAMING PAUSED ")
}
sb.WriteString("Hell> ")
hc.rl.SetPrompt(sb.String())
}
func (hc *Hellclient) pause(on bool) {
hc.isPaused = on
hc.updatePrompt()
hc.printPauseBuffer()
}
func (hc *Hellclient) togglepause() {
hc.isPaused = !hc.isPaused
hc.updatePrompt()
hc.printPauseBuffer()
}
func (hc *Hellclient) printPauseBuffer() {
if !hc.isPaused {
for _, action := range hc.actionBuffer {
action()
}
hc.actionBuffer = nil
}
}
func (hc *Hellclient) lock() {
hc.block.Lock()
}
func (hc *Hellclient) unlock() {
hc.block.Unlock()
}