From bfdb51a452dcef126fc9e929a8836d9b3b77c420 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 13:44:40 +0000 Subject: [PATCH] Configuration: lines and shading right on a UTF-8 terminal with no locale MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bbs config took the console for code page 437 whenever the locale did not say UTF-8 (LANG unset, as under sudo or in a bare WSL), so a UTF-8 terminal showed every line, shadow and shade as a replacement character. It now asks the terminal: é written, the cursor's column read back. A terminal that does not answer gets a VT's line drawing on an xterm, a VT, screen or tmux (as modemmenu does), and code page 437 otherwise. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRVaGDpEUKj7r3JGhpU --- cmd/bbs/config.go | 22 +++++++++++++++++++++- internal/textui/term.go | 32 ++++++++++++++++++++++++++++++++ internal/textui/term_test.go | 20 ++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/cmd/bbs/config.go b/cmd/bbs/config.go index d77ba42..ffa976e 100644 --- a/cmd/bbs/config.go +++ b/cmd/bbs/config.go @@ -121,8 +121,8 @@ func configScreens(path string) { } t := textui.NewTerm(newTTY(os.Stdin, os.Stdout)) t.ANSI = os.Getenv("TERM") != "" && os.Getenv("TERM") != "dumb" - t.UTF8 = utf8Locale() t.Clean8 = true + localCharset(t) t.Idle = 24 * time.Hour if cols, rows, err := term.GetSize(out); err == nil && cols > 0 { t.Cols, t.Rows = cols, rows @@ -183,6 +183,26 @@ func configScreens(path string) { } } +// localCharset gives t the console's characters: UTF-8 where the locale +// says so, or where the terminal shows é as one column (a locale may be +// unset, as under sudo or in a bare WSL, on a terminal that is UTF-8); +// otherwise a VT's line drawing on an xterm, a VT, screen or tmux, and code +// page 437 on any other (the Linux console's, SyncTERM's). +func localCharset(t *textui.Term) { + t.UTF8 = utf8Locale() + if t.UTF8 || !t.ANSI { + return + } + if u, ok, err := t.DetectUTF8(500 * time.Millisecond); err == nil && ok && u { + t.UTF8 = true + return + } + switch tt := os.Getenv("TERM"); { + case strings.HasPrefix(tt, "xterm"), strings.HasPrefix(tt, "vt"), strings.HasPrefix(tt, "screen"), strings.HasPrefix(tt, "tmux"): + t.SetDEC(false) + } +} + // utf8Locale is whether the locale (LC_ALL, then LC_CTYPE, then LANG: the // first set) says UTF-8. func utf8Locale() bool { diff --git a/internal/textui/term.go b/internal/textui/term.go index b60d608..d715b34 100644 --- a/internal/textui/term.go +++ b/internal/textui/term.go @@ -907,6 +907,38 @@ func (t *Term) DetectANSI(timeout time.Duration) (bool, error) { return false, nil } +// DetectUTF8 writes é (two bytes in UTF-8) at the start of the line and +// asks where the cursor is: one column on is UTF-8, two is a terminal of +// single bytes. ok is false when the terminal does not answer within +// timeout. +func (t *Term) DetectUTF8(timeout time.Duration) (utf8, ok bool, err error) { + t.w.WriteString("\r\xc3\xa9\x1b[6n") + if err := t.Flush(); err != nil { + return false, false, err + } + var got []byte + end := time.Now().Add(timeout) + for time.Now().Before(end) && len(got) < 64 { + t.c.SetReadDeadline(end) + b, err := t.r.ReadByte() + t.c.SetReadDeadline(time.Time{}) + if err != nil { + if errors.Is(err, os.ErrDeadlineExceeded) { + break + } + return false, false, err + } + got = append(got, b) + if m := cprPattern.FindSubmatch(got); m != nil { + col, _ := strconv.Atoi(string(m[2])) + ok, utf8 = true, col == 2 + break + } + } + t.w.WriteString("\r \r") + return utf8, ok, t.Flush() +} + var ( daPattern = regexp.MustCompile(`\x1b\[\?([0-9;]*)c`) cellPattern = regexp.MustCompile(`\x1b\[6;(\d+);(\d+)t`) diff --git a/internal/textui/term_test.go b/internal/textui/term_test.go index 223b23a..2690cff 100644 --- a/internal/textui/term_test.go +++ b/internal/textui/term_test.go @@ -68,3 +68,23 @@ func TestCode(t *testing.T) { t.Errorf("ANSI: %q", got) } } + +// TestDetectUTF8: é one column on is UTF-8, two on is a byte terminal. +func TestDetectUTF8(t *testing.T) { + for _, c := range []struct { + answer string + utf8, ok bool + }{ + {"\x1b[5;2R", true, true}, + {"\x1b[5;3R", false, true}, + } { + kc := &keyConn{in: bytes.NewReader([]byte(c.answer))} + u, ok, err := NewTerm(kc).DetectUTF8(time.Second) + if err != nil || u != c.utf8 || ok != c.ok { + t.Errorf("%q: %v %v %v, want %v %v", c.answer, u, ok, err, c.utf8, c.ok) + } + if out := kc.out.String(); !strings.HasPrefix(out, "\r\xc3\xa9\x1b[6n") { + t.Errorf("%q: sent %q", c.answer, out) + } + } +}