mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Merge pull request #47 from D-Modem/claude/project-thread-92s55y
Configuration: lines and shading right on a UTF-8 terminal with no locale
This commit is contained in:
3 files changed
+73
-1
No files matched your search
+21
-1
@@ -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 {
|
||||
|
||||
@@ -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`)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user