[complete] avoid reach end of line in windows

This commit is contained in:
chzyer 2016-07-26 21:33:15 +08:00
parent f83f3269ca
commit dc578a10ae
1 changed files with 14 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package readline package readline
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
@ -186,11 +187,18 @@ func (o *opCompleter) CompleteRefresh() {
colWidth = w colWidth = w
} }
} }
colNum := o.width / (colWidth + o.candidateOff + 2) colWidth += o.candidateOff + 1
o.candidateColNum = colNum
buf := bytes.NewBuffer(nil)
buf.Write(bytes.Repeat([]byte("\n"), lineCnt))
same := o.op.buf.RuneSlice(-o.candidateOff) 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 := bufio.NewWriter(o.w)
buf.Write(bytes.Repeat([]byte("\n"), lineCnt))
colIdx := 0 colIdx := 0
lines := 1 lines := 1
buf.WriteString("\033[J") buf.WriteString("\033[J")
@ -202,11 +210,11 @@ func (o *opCompleter) CompleteRefresh() {
buf.WriteString(string(same)) buf.WriteString(string(same))
buf.WriteString(string(c)) buf.WriteString(string(c))
buf.Write(bytes.Repeat([]byte(" "), colWidth-len(c))) buf.Write(bytes.Repeat([]byte(" "), colWidth-len(c)))
if inSelect { if inSelect {
buf.WriteString("\033[0m") buf.WriteString("\033[0m")
} }
buf.WriteString(" ")
colIdx++ colIdx++
if colIdx == colNum { if colIdx == colNum {
buf.WriteString("\n") buf.WriteString("\n")
@ -218,7 +226,7 @@ func (o *opCompleter) CompleteRefresh() {
// move back // move back
fmt.Fprintf(buf, "\033[%dA\r", lineCnt-1+lines) fmt.Fprintf(buf, "\033[%dA\r", lineCnt-1+lines)
fmt.Fprintf(buf, "\033[%dC", o.op.buf.idx+o.op.buf.PromptLen()) 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 { func (o *opCompleter) aggCandidate(candidate [][]rune) int {