From ea177b64f95d48567f49503c2d79c8a01fc8dd79 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 15 Aug 2024 11:20:57 -0700 Subject: [PATCH] Allow customization of the visualLength function, to address golang/go#68880 --- terminal.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/terminal.go b/terminal.go index 5634cbb..f64a81c 100644 --- a/terminal.go +++ b/terminal.go @@ -51,6 +51,11 @@ type Terminal struct { // may be empty if the terminal doesn't support them. Escape *EscapeCodes + // VisualLength can be override to provide a custom implementation + // for instance to handle wide characters. It should return the width + // of the rune array (and ignore ansi ESC sequences). + VisualLength func([]rune) int + // lock protects the terminal and the state in this object from // concurrent processing of a key press and a Write() call. lock sync.Mutex @@ -115,6 +120,7 @@ func NewTerminal(c io.ReadWriter, prompt string) *Terminal { historyIndex: -1, history: NewHistory(DefaultHistoryEntries), autoHistory: true, + VisualLength: visualLength, } } @@ -253,7 +259,7 @@ func (t *Terminal) moveCursorToPos(pos int) { return } - x := visualLength(t.prompt) + pos + x := t.VisualLength(t.prompt) + pos y := x / t.termWidth x = x % t.termWidth