From 42137305063fb6ee57a4be1491eee82e2a588942 Mon Sep 17 00:00:00 2001 From: Lobo Date: Sun, 12 Oct 2025 11:32:14 -0300 Subject: [PATCH] calculate polling rate and add manual polling --- display.go | 20 +++++++++++++++----- main.go | 47 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/display.go b/display.go index 7401408..1235585 100644 --- a/display.go +++ b/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() diff --git a/main.go b/main.go index 1787ab5..8ef195a 100644 --- a/main.go +++ b/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 {