From 3e151692778e19b2d435b175bf4f5d6a4483421f Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 15 Aug 2024 12:14:13 -0700 Subject: [PATCH] Trying a fix for go#68880 - not yet working/enough --- go.mod | 3 ++- go.sum | 6 ++++-- terminal.go | 26 +++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index f3be8a2..adc5e4d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,8 @@ go 1.22.6 require ( fortio.org/cli v1.8.0 fortio.org/log v1.16.0 - fortio.org/term v0.23.0-fortio-6 + fortio.org/term v0.23.0-fortio-6.0.20240815191104-2119463b2839 + github.com/rivo/uniseg v0.4.7 ) // replace fortio.org/term => ../term diff --git a/go.sum b/go.sum index cc7bc67..8e70df4 100644 --- a/go.sum +++ b/go.sum @@ -4,12 +4,14 @@ fortio.org/log v1.16.0 h1:GhU8/9NkYZmEIzvTN/DTMedDAStLJraWUUVUA2EbNDc= fortio.org/log v1.16.0/go.mod h1:t58Spg9njjymvRioh5F6qKGSupEsnMjXLGWIS1i3khE= fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0= fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= -fortio.org/term v0.23.0-fortio-6 h1:pKrUX0tKOxyEhkhLV50oJYucTVx94rzFrXc24lIuLvk= -fortio.org/term v0.23.0-fortio-6/go.mod h1:7buBfn81wEJUGWiVjFNiUE/vxWs5FdM9c7PyZpZRS30= +fortio.org/term v0.23.0-fortio-6.0.20240815191104-2119463b2839 h1:O5K8+QX6oosCALDvcD8MmUS+IPThkuyKhQDRocCvNJ0= +fortio.org/term v0.23.0-fortio-6.0.20240815191104-2119463b2839/go.mod h1:7buBfn81wEJUGWiVjFNiUE/vxWs5FdM9c7PyZpZRS30= fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U= fortio.org/version v1.0.4/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0= github.com/kortschak/goroutine v1.1.2 h1:lhllcCuERxMIK5cYr8yohZZScL1na+JM5JYPRclWjck= github.com/kortschak/goroutine v1.1.2/go.mod h1:zKpXs1FWN/6mXasDQzfl7g0LrGFIOiA6cLs9eXKyaMY= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= golang.org/x/crypto/x509roots/fallback v0.0.0-20240806160748-b2d3a6a4b4d3 h1:oWb21rU9Q9XrRwXLB7jHc1rbp6EiiimZZv5MLxpu4T0= golang.org/x/crypto/x509roots/fallback v0.0.0-20240806160748-b2d3a6a4b4d3/go.mod h1:kNa9WdvYnzFwC79zRpLRMJbdEFlhyM5RPFBBZp/wWH8= golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= diff --git a/terminal.go b/terminal.go index 7588075..4a06e45 100644 --- a/terminal.go +++ b/terminal.go @@ -11,8 +11,31 @@ import ( "fortio.org/log" "fortio.org/term" + "github.com/rivo/uniseg" ) +func VisualLength(runes []rune) int { + filtered := make([]rune, 0, len(runes)) + inEscapeSeq := false + // Skip escape sequences. + for _, r := range runes { + switch { + // Copied from term's visualLength + case inEscapeSeq: + if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') { + inEscapeSeq = false + } + case r == '\x1b': + inEscapeSeq = true + default: + filtered = append(filtered, r) + } + } + l := uniseg.StringWidth(string(filtered)) + // fmt.Printf("\nVisualLength(%q) = %d\n", string(runes), l) + return l +} + type Terminal struct { // Use this for any output to the screen/console so the required \r are added in raw mode // the prompt and command edit is refresh as needed when input comes in. @@ -38,6 +61,7 @@ func Open() (*Terminal, error) { fd: int(os.Stdin.Fd()), } t.term = term.NewTerminal(rw, "") + t.term.VisualLength = VisualLength t.Out = t.term if !t.IsTerminal() { t.Out = os.Stderr // no need to add \r for non raw mode. @@ -216,7 +240,7 @@ func (t *Terminal) Close() error { return nil } h := t.term.History() - log.LogVf("got history %v", h) + // log.LogVf("got history %v", h) slices.Reverse(h) extra := len(h) - t.capacity if extra > 0 {