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

Allow customization of the visualLength function, to address golang/go#68880 #8

Closed
wants to merge 1 commit into from
Closed
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
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 overridden 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