From dc8be9e18d0d111437f2a04a54e25d6c32b9a6f5 Mon Sep 17 00:00:00 2001 From: arrow2nd Date: Mon, 9 Nov 2020 23:26:03 +0900 Subject: [PATCH] fix: chinese corsor error chzyer/readline#179 --- runebuf.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/runebuf.go b/runebuf.go index 81d2da5..6776070 100644 --- a/runebuf.go +++ b/runebuf.go @@ -35,7 +35,7 @@ type RuneBuffer struct { sync.Mutex } -func (r* RuneBuffer) pushKill(text []rune) { +func (r *RuneBuffer) pushKill(text []rune) { r.lastKill = append([]rune{}, text...) } @@ -221,7 +221,7 @@ func (r *RuneBuffer) DeleteWord() { } for i := init + 1; i < len(r.buf); i++ { if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { - r.pushKill(r.buf[r.idx:i-1]) + r.pushKill(r.buf[r.idx : i-1]) r.Refresh(func() { r.buf = append(r.buf[:r.idx], r.buf[i-1:]...) }) @@ -350,7 +350,7 @@ func (r *RuneBuffer) Yank() { return } r.Refresh(func() { - buf := make([]rune, 0, len(r.buf) + len(r.lastKill)) + buf := make([]rune, 0, len(r.buf)+len(r.lastKill)) buf = append(buf, r.buf[:r.idx]...) buf = append(buf, r.lastKill...) buf = append(buf, r.buf[r.idx:]...) @@ -527,8 +527,11 @@ func (r *RuneBuffer) getBackspaceSequence() []byte { } var buf []byte for i := len(r.buf); i > r.idx; i-- { - // move input to the left of one - buf = append(buf, '\b') + // fix: chinese corsor error #179(@SilentTTxo) https://github.com/chzyer/readline/pull/179 + // move input to the left of one, fix character where width > 1, such like chinese + for j := 0; j < runes.Width(r.buf[i-1]); j++ { + buf = append(buf, '\b') + } if sep[i] { // up one line, go to the start of the line and move cursor right to the end (r.width) buf = append(buf, "\033[A\r"+"\033["+strconv.Itoa(r.width)+"C"...)