Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying a fix for go#68880 - not yet working/enough #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
26 changes: 25 additions & 1 deletion terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Loading