From d69f375a57ad5c901f538c8d184727119e70e256 Mon Sep 17 00:00:00 2001 From: Lobo Date: Sun, 12 Oct 2025 14:01:07 -0300 Subject: [PATCH] rendering changes, no more "dirty" --- display.go | 87 ++++++++++++++++++++---------------------- event.go | 1 - main.go | 3 -- widgets/pager/pager.go | 7 +--- 4 files changed, 43 insertions(+), 55 deletions(-) diff --git a/display.go b/display.go index d056431..c870009 100644 --- a/display.go +++ b/display.go @@ -29,8 +29,6 @@ func (app *App) FinishUI() { } func (app *App) resize() { - app.dirty = true - win := app.vx.Window() app.w.log = win.New(0, 1, win.Width, win.Height-2) app.w.title = win.New(0, 0, win.Width, 1) @@ -39,13 +37,9 @@ func (app *App) resize() { } func (app *App) Redraw() { - if !app.dirty { - return - } - app.dirty = false app.w.title.Clear() - titlebarStyle := vaxis.Style{Attribute: vaxis.AttrBold} + titleStyle := vaxis.Style{Attribute: vaxis.AttrBold} delimiterStyle := vaxis.Style{Attribute: vaxis.AttrDim} if app.conn != nil { @@ -59,7 +53,7 @@ func (app *App) Redraw() { segments := []vaxis.Segment{ {Text: "• "}, - {Text: titleString, Style: titlebarStyle}, + {Text: titleString, Style: titleStyle}, {Text: " │ ", Style: delimiterStyle}, {Text: fmt.Sprintf("↻ %s", rateString)}, } @@ -79,16 +73,52 @@ func (app *App) Redraw() { app.vx.SetTitle("nanite (disconnected)") app.w.title.PrintTruncate(0, vaxis.Segment{Text: "✕ "}, - vaxis.Segment{Text: "disconnected", Style: titlebarStyle}, + vaxis.Segment{Text: "disconnected", Style: titleStyle}, ) } app.vx.Render() } -func (app *App) HandleTerminalEvent(ev vaxis.Event) { - app.dirty = true +func (app *App) submitTextInput() { + if len(app.input.Characters()) == 0 { + return + } + if app.input.Characters()[0].Grapheme == "/" { + name, rest, _ := strings.Cut(app.input.String()[1:], " ") + if cmd, ok := CommandMap[name]; ok { + cmd(app, rest) + } else { + app.AppendSystemMessage("unknown command \"%s\"", name) + } + } else { + message := fmt.Sprintf("%s: %s", app.nick, app.input.String()) + app.AppendMessage(message) + app.outgoing <- Message(message) + app.outgoing <- Stat("") + } + + app.input.SetContent("") +} + +func (app *App) HandleTerminalEvent(ev vaxis.Event) { switch ev := ev.(type) { + case vaxis.Key: + switch ev.String() { + case "Up": + app.pager.ScrollUp() + case "Down": + app.pager.ScrollDown() + case "Enter": + app.submitTextInput() + case "Ctrl+p": + app.outgoing <- ManualPoll(app.last) + case "Ctrl+l": + app.Redraw() + app.vx.Refresh() + case "Ctrl+c": + app.stop() + } case vaxis.Mouse: switch ev.Button { case vaxis.MouseWheelUp: @@ -98,41 +128,6 @@ func (app *App) HandleTerminalEvent(ev vaxis.Event) { } case vaxis.Resize: app.resize() - case vaxis.Key: - if ev.MatchString("ctrl+c") { - app.stop() - } - switch { - case ev.MatchString("up"): - app.pager.ScrollUp() - case ev.MatchString("down"): - app.pager.ScrollDown() - case ev.MatchString("ctrl+p"): - app.outgoing <- ManualPoll(app.last) - case ev.MatchString("ctrl+l"): - app.Redraw() - app.vx.Refresh() - app.dirty = false - case ev.MatchString("enter"): - if len(app.input.Characters()) == 0 { - break - } - if app.input.Characters()[0].Grapheme == "/" { - name, rest, _ := strings.Cut(app.input.String()[1:], " ") - if cmd, ok := CommandMap[name]; ok { - cmd(app, rest) - } else { - app.AppendSystemMessage("unknown command \"%s\"", name) - } - } else { - message := fmt.Sprintf("%s: %s", app.nick, app.input.String()) - app.AppendMessage(message) - app.outgoing <- Message(message) - app.outgoing <- Stat("") - } - - app.input.SetContent("") - } } app.input.Update(ev) diff --git a/event.go b/event.go index 3657cbc..aba052f 100644 --- a/event.go +++ b/event.go @@ -76,7 +76,6 @@ type Stat string func (data Stat) HandleIncoming(app *App) { app.stats = string(data) - app.dirty = true } func (_ Stat) HandleOutgoing(app *App) error { res, err := app.Stat() diff --git a/main.go b/main.go index c582c6b..cc1f0dc 100644 --- a/main.go +++ b/main.go @@ -90,7 +90,6 @@ type App struct { } pager *pager.Model input *textinput.Model - dirty bool } func (app *App) Connect(host, port string) (err error) { @@ -168,7 +167,6 @@ func (app *App) AppendMessage(data string) { ) app.last += 1 app.pager.Offset = math.MaxInt - app.dirty = true } func (app *App) AppendSystemMessage(format string, args ...any) { @@ -179,7 +177,6 @@ func (app *App) AppendSystemMessage(format string, args ...any) { vaxis.Segment{Text: "\n"}, ) app.pager.Offset = math.MaxInt - app.dirty = true } func (app *App) SetNick(nick string) { diff --git a/widgets/pager/pager.go b/widgets/pager/pager.go index 439acbb..662bd41 100644 --- a/widgets/pager/pager.go +++ b/widgets/pager/pager.go @@ -111,17 +111,14 @@ func (m *Model) layoutSlow() { total += char.Width } - switch { - case total > cols: - case total+col > cols: + if total > cols || total+col > cols { m.lines = append(m.lines, l) l = &line{} col = 0 - default: } for _, char := range chars { - if uniseg.HasTrailingLineBreakInString(char.Grapheme) { + if uniseg.HasTrailingLineBreakInString(char.Grapheme) && col != 0 { m.lines = append(m.lines, l) l = &line{} col = 0