From 40755ea9f8bf7c53572dcbb3426787fdb74699df Mon Sep 17 00:00:00 2001 From: Nao YONASHIRO Date: Thu, 15 Feb 2018 01:06:06 +0900 Subject: [PATCH 1/2] fix: support linewrap on windows --- render.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/render.go b/render.go index 52c8c36..50cef95 100644 --- a/render.go +++ b/render.go @@ -1,5 +1,7 @@ package prompt +import "runtime" + // Render to render prompt information from state of Buffer. type Render struct { out ConsoleWriter @@ -109,7 +111,7 @@ func (r *Render) renderCompletion(buf *Buffer, completions *CompletionManager) { cursor := len(prefix) + len(buf.Document().TextBeforeCursor()) x, _ := r.toPos(cursor) if x+width >= int(r.col) { - r.out.CursorBackward(x + width - int(r.col)) + cursor = r.backward(cursor, x+width-int(r.col)) } contentHeight := len(completions.tmp) @@ -148,7 +150,10 @@ func (r *Render) renderCompletion(buf *Buffer, completions *CompletionManager) { r.out.SetColor(DefaultColor, r.scrollbarBGColor, false) } r.out.WriteStr(" ") - r.out.CursorBackward(width) + r.out.SetColor(DefaultColor, DefaultColor, false) + + r.lineWrap(cursor + width) + r.backward(cursor+width, width) } if x+width >= int(r.col) { @@ -189,6 +194,7 @@ func (r *Render) Render(buffer *Buffer, completion *CompletionManager) { r.out.SetColor(r.inputTextColor, r.inputBGColor, false) r.out.WriteStr(line) r.out.SetColor(DefaultColor, DefaultColor, false) + r.lineWrap(cursor) cursor = r.backward(cursor, len(line)-buffer.CursorPosition) @@ -201,7 +207,9 @@ func (r *Render) Render(buffer *Buffer, completion *CompletionManager) { r.out.SetColor(DefaultColor, DefaultColor, false) cursor += len(suggest.Text) + r.lineWrap(cursor) } + r.out.Flush() r.previousCursor = cursor @@ -231,12 +239,11 @@ func (r *Render) backward(from, n int) int { } func (r *Render) move(from, to int) int { - _, fromY := r.toPos(from) + fromX, fromY := r.toPos(from) toX, toY := r.toPos(to) r.out.CursorUp(fromY - toY) - r.out.WriteRaw([]byte{'\r'}) - r.out.CursorForward(toX) + r.out.CursorBackward(fromX - toX) return to } @@ -246,14 +253,15 @@ func (r *Render) move(from, to int) int { // x will not return 0 except for the first row. func (r *Render) toPos(cursor int) (x, y int) { col := int(r.col) - - if cursor > 0 && cursor%col == 0 { - return col - 1, cursor/col - 1 - } - return cursor % col, cursor / col } +func (r *Render) lineWrap(cursor int) { + if runtime.GOOS != "windows" && cursor > 0 && cursor%int(r.col) == 0 { + r.out.WriteRaw([]byte{'\n'}) + } +} + func clamp(high, low, x float64) float64 { switch { case high < x: From 2e8a01dd61ca6e776a6e8770361db1a1048fa003 Mon Sep 17 00:00:00 2001 From: Nao YONASHIRO Date: Thu, 15 Feb 2018 16:48:11 +0900 Subject: [PATCH 2/2] docs: update toPos document --- render.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/render.go b/render.go index 50cef95..e721044 100644 --- a/render.go +++ b/render.go @@ -248,9 +248,6 @@ func (r *Render) move(from, to int) int { } // toPos returns the relative position from the beginning of the string. -// the coordinate system with the beginning of the string as (0,0) and the width as r.col. -// the cursor points to the next character, but it points to that character only at the right end (x == r.col - 1). -// x will not return 0 except for the first row. func (r *Render) toPos(cursor int) (x, y int) { col := int(r.col) return cursor % col, cursor / col