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

1178 lines
29 KiB
Go

// Package textui is the BBS on a character terminal: a telnet client, or a
// caller's terminal program over a modem. The server echoes and edits
// what the caller types; ANSI color and cursor movement are used only when
// the terminal has them.
package textui
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
"github.com/D-Modem/bbs/internal/petscii"
"github.com/D-Modem/bbs/internal/rip"
"github.com/D-Modem/bbs/internal/screens"
"github.com/D-Modem/bbs/internal/vt"
)
// ErrIdle: the caller typed nothing for too long.
var ErrIdle = errors.New("idle timeout")
// noIdle: how long a read waits with no Idle (as good as for ever)
const noIdle = 100 * 365 * 24 * time.Hour
// ErrPETSCII: while WatchPETSCII, a key only a Commodore sends.
var ErrPETSCII = errors.New("a Commodore's key")
// Conn is what a terminal runs on: a net.Conn, or a telnet.Conn.
type Conn interface {
io.ReadWriter
SetReadDeadline(time.Time) error
}
// Term is the caller's terminal.
type Term struct {
c Conn
r *bufio.Reader
w *bufio.Writer
ANSI bool // color and cursor movement
UTF8 bool // UTF-8 both ways; otherwise 7-bit ASCII
Sixel bool // sixel graphics
// NoPictures: the caller turned pictures off (PictureMode)
NoPictures bool
// PETSCII: a Commodore 64 or 128 (SetPETSCII); colors, but not ANSI
PETSCII bool
// RIP: an ANSI terminal with RIPscrip graphics (RIPterm, SyncTERM)
RIP bool
// DEC: a VT without UTF-8 (SetDEC): DEC Special Graphics, Latin-1
DEC bool
// VT52: a VT52 (SetVT52): its escapes, no attributes
VT52 bool
// Mono: a terminal without color: bold for colors
Mono bool
// Ident is what the terminal said it is (vt.Info's Name), if anything
Ident string
// WatchPETSCII: the terminal may be a Commodore's: a key only one
// sends ends a read with ErrPETSCII
WatchPETSCII bool
// Upper: ASCII letters printed in capitals (which a Commodore in its
// power-on set and anyone else both read)
Upper bool
// Clean8 is an 8-bit clean line (a modem, rlogin, telnet in binary
// mode): what X/Y/ZMODEM need
Clean8 bool
// a character cell in pixels, if the terminal said (for sixel)
CellW, CellH int
Cols int
Rows int
Idle time.Duration // for each key
deadline time.Time // a hard limit for keys (the login's)
lastCR bool
// Notify, if set, gives lines to show the caller while a key is
// awaited (chat, messages from other nodes): they are printed above
// the line being typed, which is then drawn again
Notify func() []string
cur []rune // what has been printed of the line the cursor is on
// PausePrompt is Pause's ("": "[Press a key]")
PausePrompt string
echo *echoConn // what is sent, for a sysop watching
xl translator // under w: PETSCII, DEC, VT52
latin1 bool // a DEC terminal's 8-bit characters are Latin-1
sinceKey int // lines printed since a key was read
ripScene bool // a RIP scene is on the screen
// OnLineSpeedKey, if set, is called for Ctrl-B at a prompt: the line
// speed's picker (PickLineSpeed)
OnLineSpeedKey func() error
line *pacer // under echo: what is sent, at the line speed
picking bool // in PickLineSpeed: Ctrl-B is a key
}
// translator turns what the BBS writes (UTF-8, ANSI) into a terminal's.
type translator interface {
io.Writer
Settle() error // before waiting for a key
Raw(b []byte) error // bytes as they are
}
// setTranslator makes x what writes to the line (nil: nothing between),
// what was written before it flushed first (Flush settles the old one).
func (t *Term) setTranslator(x translator) {
t.Flush()
t.xl = x
if x == nil {
t.w = bufio.NewWriterSize(t.c, 4096)
} else {
t.w = bufio.NewWriterSize(x, 4096)
}
}
// SetDEC makes the terminal a VT without UTF-8: line drawing in DEC
// Special Graphics, Latin-1 if latin1 (a VT220's or later, on an 8-bit
// line).
func (t *Term) SetDEC(latin1 bool) {
t.DEC, t.UTF8, t.latin1 = true, false, latin1
t.setTranslator(vt.NewDECWriter(t.c, latin1))
}
// SetVT52 makes the terminal a VT52: no ANSI, its own escapes.
func (t *Term) SetVT52() {
t.VT52, t.ANSI, t.UTF8, t.Sixel, t.RIP, t.Mono = true, false, false, false, false, true
t.Cols, t.Rows = 80, 24
t.setTranslator(vt.NewVT52Writer(t.c))
}
// SetPETSCII makes the terminal a Commodore's (width 40, or a C128's 80)
// or no longer one.
func (t *Term) SetPETSCII(on bool, width int) {
if !on {
t.PETSCII = false
t.setTranslator(nil)
return
}
t.PETSCII, t.ANSI, t.UTF8, t.Sixel, t.RIP = true, false, false, false, false
t.Cols, t.Rows = width, 25
pet := petscii.NewWriter(t.c, width)
t.setTranslator(pet)
pet.Raw(petscii.Start())
t.cur = t.cur[:0]
}
// Raw writes bytes as they are, past any translation (a .seq screen to a
// Commodore).
func (t *Term) Raw(b []byte) error {
if err := t.Flush(); err != nil {
return err
}
t.cur = t.cur[:0]
if t.xl != nil {
return t.xl.Raw(b)
}
_, err := t.c.Write(b)
return err
}
// NewTerm makes a terminal on c, 80x24 plain ASCII until told otherwise.
func NewTerm(c Conn) *Term {
line := &pacer{Conn: c, sleep: time.Sleep}
ec := &echoConn{Conn: line}
t := &Term{c: ec, echo: ec, line: line, r: bufio.NewReaderSize(c, 1024), w: bufio.NewWriterSize(ec, 4096),
Cols: 80, Rows: 24, Idle: 10 * time.Minute}
line.poll = t.pollSkip
return t
}
// echoConn gives what is written to the caller to Echo as well (for a
// sysop watching).
type echoConn struct {
Conn
mu sync.Mutex
echo func([]byte)
}
// Write writes p to the line and gives what was written to echo, if set.
func (c *echoConn) Write(p []byte) (int, error) {
n, err := c.Conn.Write(p)
c.mu.Lock()
echo := c.echo
c.mu.Unlock()
if echo != nil && n > 0 {
echo(p[:n])
}
return n, err
}
// SetEcho gives f a copy of everything the terminal sends from now on.
func (t *Term) SetEcho(f func([]byte)) {
t.echo.mu.Lock()
t.echo.echo = f
t.echo.mu.Unlock()
}
// SetDeadline sets a limit for every key from now on (zero: none).
func (t *Term) SetDeadline(d time.Time) { t.deadline = d }
// Colors, for C.
const (
Reset = "0"
Bold = "1"
Red = "1;31"
Green = "1;32"
Yellow = "1;33"
Blue = "1;34"
Magenta = "1;35"
Cyan = "1;36"
White = "1;37"
Dim = "0;37"
)
// C wraps s in a color, on an ANSI terminal.
func (t *Term) C(color, s string) string {
switch {
case t.VT52 || !t.ANSI && !t.PETSCII:
return s
case t.Mono:
// bright colors bold, the rest plain
if strings.HasPrefix(color, "1") {
return "\x1b[1m" + s + "\x1b[0m"
}
return s
}
return "\x1b[" + color + "m" + s + "\x1b[0m"
}
// Code is a tag of the sysop's ({red}, {clear}: screens.Tags) as this
// terminal's escape sequence: ANSI's (a Commodore's translated), bold
// alone on one without color, the clears and the cursor's moves alone on
// a VT52, and nothing on a plain terminal ({clear} a new line, as Clear).
func (t *Term) Code(tag string) string {
moves := t.ANSI || t.PETSCII || t.VT52
switch tag {
case "clear", "cls":
if moves {
return "\x1b[2J\x1b[H"
}
return "\r\n"
case "home":
if moves {
return "\x1b[H"
}
return ""
case "eol":
if moves {
return "\x1b[K"
}
return ""
}
if t.VT52 || !t.ANSI && !t.PETSCII {
return ""
}
sgr := ""
switch tag {
case "bright", "bold":
sgr = "1"
case "normal":
sgr = "22"
case "blink":
sgr = "5"
case "underline":
sgr = "4"
case "reverse":
sgr = "7"
case "default", "reset":
sgr = "0"
default:
name, bg := strings.CutPrefix(tag, "on ")
n, ok := screens.ColorNumber(name)
if !ok || t.Mono {
return ""
}
if bg {
n += 40
} else {
n += 30
}
sgr = strconv.Itoa(n)
}
return "\x1b[" + sgr + "m"
}
// Print writes s: \n as CR LF, and on an ASCII terminal anything else as ?.
// Our own escape sequences (from C) pass, and the sysop's tags, marked
// (screens.Marked), become the terminal's (Code); text from users never
// has either (bbs.Clean took them out).
func (t *Term) Print(s string) {
if strings.IndexByte(s, 0x1e) >= 0 {
s = screens.Marks.ReplaceAllStringFunc(s, func(m string) string { return t.Code(m[1 : len(m)-1]) })
}
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
i += size
switch r {
case '\n':
t.cur = t.cur[:0]
t.sinceKey++
case '\b':
if len(t.cur) > 0 {
t.cur = t.cur[:len(t.cur)-1]
}
default:
t.cur = append(t.cur, r)
}
if t.Upper && r >= 'a' && r <= 'z' {
r -= 'a' - 'A'
}
switch {
case r == '\n':
t.w.WriteString("\r\n")
case r < 0x80:
t.w.WriteByte(byte(r))
case (t.UTF8 || t.xl != nil) && r != utf8.RuneError:
t.w.WriteRune(r)
default:
t.w.WriteByte('?')
}
}
}
// Printf prints a formatted string.
func (t *Term) Printf(format string, a ...any) { t.Print(fmt.Sprintf(format, a...)) }
// Println prints s and a new line.
func (t *Term) Println(s string) { t.Print(s + "\n") }
// Flush sends what is printed.
func (t *Term) Flush() error {
if err := t.w.Flush(); err != nil {
return err
}
if t.xl != nil {
return t.xl.Settle()
}
return nil
}
// Clear clears the screen (a blank line on a plain terminal).
func (t *Term) Clear() {
t.cur = t.cur[:0]
if t.ANSI || t.PETSCII || t.VT52 {
t.w.WriteString("\x1b[2J\x1b[H")
} else {
t.w.WriteString("\r\n")
}
}
// readRune reads one key: a rune, with arrow keys and other escape
// sequences dropped and CR LF taken as one CR. While it waits it shows
// what Notify has.
func (t *Term) readRune() (rune, error) { return t.read(false) }
// Keys EditKey reads beyond runes: the cursor and editing keys.
const (
KeyUp rune = 0xE000 + iota
KeyDown
KeyRight
KeyLeft
KeyHome
KeyEnd
KeyPgUp
KeyPgDn
KeyInsert
KeyDelete
KeyF1 // and on to KeyF12: KeyF1 + 11
)
// fkeys are ESC [ N ~'s function keys (xterm, SyncTERM)
var fkeys = map[string]rune{"11": KeyF1, "12": KeyF1 + 1, "13": KeyF1 + 2, "14": KeyF1 + 3, "15": KeyF1 + 4,
"17": KeyF1 + 5, "18": KeyF1 + 6, "19": KeyF1 + 7, "20": KeyF1 + 8, "21": KeyF1 + 9, "23": KeyF1 + 10, "24": KeyF1 + 11}
// EditKey reads one key as typed, for a full-screen editor: runes and
// control characters as they are (CR LF as CR), the cursor and editing
// keys as KeyUp and the rest, a lone ESC as ESC.
func (t *Term) EditKey() (rune, error) { return t.read(true) }
// read reads a key for readRune (keys false) or EditKey (true), waiting
// at most Idle (0: no limit) or to the deadline, if sooner: ErrIdle then. A LF after
// a CR is dropped; an escape sequence is dropped, or, for EditKey, the
// key it is.
func (t *Term) read(keys bool) (rune, error) {
if err := t.Flush(); err != nil {
return 0, err
}
t.line.skip.Store(false) // a skipped screen ends at its prompt
t.line.noPoll.Store(false)
start := time.Now()
idle := t.Idle
if idle <= 0 {
idle = noIdle
}
for {
limit := start.Add(idle)
if !t.deadline.IsZero() && t.deadline.Before(limit) {
limit = t.deadline
}
if err := t.await(limit); err != nil {
return 0, err
}
t.c.SetReadDeadline(limit)
r, err := t.next()
t.c.SetReadDeadline(time.Time{})
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
return 0, ErrIdle
}
return 0, err
}
if r == '\n' && t.lastCR {
t.lastCR = false
continue
}
t.lastCR = r == '\r'
if r == 0x1b && keys {
k, err := t.escapeKey()
if err != nil {
return 0, err
}
if k == 0 {
continue
}
t.sinceKey = 0
return k, nil
}
if r == 0x1b {
if err := t.skipEscape(); err != nil {
return 0, err
}
continue
}
if r == lineSpeedKey && t.OnLineSpeedKey != nil && !t.picking && !t.PETSCII {
if err := t.OnLineSpeedKey(); err != nil {
return 0, err
}
if err := t.Flush(); err != nil {
return 0, err
}
start = time.Now()
continue
}
t.sinceKey = 0
return r, nil
}
}
// await waits until a key is there (or limit), showing Notify's lines as
// they come.
func (t *Term) await(limit time.Time) error {
for t.r.Buffered() == 0 {
t.notices()
if err := t.Flush(); err != nil {
return err
}
now := time.Now()
if !now.Before(limit) {
return ErrIdle
}
slice := limit
if t.Notify != nil && now.Add(500*time.Millisecond).Before(limit) {
slice = now.Add(500 * time.Millisecond)
}
t.c.SetReadDeadline(slice)
_, err := t.r.Peek(1)
t.c.SetReadDeadline(time.Time{})
if err == nil {
return nil
}
if !errors.Is(err, os.ErrDeadlineExceeded) {
return err
}
}
return nil
}
// notices prints Notify's lines above the line being typed, and draws
// that again.
func (t *Term) notices() {
if t.Notify == nil {
return
}
ls := t.Notify()
if len(ls) == 0 {
return
}
cur := string(t.cur)
if t.ANSI {
t.w.WriteString("\r\x1b[K")
} else if cur != "" {
t.w.WriteString("\r\n")
}
t.cur = t.cur[:0]
for _, l := range ls {
t.Println(l)
}
t.Print(cur)
}
// next is the next character from the line, as the terminal sends it:
// PETSCII, UTF-8, Latin-1, or ASCII (anything else utf8.RuneError). While
// WatchPETSCII, a byte only a Commodore sends is ErrPETSCII.
func (t *Term) next() (rune, error) {
if t.PETSCII {
b, err := t.r.ReadByte()
if err != nil {
return 0, err
}
return petscii.Decode(b), nil
}
if t.WatchPETSCII && !t.UTF8 {
b, err := t.r.ReadByte()
if err != nil {
return 0, err
}
switch {
case !petscii.Telltale(b):
case b >= 0xC1 && b <= 0xDA && t.continuation():
t.r.ReadByte() // a UTF-8 letter, not a shifted one
default:
return 0, ErrPETSCII
}
if b >= 0x80 {
return utf8.RuneError, nil
}
return rune(b), nil
}
if t.UTF8 {
r, _, err := t.r.ReadRune()
return r, err
}
b, err := t.r.ReadByte()
if b >= 0x80 {
if t.latin1 && b >= 0xa0 {
return rune(b), err
}
return utf8.RuneError, err
}
return rune(b), err
}
// continuation: the next byte, if one comes at once, continues a UTF-8
// character
func (t *Term) continuation() bool {
if t.r.Buffered() == 0 {
t.c.SetReadDeadline(time.Now().Add(200 * time.Millisecond))
defer t.c.SetReadDeadline(time.Time{})
}
b, err := t.r.Peek(1)
return err == nil && b[0] >= 0x80 && b[0] <= 0xBF
}
// skipEscape drops the rest of an escape sequence (ESC [ ... final, or
// ESC O x), waiting briefly: a lone ESC is dropped by itself.
func (t *Term) skipEscape() error {
t.c.SetReadDeadline(time.Now().Add(300 * time.Millisecond))
defer t.c.SetReadDeadline(time.Time{})
b, err := t.r.ReadByte()
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
return nil
}
return err
}
switch b {
case '[':
for i := 0; i < 16; i++ {
b, err := t.r.ReadByte()
if err != nil {
return nil
}
if b >= 0x40 && b <= 0x7e {
return nil
}
}
case 'O':
t.r.ReadByte()
case 'A', 'B', 'C', 'D':
if !t.VT52 {
t.r.UnreadByte()
} // a VT52's cursor keys
case '?':
if t.VT52 {
t.r.ReadByte() // its keypad
} else {
t.r.UnreadByte()
}
default:
t.r.UnreadByte()
}
return nil
}
// escapeKey reads the rest of an escape sequence as a key (ESC [ A, ESC O
// H, ESC [ 3 ~, a VT52's ESC A ...): 0 for one that is no key, ESC for a
// lone ESC.
func (t *Term) escapeKey() (rune, error) {
t.c.SetReadDeadline(time.Now().Add(300 * time.Millisecond))
defer t.c.SetReadDeadline(time.Time{})
b, err := t.r.ReadByte()
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
return 0x1b, nil
}
return 0, err
}
final := map[byte]rune{'A': KeyUp, 'B': KeyDown, 'C': KeyRight, 'D': KeyLeft, 'H': KeyHome, 'F': KeyEnd}
switch b {
case '[':
var param []byte
for i := 0; i < 16; i++ {
c, err := t.r.ReadByte()
if err != nil {
return 0, nil
}
if c == '[' && i == 0 {
// the Linux console's F1-F5: ESC [ [ A to E
c, err := t.r.ReadByte()
if err != nil || c < 'A' || c > 'E' {
return 0, nil
}
return KeyF1 + rune(c-'A'), nil
}
if c >= 0x40 && c <= 0x7e {
if c == '~' {
p, _, _ := strings.Cut(string(param), ";") // ;2 and the like: shift, ctrl
if k, ok := fkeys[p]; ok {
return k, nil
}
return map[string]rune{"1": KeyHome, "7": KeyHome, "4": KeyEnd, "8": KeyEnd,
"2": KeyInsert, "3": KeyDelete, "5": KeyPgUp, "6": KeyPgDn}[p], nil
}
return final[c], nil
}
param = append(param, c)
}
return 0, nil
case 'O':
c, err := t.r.ReadByte()
if err != nil {
return 0, nil
}
if c >= 'P' && c <= 'S' {
return KeyF1 + rune(c-'P'), nil // F1-F4
}
return final[c], nil
case 'A', 'B', 'C', 'D':
if t.VT52 {
return final[b], nil
}
}
t.r.UnreadByte()
return 0x1b, nil
}
// Key reads one key, upper-cased; Enter is '\r'.
func (t *Term) Key() (rune, error) {
for {
r, err := t.readRune()
if err != nil {
return 0, err
}
if r == '\n' {
r = '\r'
}
if r == '\r' || unicode.IsPrint(r) {
return unicode.ToUpper(r), nil
}
}
}
// LineOpts are ReadLine's options.
type LineOpts struct {
Max int // runes, at most
Mask bool // echo * (passwords)
Initial string // already typed
Wrap bool // at Max, a word being typed goes to the next line (Carry)
}
// Line is a line read: the text, and the word that wrapped to the next.
type Line struct {
Text string
Carry string
}
// ReadLine reads a line, echoing and editing it: backspace or delete
// erase, ^U erases the line, ^W the last word.
func (t *Term) ReadLine(o LineOpts) (Line, error) {
if o.Max <= 0 {
o.Max = 79
}
buf := []rune(o.Initial)
if len(buf) > o.Max {
buf = buf[:o.Max]
}
t.Print(string(buf))
echo := func(r rune) {
if o.Mask {
t.Print("*")
} else {
t.Print(string(r))
}
}
erase := func(n int) {
for i := 0; i < n; i++ {
t.Print("\b \b")
}
}
for {
r, err := t.readRune()
if err != nil {
return Line{}, err
}
switch {
case r == '\r' || r == '\n':
t.Print("\n")
t.sinceKey = 0
return Line{Text: string(buf)}, nil
case r == 8 || r == 0x7f:
if len(buf) > 0 {
buf = buf[:len(buf)-1]
erase(1)
}
case r == 0x15: // ^U
erase(len(buf))
buf = buf[:0]
case r == 0x17: // ^W
n := len(buf)
for n > 0 && buf[n-1] == ' ' {
n--
}
for n > 0 && buf[n-1] != ' ' {
n--
}
erase(len(buf) - n)
buf = buf[:n]
case r == '\t':
if len(buf) < o.Max {
buf = append(buf, ' ')
echo(' ')
}
case r == utf8.RuneError || !unicode.IsPrint(r):
// nothing
case len(buf) < o.Max:
buf = append(buf, r)
echo(r)
case o.Wrap:
// the line is full: the word being typed goes on the next
if r == ' ' {
t.Print("\n")
return Line{Text: string(buf)}, nil
}
cut := len(buf)
for cut > 0 && buf[cut-1] != ' ' {
cut--
}
if cut == 0 || len(buf)-cut > o.Max/2 {
cut = len(buf) // one long word: break it
}
carry := string(buf[cut:]) + string(r)
erase(len(buf) - cut)
t.Print("\n")
return Line{Text: strings.TrimRight(string(buf[:cut]), " "), Carry: carry}, nil
}
}
}
// Input prints a prompt and reads a line of at most max runes.
func (t *Term) Input(prompt string, max int) (string, error) {
t.Print(prompt)
l, err := t.ReadLine(LineOpts{Max: max})
return strings.TrimSpace(l.Text), err
}
// Password prints a prompt and reads a masked line.
func (t *Term) Password(prompt string) (string, error) {
t.Print(prompt)
l, err := t.ReadLine(LineOpts{Max: 72, Mask: true})
return l.Text, err
}
// YesNo asks a question; Enter is def.
func (t *Term) YesNo(prompt string, def bool) (bool, error) {
hint := " [y/N] "
if def {
hint = " [Y/n] "
}
t.Print(prompt + hint)
for {
k, err := t.Key()
if err != nil {
return false, err
}
switch k {
case 'Y':
t.Println("Yes")
return true, nil
case 'N':
t.Println("No")
return false, nil
case '\r':
if def {
t.Println("Yes")
} else {
t.Println("No")
}
return def, nil
}
}
}
// Pause waits for a key.
func (t *Term) Pause() error {
p := t.PausePrompt
if p == "" {
p = "[Press a key]"
}
t.Print(t.C(Dim, p))
_, err := t.Key()
t.Print("\r" + strings.Repeat(" ", min(utf8.RuneCountInString(p), 79)) + "\r")
return err
}
// Art writes ANSI art as it is (its escape sequences too): UTF-8 to a
// UTF-8 terminal, CP437 to any other; \n as CR LF.
func (t *Term) Art(s string) {
s = strings.ReplaceAll(s, "\n", "\r\n")
if t.UTF8 || t.xl != nil {
t.w.WriteString(s)
} else {
t.w.Write(screens.EncodeCP437(s))
}
if i := strings.LastIndex(s, "\n"); i >= 0 {
t.cur = t.cur[:0]
}
}
// Page shows lines a screen at a time; false if the caller quit.
func (t *Term) Page(lines []string) (bool, error) { return t.PageAfter(lines, 0) }
// PageAfter is Page below used rows already on the screen.
func (t *Term) PageAfter(lines []string, used int) (bool, error) {
per := max(t.Rows-2, 5)
room := max(per-used, 1) // the lines before the first pause
for i, l := range lines {
if i > 0 && (i == room || i > room && (i-room)%per == 0) {
t.Print(t.C(Dim, "-- More -- [Enter] continue, [Q]uit "))
k, err := t.Key()
t.Print("\r" + strings.Repeat(" ", 37) + "\r")
if err != nil {
return false, err
}
if k == 'Q' {
return false, nil
}
}
t.Println(l)
}
return true, nil
}
var cprPattern = regexp.MustCompile(`\x1b\[(\d+);(\d+)R`)
// DetectANSI asks the terminal where its cursor is, after moving it as far
// down and right as it goes: an ANSI terminal answers, and the answer is
// its size. A terminal that does not answer within timeout is not ANSI.
func (t *Term) DetectANSI(timeout time.Duration) (bool, error) {
t.w.WriteString("\x1b7\x1b[999;999H\x1b[6n\x1b8")
if err := t.Flush(); err != nil {
return 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, err
}
got = append(got, b)
if m := cprPattern.FindSubmatch(got); m != nil {
rows, _ := strconv.Atoi(string(m[1]))
cols, _ := strconv.Atoi(string(m[2]))
if rows >= 10 && rows <= 200 {
t.Rows = rows
}
if cols >= 40 && cols <= 300 {
t.Cols = cols
}
return true, nil
}
}
// a plain terminal shows the request as text: wipe it
t.w.WriteString("\r" + strings.Repeat(" ", 20) + "\r")
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`)
)
// DetectSixel asks the terminal for its device attributes (DA1), and its
// character cell's size in pixels (xterm's CSI 16 t, which not every
// terminal answers): a terminal whose attributes include 4 has sixel
// graphics. Only for ANSI terminals; one that does not answer within
// timeout has none.
func (t *Term) DetectSixel(timeout time.Duration) (bool, error) {
t.w.WriteString("\x1b[16t\x1b[c")
if err := t.Flush(); err != nil {
return false, err
}
var got []byte
end := time.Now().Add(timeout)
for time.Now().Before(end) && len(got) < 256 {
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, err
}
got = append(got, b)
if m := daPattern.FindSubmatch(got); m != nil {
if c := cellPattern.FindSubmatch(got); c != nil {
h, _ := strconv.Atoi(string(c[1]))
w, _ := strconv.Atoi(string(c[2]))
if w >= 4 && w <= 64 && h >= 4 && h <= 128 {
t.CellW, t.CellH = w, h
}
}
for _, a := range strings.Split(string(m[1]), ";") {
if a == "4" {
return true, nil
}
}
return false, nil
}
}
return false, nil
}
// Picture shows a sixel picture (sixel.Encode's) on its own lines.
func (t *Term) Picture(data []byte) {
if !t.Sixel || len(data) == 0 {
return
}
t.w.Write(data)
t.w.WriteString("\r\n")
}
// RawKey reads one byte as it comes (no decoding), waiting up to d
// (ErrIdle after).
func (t *Term) RawKey(d time.Duration) (byte, error) {
if err := t.Flush(); err != nil {
return 0, err
}
t.c.SetReadDeadline(time.Now().Add(d))
b, err := t.r.ReadByte()
t.c.SetReadDeadline(time.Time{})
if errors.Is(err, os.ErrDeadlineExceeded) {
return 0, ErrIdle
}
return b, err
}
// Identify asks an ANSI terminal what it is (vt.Query): its answers, and
// its character cell (for sixel) if it says.
func (t *Term) Identify(timeout time.Duration) (vt.Info, error) {
t.w.WriteString(vt.Query)
if err := t.Flush(); err != nil {
return vt.Info{}, err
}
got, err := t.collect(timeout, 1024, vt.Done)
if err != nil {
return vt.Info{}, err
}
in := vt.Parse(got)
if in.CellW > 0 {
t.CellW, t.CellH = in.CellW, in.CellH
}
t.Ident = in.Name()
return in, nil
}
// DetectVT52 asks a terminal that is not ANSI whether it is a VT52 (ESC
// Z); any other shows a Z, which is wiped.
func (t *Term) DetectVT52(timeout time.Duration) (bool, error) {
t.w.WriteString(vt.QueryVT52)
if err := t.Flush(); err != nil {
return false, err
}
got, err := t.collect(timeout, 16, func(b []byte) bool { return vt.Parse(b).VT52 })
if err != nil {
return false, err
}
if vt.Parse(got).VT52 {
t.Ident = "VT52"
return true, nil
}
t.w.WriteString("\r \r")
return false, nil
}
// collect reads what comes until done says so, max bytes, or timeout.
func (t *Term) collect(timeout time.Duration, max int, done func([]byte) bool) ([]byte, error) {
var got []byte
end := time.Now().Add(timeout)
for time.Now().Before(end) && len(got) < max {
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 nil, err
}
got = append(got, b)
if done(got) {
break
}
}
return got, nil
}
// Narrow is a screen too narrow for the usual columns (a C64's 40).
func (t *Term) Narrow() bool { return t.Cols < 60 }
// DetectRIP asks an ANSI terminal whether it has RIPscrip, with the
// position report every ANSI terminal answers after it: a RIP terminal's
// answer comes first.
func (t *Term) DetectRIP(timeout time.Duration) (bool, error) {
t.w.WriteString(rip.Query + "\x1b[6n")
if err := t.Flush(); err != nil {
return false, err
}
var got []byte
end := time.Now().Add(timeout)
for time.Now().Before(end) && len(got) < 128 {
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, err
}
got = append(got, b)
if cprPattern.Match(got) {
break
}
}
return rip.Version.Match(got), nil
}
// RIPScene draws a RIP scene (rip.Menu's), from the first column.
func (t *Term) RIPScene(scene string) {
t.w.WriteString("\r\n" + scene)
t.cur = t.cur[:0]
t.ripScene, t.sinceKey = true, 0
}
// RIPClear takes a RIP scene off the screen: graphics, buttons, the text
// window as it was.
func (t *Term) RIPClear() {
if !t.ripScene {
return
}
t.w.WriteString("\r\n" + (&rip.Script{}).KillMouseFields().Reset().String() + "\x1b[2J\x1b[H")
t.cur = t.cur[:0]
t.ripScene, t.sinceKey = false, 0
}
// port is the terminal as a line for file transfers: the same buffered
// reader (what was typed ahead is not lost), writes straight through.
type port struct {
t *Term
paced bool // at the line speed (a door's); else the line's own (a transfer's)
}
// Read reads from the terminal's reader, what was typed ahead first.
func (p port) Read(b []byte) (int, error) { return p.t.r.Read(b) }
// Write writes b to the line as it is, past any translation.
func (p port) Write(b []byte) (int, error) {
// the protocol's own timing: not slowed to the line speed
if !p.paced {
p.t.line.setDirect(true)
defer p.t.line.setDirect(false)
}
return p.t.c.Write(b)
}
// SetReadDeadline sets the line's read deadline.
func (p port) SetReadDeadline(d time.Time) error { return p.t.c.SetReadDeadline(d) }
// Port is the line for a file transfer; what was printed goes first.
func (t *Term) Port() (port, error) {
if err := t.Flush(); err != nil {
return port{}, err
}
return port{t: t}, nil
}
// DoorPort is Port for a door: what it sends comes at the line speed.
func (t *Term) DoorPort() (port, error) {
p, err := t.Port()
p.paced = true
// the door's own goroutine reads the keys till it is done: not polled
t.line.noPoll.Store(true)
return p, err
}
// Settle waits for the line to be quiet for d (the end of a transfer, a
// terminal program's leftovers) and drops what came.
func (t *Term) Settle(d time.Duration) {
for i := 0; i < 1000; i++ {
t.c.SetReadDeadline(time.Now().Add(d))
_, err := t.r.ReadByte()
if err != nil {
break
}
for t.r.Buffered() > 0 {
t.r.ReadByte()
}
}
t.c.SetReadDeadline(time.Time{})
t.lastCR = false
}