Text terminals: menu keys checked, VT52 erasing, sheet keys, long bodies

- Menu takes only an item's key (returned as the item has it), asking
  again for any other without redrawing, as a 3270 does; Enter is still
  "" (none chosen: Back would log off from the top menu).  view.UI's
  Menu doc says so.
- VT52: ED 1 (erase up to the cursor) was sent as ESC J, erasing below;
  it is dropped now, as EL 1 is.  EL 2 erases the whole line from its
  start; ED 3 clears like ED 2.
- A sheet's key line leaves out 3270 PF-only keys instead of writing a
  NUL for them.
- The full-screen editor cuts a body longer than its line limit.
- keyLabel's unreachable branch removed; setTranslator's comment notes
  that Flush settles the old translator (it already did).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
Claude committed 2026-10-18 22:28:08 +00:00
1 parent ca664c88ed
commit e70735b708
10 files changed
+82 -20

No files matched your search

+19 -3
View File
@@ -131,9 +131,10 @@ func startFull(t *testing.T, g ui3270.Graphics, tweak func(*bbs.Settings), prep
// dialog is a connection to the BBS, read as text.
type dialog struct {
t *testing.T
c net.Conn
got []byte
t *testing.T
c net.Conn
got []byte
before []byte // what came before the last expect's want
}
// dial is a dialog on the unix socket name in the test's directory.
@@ -161,6 +162,7 @@ func (d *dialog) expect(want string) {
buf := make([]byte, 4096)
for {
if i := bytes.Index(d.got, []byte(want)); i >= 0 {
d.before = append(d.before[:0], d.got[:i]...)
d.got = d.got[i+len(want):]
return
}
@@ -883,3 +885,17 @@ func TestIndFile(t *testing.T) {
t.Errorf("no not-found message:\n%s", out)
}
}
// TestMenuKeys: on a text terminal a key that is no item's is asked for
// again, the menu not redrawn; one that is (in either case) is taken.
func TestMenuKeys(t *testing.T) {
e := start(t, nil, &Listener{Name: "modem", Kind: "raw", Address: "modem.sock", ANSI: "no"})
d := login(e, "alice", "alicepass1")
d.send("Z")
d.expect("Command: ")
if bytes.Contains(d.before, []byte("Main menu")) {
t.Errorf("redrawn for a key that is no item's: %q", d.before)
}
d.send("l")
d.expect("Last callers")
}
+3
View File
@@ -21,6 +21,9 @@ type fsBuffer struct {
func newBuffer(body []string, width, max int) *fsBuffer {
b := &fsBuffer{width: width, max: max, dirty: -1}
for _, l := range body {
if max > 0 && len(b.lines) >= max {
break // the rest past the limit, which nothing could save
}
b.lines = append(b.lines, []rune(l))
}
if len(b.lines) == 0 {
+5
View File
@@ -96,4 +96,9 @@ func TestBufferQuoteAndLimit(t *testing.T) {
if n := b.InsertLines([]string{"> three"}); n != 0 {
t.Errorf("quoted past the limit: %d", n)
}
// a body longer than the limit is cut to it
b = newBuffer([]string{"1", "2", "3", "4", "5", "6"}, 20, 4)
if got := b.Text(); len(got) != 4 || got[3] != "4" {
t.Errorf("a long body: %q", got)
}
}
+3 -1
View File
@@ -320,7 +320,9 @@ func (d *sheetDraw) drawFoot() {
}
keys := "Enter change Esc back F1 help F9 quit F10 save+exit"
for _, a := range d.sh.Keys {
keys = string(a.Key) + " " + a.Label + " " + keys
if a.Key != 0 { // a 3270's PF key alone: nothing to press here
keys = string(a.Key) + " " + a.Label + " " + keys
}
}
w := t.Cols - 1
var b strings.Builder
+1 -1
View File
@@ -100,7 +100,7 @@ type translator interface {
}
// setTranslator makes x what writes to the line (nil: nothing between),
// what was written before it flushed first.
// what was written before it flushed first (Flush settles the old one).
func (t *Term) setTranslator(x translator) {
t.Flush()
t.xl = x
+13 -7
View File
@@ -63,9 +63,6 @@ func keyLabel(a view.Action) string {
return string(rs[:i]) + "[" + string(k) + "]" + string(rs[i+1:])
}
}
if len(rs) > 0 && unicode.ToUpper(rs[0]) == k {
return "[" + string(k) + "]" + string(rs[1:])
}
return "[" + string(k) + "] " + a.Label
}
@@ -111,7 +108,8 @@ func (s *session) Narrow() bool { return s.t.Narrow() }
// Menu shows a menu: its intro, the sysop's screen if there is one, else
// its items (in RIP buttons where it can; in brief, only on ?), and reads
// a key. It is the key typed, unchecked; "" for Enter.
// a key until it is an item's. It is that item's key; "" for Enter (none
// chosen: the caller shows its menu again, or goes back if it will).
func (s *session) Menu(m *view.Menu) (string, error) {
t := s.t
if len(m.Intro) > 0 {
@@ -147,10 +145,18 @@ func (s *session) Menu(m *view.Menu) (string, error) {
if err == nil && brief && k == '?' {
k, err = s.menu(m.Title, items, "\n"+prompt, true)
}
if err != nil || k == '\r' {
return "", err
for {
if err != nil || k == '\r' {
return "", err
}
for _, it := range m.Items {
if strings.EqualFold(it.Key, string(k)) {
return it.Key, nil
}
}
// not an item's: asked again, as a 3270 does
k, err = s.menu(m.Title, items, "\n"+prompt, false)
}
return string(k), nil
}
// Table shows a table a page at a time, its columns fitted to the screen,
+14
View File
@@ -1,6 +1,7 @@
package textui
import (
"strings"
"testing"
"github.com/Neo-Desktop/bbs-sec/internal/view"
@@ -23,3 +24,16 @@ func TestKeyLabel(t *testing.T) {
t.Errorf("prompt %q", got)
}
}
// TestSheetFoot: the key line names a sheet's keys, leaving out those with
// a 3270 PF key alone (no NUL for them).
func TestSheetFoot(t *testing.T) {
term, c := ansiTerm()
d := &sheetDraw{t: term, sh: &view.Sheet{Keys: []view.Action{{Key: 'A', Label: "Add"}, {PF: 5, Label: "PFonly"}}}, cur: -1}
d.drawFoot()
term.Flush()
out := c.String()
if strings.Contains(out, "\x00") || strings.Contains(out, "PFonly") || !strings.Contains(out, "A Add Enter change") {
t.Errorf("foot %q", out)
}
}
+2 -1
View File
@@ -206,7 +206,8 @@ type UI interface {
PageSize() int
// Narrow is a screen under 60 columns.
Narrow() bool
// Menu returns the key chosen, or Back (a 3270's PF3).
// Menu returns the key of the item chosen, Back (a 3270's PF3), or ""
// for none (a text terminal's Enter).
Menu(m *Menu) (string, error)
Table(t *Table) (Choice, error)
Message(m *Message) (string, error)
+7
View File
@@ -92,4 +92,11 @@ func TestVT52Writer(t *testing.T) {
if buf.String() != want {
t.Errorf("%q\nwant %q", buf.String(), want)
}
// erasing: up to the cursor is beyond a VT52 (not made onwards); the
// whole line from its start
buf.Reset()
v.Write([]byte("a\x1b[1Jb\x1b[Jc\x1b[1Kd\x1b[2Ke\x1b[0K"))
if want := "ab\x1bJcd\r\x1bKe\x1bK"; buf.String() != want {
t.Errorf("erasing: %q\nwant %q", buf.String(), want)
}
}
+15 -7
View File
@@ -235,9 +235,10 @@ func (v *VT52Writer) Write(b []byte) (int, error) {
return len(b), nil
}
// esc is an ANSI escape as a VT52's: ED, EL (to the end of the line only),
// CUP (within 24 by 80) and the cursor moves (at most 100); anything else
// dropped.
// esc is an ANSI escape as a VT52's: ED (onwards, or all), EL (onwards,
// or the whole line, the cursor left at its start), CUP (within 24 by 80)
// and the cursor moves (at most 100); anything else, erasing up to the
// cursor among it, dropped.
func (v *VT52Writer) esc(e []byte) {
if len(e) < 3 || e[1] != '[' {
return // ESC 7, charsets, DCS (sixel), OSC: nothing on a VT52
@@ -262,15 +263,22 @@ func (v *VT52Writer) esc(e []byte) {
}
switch final {
case 'J':
if num(0, 0) == 2 {
v.out = append(v.out, "\x1bH\x1bJ"...)
} else {
switch num(0, 0) {
case 0:
v.out = append(v.out, "\x1bJ"...)
case 2, 3:
v.out = append(v.out, "\x1bH\x1bJ"...)
}
// 1 (to the cursor): a VT52 erases only onwards, so nothing
case 'K':
if num(0, 0) == 0 {
switch num(0, 0) {
case 0:
v.out = append(v.out, "\x1bK"...)
case 2:
// the whole line: from its start, where the cursor is left
v.out = append(v.out, "\r\x1bK"...)
}
// 1 (to the cursor): nothing, as for J
case 'H', 'f':
row, col := min(num(0, 1), 24), min(num(1, 1), 80)
v.out = append(v.out, 0x1b, 'Y', byte(31+row), byte(31+col))