calculate polling rate and add manual polling

This commit is contained in:
Lobo 2025-10-12 11:32:14 -03:00
parent 61b2f6345b
commit 4213730506
2 changed files with 57 additions and 10 deletions

View file

@ -50,6 +50,10 @@ func (app *App) Redraw() {
if app.conn != nil { if app.conn != nil {
app.vx.SetTitle(fmt.Sprintf("%s:%s", app.host, app.port)) 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, app.w.title.PrintTruncate(0,
vaxis.Segment{ vaxis.Segment{
Text: "• ", Text: "• ",
@ -67,7 +71,15 @@ func (app *App) Redraw() {
Text: app.port, Text: app.port,
Style: titlebarStyle, 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 { } else {
app.vx.SetTitle("nanite (disconnected)") app.vx.SetTitle("nanite (disconnected)")
app.w.title.PrintTruncate(0, 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() app.vx.Render()
} }
@ -107,6 +114,9 @@ func (app *App) HandleTerminalEvent(ev vaxis.Event) {
app.pager.ScrollUp() app.pager.ScrollUp()
case ev.MatchString("down"): case ev.MatchString("down"):
app.pager.ScrollDown() app.pager.ScrollDown()
case ev.MatchString("ctrl+p"):
app.AppendSystemMessage("polling for new messages")
app.outgoing <- Poll(app.last)
case ev.MatchString("ctrl+l"): case ev.MatchString("ctrl+l"):
app.Redraw() app.Redraw()
app.vx.Refresh() app.vx.Refresh()

47
main.go
View file

@ -8,6 +8,7 @@ import (
"math" "math"
"net" "net"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
@ -31,6 +32,28 @@ var CommandMap = map[string]func(*App, string){
app.AppendSystemMessage("your nickname is now %s", app.nick) 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) { "me": func(app *App, rest string) {
msg := fmt.Sprintf("%s %s", app.nick, rest) msg := fmt.Sprintf("%s %s", app.nick, rest)
app.AppendMessage(msg) app.AppendMessage(msg)
@ -55,6 +78,7 @@ type App struct {
nick string nick string
last int last int
rate time.Duration
ticker *time.Ticker ticker *time.Ticker
incoming chan IncomingEvent incoming chan IncomingEvent
@ -84,10 +108,22 @@ func (app *App) Connect(host, port string) (err error) {
app.incoming = make(chan IncomingEvent) app.incoming = make(chan IncomingEvent)
app.outgoing = make(chan OutgoingEvent, 256) app.outgoing = make(chan OutgoingEvent, 256)
app.error = make(chan error) 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() { go func() {
app.Last(50) app.Last(20)
for { for {
select { select {
case ev := <-app.outgoing: case ev := <-app.outgoing:
@ -167,14 +203,15 @@ func main() {
app.ctx, app.stop = context.WithCancel(context.Background()) app.ctx, app.stop = context.WithCancel(context.Background())
defer app.stop() defer app.stop()
app.InitUI()
app.Redraw()
defer app.FinishUI()
if err := app.Connect(args[1], port); err != nil { if err := app.Connect(args[1], port); err != nil {
panic(err) panic(err)
} }
defer app.Disconnect() defer app.Disconnect()
app.InitUI()
defer app.FinishUI()
app.SetNick("wolfdog") app.SetNick("wolfdog")
for { for {