From 77f8335e6bbd45a72ef891ffe77f6ceebd8ca868 Mon Sep 17 00:00:00 2001 From: vvvomit Date: Thu, 29 Oct 2026 16:38:37 +0330 Subject: [PATCH] Rooms: names can't garble the screen, and no screen leaves rows behind A room or user name with a tab, a newline, an escape or some emoji was drawn wider than measured, so the terminal wrapped and scrolled, and going on to Users kept Rooms' header and keys on screen. Names from Synapse and log lines are made printable, cutting to fit counts screen cells (CJK and emoji take two), the coloured title keeps an emoji whole, and changing screens repaints the terminal. Co-Authored-By: Claude Opus 5.5 --- go.mod | 2 +- internal/deploy/deploy.go | 2 +- internal/deploy/rooms.go | 20 +++++++++++++++++++- internal/deploy/users.go | 2 +- internal/deploy/users_test.go | 13 +++++++++++++ internal/ui/app.go | 4 +++- internal/ui/theme.go | 28 +++++++++++++++++----------- 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 1b1048f..6fe5439 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/lrstanley/bubblezone v1.0.0 github.com/lucasb-eyer/go-colorful v1.3.0 github.com/muesli/termenv v0.16.0 + github.com/rivo/uniseg v0.4.7 gopkg.in/yaml.v3 v3.0.1 ) @@ -29,7 +30,6 @@ require ( github.com/mattn/go-runewidth v0.0.19 // indirect github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/cancelreader v0.2.2 // indirect - github.com/rivo/uniseg v0.4.7 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect golang.org/x/sys v0.38.0 // indirect golang.org/x/text v0.24.0 // indirect diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index cca204b..1cba9ef 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -773,7 +773,7 @@ func FollowLogs(ctx context.Context, c *config.Config, unit string) (<-chan stri sc.Buffer(make([]byte, 64*1024), 1024*1024) var last string for sc.Scan() { - last = sc.Text() + last = Printable(sc.Text()) select { case ch <- last: case <-ctx.Done(): diff --git a/internal/deploy/rooms.go b/internal/deploy/rooms.go index 5d12374..4795375 100644 --- a/internal/deploy/rooms.go +++ b/internal/deploy/rooms.go @@ -5,7 +5,9 @@ import ( "encoding/json" "fmt" "net/url" + "strings" "time" + "unicode" "interzone/internal/config" ) @@ -30,6 +32,22 @@ func (r Room) Title() string { return r.ID } +// Printable makes text anyone on Matrix can set safe to draw: control +// characters (escapes, tabs, newlines) become spaces, and zero-width joiners +// go: terminals disagree on how wide a joined emoji is, and a line drawn +// wider than planned wraps and garbles the screen. +func Printable(s string) string { + return strings.Map(func(r rune) rune { + switch { + case r == '\u200d': + return -1 + case unicode.IsControl(r): + return ' ' + } + return r + }, s) +} + // ListRooms returns every room on the server, biggest first. func ListRooms(ctx context.Context, c *config.Config, login *AdminLogin) ([]Room, error) { out, err := adminCall(ctx, c, login, map[string]any{"op": "rooms"}) @@ -66,7 +84,7 @@ func parseRooms(out []byte) ([]Room, error) { } rooms := make([]Room, 0, len(v.Rooms)) for _, r := range v.Rooms { - rooms = append(rooms, Room{ID: r.ID, Name: str(r.Name), Alias: str(r.Alias), Creator: r.Creator, + rooms = append(rooms, Room{ID: r.ID, Name: Printable(str(r.Name)), Alias: Printable(str(r.Alias)), Creator: Printable(r.Creator), Members: r.Members, Local: r.Local, Events: r.Events, Public: r.Public, Encrypted: str(r.Encrypted) != "", Federated: r.Federated, Blocked: r.Blocked}) } diff --git a/internal/deploy/users.go b/internal/deploy/users.go index 41d72c9..d25301b 100644 --- a/internal/deploy/users.go +++ b/internal/deploy/users.go @@ -345,7 +345,7 @@ func parseUsers(out []byte, admins map[string]bool) (*UserList, error) { if admins != nil { admin = admins[localpart(u.Name)] } - mu := MatrixUser{ID: u.Name, DisplayName: u.DisplayName, Admin: admin, + mu := MatrixUser{ID: u.Name, DisplayName: Printable(u.DisplayName), Admin: admin, Deactivated: truthy(u.Deactivated), Created: msTime(u.Created), LastSeen: msTime(u.LastSeen), Bridge: u.Appservice, Bot: u.Bot, Rooms: count(u.Rooms), Devices: count(u.Devices), Emails: u.Threepids, Type: u.UserType, Locked: truthy(u.Locked), ShadowBanned: truthy(u.ShadowBanned), Avatar: u.Avatar != ""} diff --git a/internal/deploy/users_test.go b/internal/deploy/users_test.go index 065e0d7..ff85bb8 100644 --- a/internal/deploy/users_test.go +++ b/internal/deploy/users_test.go @@ -36,3 +36,16 @@ func TestParseUsers(t *testing.T) { t.Errorf("the puppet: %+v", puppet) } } + +func TestPrintable(t *testing.T) { + for in, want := range map[string]string{ + "tab\there\nnext": "tab here next", + "esc \x1b[2Jred": "esc [2Jred", + "👨‍👩 ✌️ ok": "👨👩 ✌️ ok", + "گروه 日本": "گروه 日本", + } { + if got := Printable(in); got != want { + t.Errorf("Printable(%q) = %q, want %q", in, got, want) + } + } +} diff --git a/internal/ui/app.go b/internal/ui/app.go index dba8afc..8582df4 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -151,7 +151,9 @@ func (a *App) jump(n int) tea.Cmd { if sec := a.section(); sec != was { a.openSection(sec) } - return a.cur().enter(a.cfg) + // A repaint: a line the terminal drew wider than measured (some emoji) + // would otherwise leave the last screen's rows behind. + return tea.Batch(a.cur().enter(a.cfg), tea.ClearScreen) } // ownedMsg is a result for one screen, named by owner. diff --git a/internal/ui/theme.go b/internal/ui/theme.go index f4f16f9..f0f9c1f 100644 --- a/internal/ui/theme.go +++ b/internal/ui/theme.go @@ -6,8 +6,10 @@ import ( "strings" "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/ansi" "github.com/lucasb-eyer/go-colorful" "github.com/muesli/termenv" + "github.com/rivo/uniseg" "interzone/internal/config" ) @@ -170,28 +172,33 @@ func gradientStops() []string { return []string{pick(cPurple), pick(cPink), pick(cMint)} } -// gradient colours each rune of s along the theme's ramp. +// gradient colours each character of s along the theme's ramp. It steps by +// grapheme, so an emoji isn't split from its variation selector: apart, +// they'd be measured narrower than the terminal draws them. func gradient(s string) string { stops := gradientStops() - runes := []rune(s) + var chars []string + for g := uniseg.NewGraphemes(s); g.Next(); { + chars = append(chars, g.Str()) + } n := 0 - for _, r := range runes { - if r != ' ' && r != '\n' { + for _, r := range chars { + if r != " " && r != "\n" { n++ } } var b strings.Builder i := 0 - for _, r := range runes { - if r == ' ' || r == '\n' { - b.WriteRune(r) + for _, r := range chars { + if r == " " || r == "\n" { + b.WriteString(r) continue } t := 0.0 if n > 1 { t = float64(i) / float64(n-1) } - b.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color(ramp(stops, t))).Render(string(r))) + b.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color(ramp(stops, t))).Render(r)) i++ } return b.String() @@ -263,11 +270,10 @@ func truncate(s string, w int) string { if w <= 1 { return "" } - r := []rune(s) - if len(r) <= w { + if ansi.StringWidth(s) <= w { return s } - return string(r[:w-1]) + "…" + return ansi.Truncate(s, w, "…") // by width: CJK and emoji take two cells } // truncateMid shortens s to w runes by cutting its middle, so names that