diff --git a/internal/server/server_test.go b/internal/server/server_test.go index aacba63..b1d6181 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -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") +} diff --git a/internal/textui/fsbuffer.go b/internal/textui/fsbuffer.go index 4806ad9..45fe994 100644 --- a/internal/textui/fsbuffer.go +++ b/internal/textui/fsbuffer.go @@ -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 { diff --git a/internal/textui/fsbuffer_test.go b/internal/textui/fsbuffer_test.go index 6db67fc..2557ffe 100644 --- a/internal/textui/fsbuffer_test.go +++ b/internal/textui/fsbuffer_test.go @@ -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) + } } diff --git a/internal/textui/sheet.go b/internal/textui/sheet.go index 75ec3e0..1c7b772 100644 --- a/internal/textui/sheet.go +++ b/internal/textui/sheet.go @@ -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 diff --git a/internal/textui/term.go b/internal/textui/term.go index e87fc7d..383d35b 100644 --- a/internal/textui/term.go +++ b/internal/textui/term.go @@ -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 diff --git a/internal/textui/view.go b/internal/textui/view.go index 297472f..95e8373 100644 --- a/internal/textui/view.go +++ b/internal/textui/view.go @@ -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, diff --git a/internal/textui/view_test.go b/internal/textui/view_test.go index 6f85c0d..e7d5b74 100644 --- a/internal/textui/view_test.go +++ b/internal/textui/view_test.go @@ -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) + } +} diff --git a/internal/view/view.go b/internal/view/view.go index 6c250ea..053ee73 100644 --- a/internal/view/view.go +++ b/internal/view/view.go @@ -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) diff --git a/internal/vt/vt_test.go b/internal/vt/vt_test.go index 293363b..369963d 100644 --- a/internal/vt/vt_test.go +++ b/internal/vt/vt_test.go @@ -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) + } } diff --git a/internal/vt/writer.go b/internal/vt/writer.go index af29942..322c0de 100644 --- a/internal/vt/writer.go +++ b/internal/vt/writer.go @@ -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))