Skip to content

Commit

Permalink
[complete] avoid reach end of line in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer authored and lunixbochs committed May 4, 2017
1 parent 1d94003 commit d91f50f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions complete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package readline

import (
"bufio"
"bytes"
"fmt"
"io"
Expand Down Expand Up @@ -186,11 +187,18 @@ func (o *opCompleter) CompleteRefresh() {
colWidth = w
}
}
colNum := o.width / (colWidth + o.candidateOff + 2)
colWidth += o.candidateOff + 1
same := o.op.buf.RuneSlice(-o.candidateOff)

// -1 to avoid reach the end of line
width := o.width - 1
colNum := width / colWidth
colWidth += (width - (colWidth * colNum)) / colNum

o.candidateColNum = colNum
buf := bytes.NewBuffer(nil)
buf := bufio.NewWriter(o.w)
buf.Write(bytes.Repeat([]byte("\n"), lineCnt))
same := o.op.buf.RuneSlice(-o.candidateOff)

colIdx := 0
lines := 1
buf.WriteString("\033[J")
Expand All @@ -202,11 +210,11 @@ func (o *opCompleter) CompleteRefresh() {
buf.WriteString(string(same))
buf.WriteString(string(c))
buf.Write(bytes.Repeat([]byte(" "), colWidth-len(c)))

if inSelect {
buf.WriteString("\033[0m")
}

buf.WriteString(" ")
colIdx++
if colIdx == colNum {
buf.WriteString("\n")
Expand All @@ -218,7 +226,7 @@ func (o *opCompleter) CompleteRefresh() {
// move back
fmt.Fprintf(buf, "\033[%dA\r", lineCnt-1+lines)
fmt.Fprintf(buf, "\033[%dC", o.op.buf.idx+o.op.buf.PromptLen())
o.w.Write(buf.Bytes())
buf.Flush()
}

func (o *opCompleter) aggCandidate(candidate [][]rune) int {
Expand Down

0 comments on commit d91f50f

Please sign in to comment.