rendering changes, no more "dirty"

This commit is contained in:
Lobo 2025-10-12 14:01:07 -03:00
parent 13d57534a6
commit d69f375a57
4 changed files with 43 additions and 55 deletions

View file

@ -29,8 +29,6 @@ func (app *App) FinishUI() {
} }
func (app *App) resize() { func (app *App) resize() {
app.dirty = true
win := app.vx.Window() win := app.vx.Window()
app.w.log = win.New(0, 1, win.Width, win.Height-2) app.w.log = win.New(0, 1, win.Width, win.Height-2)
app.w.title = win.New(0, 0, win.Width, 1) app.w.title = win.New(0, 0, win.Width, 1)
@ -39,13 +37,9 @@ func (app *App) resize() {
} }
func (app *App) Redraw() { func (app *App) Redraw() {
if !app.dirty {
return
}
app.dirty = false
app.w.title.Clear() app.w.title.Clear()
titlebarStyle := vaxis.Style{Attribute: vaxis.AttrBold} titleStyle := vaxis.Style{Attribute: vaxis.AttrBold}
delimiterStyle := vaxis.Style{Attribute: vaxis.AttrDim} delimiterStyle := vaxis.Style{Attribute: vaxis.AttrDim}
if app.conn != nil { if app.conn != nil {
@ -59,7 +53,7 @@ func (app *App) Redraw() {
segments := []vaxis.Segment{ segments := []vaxis.Segment{
{Text: "• "}, {Text: "• "},
{Text: titleString, Style: titlebarStyle}, {Text: titleString, Style: titleStyle},
{Text: " │ ", Style: delimiterStyle}, {Text: " │ ", Style: delimiterStyle},
{Text: fmt.Sprintf("↻ %s", rateString)}, {Text: fmt.Sprintf("↻ %s", rateString)},
} }
@ -79,16 +73,52 @@ func (app *App) Redraw() {
app.vx.SetTitle("nanite (disconnected)") app.vx.SetTitle("nanite (disconnected)")
app.w.title.PrintTruncate(0, app.w.title.PrintTruncate(0,
vaxis.Segment{Text: "✕ "}, vaxis.Segment{Text: "✕ "},
vaxis.Segment{Text: "disconnected", Style: titlebarStyle}, vaxis.Segment{Text: "disconnected", Style: titleStyle},
) )
} }
app.vx.Render() app.vx.Render()
} }
func (app *App) HandleTerminalEvent(ev vaxis.Event) { func (app *App) submitTextInput() {
app.dirty = true 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) { 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: case vaxis.Mouse:
switch ev.Button { switch ev.Button {
case vaxis.MouseWheelUp: case vaxis.MouseWheelUp:
@ -98,41 +128,6 @@ func (app *App) HandleTerminalEvent(ev vaxis.Event) {
} }
case vaxis.Resize: case vaxis.Resize:
app.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) app.input.Update(ev)

View file

@ -76,7 +76,6 @@ type Stat string
func (data Stat) HandleIncoming(app *App) { func (data Stat) HandleIncoming(app *App) {
app.stats = string(data) app.stats = string(data)
app.dirty = true
} }
func (_ Stat) HandleOutgoing(app *App) error { func (_ Stat) HandleOutgoing(app *App) error {
res, err := app.Stat() res, err := app.Stat()

View file

@ -90,7 +90,6 @@ type App struct {
} }
pager *pager.Model pager *pager.Model
input *textinput.Model input *textinput.Model
dirty bool
} }
func (app *App) Connect(host, port string) (err error) { func (app *App) Connect(host, port string) (err error) {
@ -168,7 +167,6 @@ func (app *App) AppendMessage(data string) {
) )
app.last += 1 app.last += 1
app.pager.Offset = math.MaxInt app.pager.Offset = math.MaxInt
app.dirty = true
} }
func (app *App) AppendSystemMessage(format string, args ...any) { func (app *App) AppendSystemMessage(format string, args ...any) {
@ -179,7 +177,6 @@ func (app *App) AppendSystemMessage(format string, args ...any) {
vaxis.Segment{Text: "\n"}, vaxis.Segment{Text: "\n"},
) )
app.pager.Offset = math.MaxInt app.pager.Offset = math.MaxInt
app.dirty = true
} }
func (app *App) SetNick(nick string) { func (app *App) SetNick(nick string) {

View file

@ -111,17 +111,14 @@ func (m *Model) layoutSlow() {
total += char.Width total += char.Width
} }
switch { if total > cols || total+col > cols {
case total > cols:
case total+col > cols:
m.lines = append(m.lines, l) m.lines = append(m.lines, l)
l = &line{} l = &line{}
col = 0 col = 0
default:
} }
for _, char := range chars { for _, char := range chars {
if uniseg.HasTrailingLineBreakInString(char.Grapheme) { if uniseg.HasTrailingLineBreakInString(char.Grapheme) && col != 0 {
m.lines = append(m.lines, l) m.lines = append(m.lines, l)
l = &line{} l = &line{}
col = 0 col = 0