shithub: hell

ref: 22c76389a787fb39769680882ef5961819429d05
dir: /hellclient.go/

View raw version
package main

import (
	"io"
	"strings"
	"sync"
	"fmt"

	"github.com/chzyer/readline"
	"github.com/mattn/go-mastodon"
)

var (
	ErrInterrupt = readline.ErrInterrupt
	EOF          = io.EOF
)

type Hellclient struct {
	//if you're gonna touch or read anything here lock the mutex with hc.lock()
	isPaused bool
	rl       *readline.Instance
	block    sync.Mutex

	homeMap      map[string]*mastodon.Status
	debugMap     map[string]interface{}
	actionBuffer []func()
}

func NewHellclient() (*Hellclient, error) {
	rl, err := readline.New("> ")
	homeMap := make(map[string]*mastodon.Status)
	debugMap := make(map[string]interface{})
	if err != nil {
		return nil, err
	}
	return &Hellclient{rl: rl, homeMap: homeMap, debugMap: debugMap, isPaused: false}, nil
}

func (hc *Hellclient) updatePrompt() {
	var sb strings.Builder
	if hc.isPaused {
		sb.WriteString("STREAMING PAUSED ")
	}
	sb.WriteString("> ")
	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) {
		fmt.Println("Output resumed")
		for _, action := range hc.actionBuffer {
			action()
		}
		hc.actionBuffer = nil
	}
}

func (hc *Hellclient) lock() {
	hc.block.Lock()
}

func (hc *Hellclient) unlock() {
	hc.block.Unlock()
}