calculate polling rate and add manual polling
This commit is contained in:
parent
61b2f6345b
commit
4213730506
2 changed files with 57 additions and 10 deletions
20
display.go
20
display.go
|
|
@ -50,6 +50,10 @@ func (app *App) Redraw() {
|
|||
|
||||
if app.conn != nil {
|
||||
app.vx.SetTitle(fmt.Sprintf("%s:%s", app.host, app.port))
|
||||
rateString := "n/a"
|
||||
if app.rate != 0 {
|
||||
rateString = app.rate.String()
|
||||
}
|
||||
app.w.title.PrintTruncate(0,
|
||||
vaxis.Segment{
|
||||
Text: "• ",
|
||||
|
|
@ -67,7 +71,15 @@ func (app *App) Redraw() {
|
|||
Text: app.port,
|
||||
Style: titlebarStyle,
|
||||
},
|
||||
vaxis.Segment{
|
||||
Text: fmt.Sprintf(" | %s", rateString),
|
||||
},
|
||||
)
|
||||
|
||||
// let the widgets draw themselves
|
||||
app.pager.Layout()
|
||||
app.pager.Draw(app.w.log)
|
||||
app.input.Draw(app.w.input)
|
||||
} else {
|
||||
app.vx.SetTitle("nanite (disconnected)")
|
||||
app.w.title.PrintTruncate(0,
|
||||
|
|
@ -77,11 +89,6 @@ func (app *App) Redraw() {
|
|||
},
|
||||
)
|
||||
}
|
||||
|
||||
// let the widgets draw themselves
|
||||
app.pager.Layout()
|
||||
app.pager.Draw(app.w.log)
|
||||
app.input.Draw(app.w.input)
|
||||
app.vx.Render()
|
||||
}
|
||||
|
||||
|
|
@ -107,6 +114,9 @@ func (app *App) HandleTerminalEvent(ev vaxis.Event) {
|
|||
app.pager.ScrollUp()
|
||||
case ev.MatchString("down"):
|
||||
app.pager.ScrollDown()
|
||||
case ev.MatchString("ctrl+p"):
|
||||
app.AppendSystemMessage("polling for new messages")
|
||||
app.outgoing <- Poll(app.last)
|
||||
case ev.MatchString("ctrl+l"):
|
||||
app.Redraw()
|
||||
app.vx.Refresh()
|
||||
|
|
|
|||
47
main.go
47
main.go
|
|
@ -8,6 +8,7 @@ import (
|
|||
"math"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -31,6 +32,28 @@ var CommandMap = map[string]func(*App, string){
|
|||
app.AppendSystemMessage("your nickname is now %s", app.nick)
|
||||
}
|
||||
},
|
||||
"poll": func(app *App, rest string) {
|
||||
if rest == "" {
|
||||
app.AppendSystemMessage("polling for new messages")
|
||||
app.outgoing <- Poll(app.last)
|
||||
} else {
|
||||
num, err := strconv.Atoi(rest)
|
||||
if err != nil {
|
||||
app.AppendSystemMessage("invalid number %s", rest)
|
||||
} else {
|
||||
if num == 0 {
|
||||
app.ticker.Stop()
|
||||
app.rate = 0
|
||||
app.AppendSystemMessage("disabled automatic polling")
|
||||
} else {
|
||||
app.rate = time.Second * time.Duration(num)
|
||||
app.ticker.Stop()
|
||||
app.ticker = time.NewTicker(app.rate)
|
||||
app.AppendSystemMessage("polling every %s", app.rate.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"me": func(app *App, rest string) {
|
||||
msg := fmt.Sprintf("%s %s", app.nick, rest)
|
||||
app.AppendMessage(msg)
|
||||
|
|
@ -55,6 +78,7 @@ type App struct {
|
|||
|
||||
nick string
|
||||
last int
|
||||
rate time.Duration
|
||||
ticker *time.Ticker
|
||||
|
||||
incoming chan IncomingEvent
|
||||
|
|
@ -84,10 +108,22 @@ func (app *App) Connect(host, port string) (err error) {
|
|||
app.incoming = make(chan IncomingEvent)
|
||||
app.outgoing = make(chan OutgoingEvent, 256)
|
||||
app.error = make(chan error)
|
||||
app.ticker = time.NewTicker(1 * time.Second)
|
||||
|
||||
// Calculate latency
|
||||
now := time.Now()
|
||||
_, err = app.Poll(0)
|
||||
if err != nil {
|
||||
app.Disconnect()
|
||||
return err
|
||||
}
|
||||
delta := time.Since(now).Round(time.Second)
|
||||
delta = min(max(time.Second, delta*3/2), 5*time.Second)
|
||||
|
||||
app.rate = delta
|
||||
app.ticker = time.NewTicker(delta)
|
||||
|
||||
go func() {
|
||||
app.Last(50)
|
||||
app.Last(20)
|
||||
for {
|
||||
select {
|
||||
case ev := <-app.outgoing:
|
||||
|
|
@ -167,14 +203,15 @@ func main() {
|
|||
app.ctx, app.stop = context.WithCancel(context.Background())
|
||||
defer app.stop()
|
||||
|
||||
app.InitUI()
|
||||
app.Redraw()
|
||||
defer app.FinishUI()
|
||||
|
||||
if err := app.Connect(args[1], port); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer app.Disconnect()
|
||||
|
||||
app.InitUI()
|
||||
defer app.FinishUI()
|
||||
|
||||
app.SetNick("wolfdog")
|
||||
|
||||
for {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue