Skip to content

Commit

Permalink
Allow customization of the visualLength function, to address golang/g…
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Aug 15, 2024
1 parent 6308a8b commit ea177b6
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,6 +120,7 @@ func NewTerminal(c io.ReadWriter, prompt string) *Terminal {
historyIndex: -1,
history: NewHistory(DefaultHistoryEntries),
autoHistory: true,
VisualLength: visualLength,
}
}

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ea177b6

Please sign in to comment.