Files
Claude bfdb51a452 Configuration: lines and shading right on a UTF-8 terminal with no locale
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRVaGDpEUKj7r3JGhpU
2026-11-10 13:44:40 +00:00

91 lines
2.5 KiB
Go

package textui
import (
"bytes"
"strings"
"testing"
"time"
"github.com/D-Modem/bbs/internal/screens"
)
type bufConn struct{ bytes.Buffer }
// SetReadDeadline does nothing: a buffer has no deadline.
func (*bufConn) SetReadDeadline(time.Time) error { return nil }
// TestCode: the sysop's tags as each terminal's escape sequences.
func TestCode(t *testing.T) {
ansi := NewTerm(&bufConn{})
ansi.ANSI = true
mono := NewTerm(&bufConn{})
mono.ANSI, mono.Mono = true, true
plain := NewTerm(&bufConn{})
vt52 := NewTerm(&bufConn{})
vt52.SetVT52()
for _, c := range []struct {
tag string
ansi, mono, plain, vt52 string
}{
{"red", "\x1b[31m", "", "", ""},
{"pink", "\x1b[35m", "", "", ""},
{"turquoise", "\x1b[36m", "", "", ""},
{"on blue", "\x1b[44m", "", "", ""},
{"bright", "\x1b[1m", "\x1b[1m", "", ""},
{"normal", "\x1b[22m", "\x1b[22m", "", ""},
{"underline", "\x1b[4m", "\x1b[4m", "", ""},
{"reset", "\x1b[0m", "\x1b[0m", "", ""},
{"clear", "\x1b[2J\x1b[H", "\x1b[2J\x1b[H", "\r\n", "\x1b[2J\x1b[H"},
{"eol", "\x1b[K", "\x1b[K", "", "\x1b[K"},
} {
for _, x := range []struct {
t *Term
want string
}{{ansi, c.ansi}, {mono, c.mono}, {plain, c.plain}, {vt52, c.vt52}} {
if got := x.t.Code(c.tag); got != x.want {
t.Errorf("%s: %q, want %q", c.tag, got, x.want)
}
}
}
// Print turns marks into the terminal's codes, on a Commodore too
c := &bufConn{}
pet := NewTerm(c)
pet.SetPETSCII(true, 40)
c.Reset()
pet.Print(screens.Mark("red") + "HI" + screens.Mark("reset"))
pet.Flush()
// red (0x1c), the letters in PETSCII, then the default light grey
if out := c.String(); strings.ContainsAny(out, "\x1b\x1e\x1f") || len(out) != 4 || out[0] != 0x1c {
t.Errorf("PETSCII: %q", out)
}
c2 := &bufConn{}
a := NewTerm(c2)
a.ANSI = true
a.Print(screens.Mark("green") + "go" + screens.Mark("default"))
a.Flush()
if got := c2.String(); got != "\x1b[32mgo\x1b[0m" {
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)
}
}
}