fix runebuf

This commit is contained in:
Cheney 2015-09-22 17:36:52 +08:00
parent c9aba7b858
commit f8c012aa53
1 changed files with 11 additions and 23 deletions

View File

@ -178,37 +178,25 @@ func (r *RuneBuffer) RefreshSet(originLength, originIdx int) {
func (r *RuneBuffer) Output(originLength, originIdx int, prompt bool) []byte { func (r *RuneBuffer) Output(originLength, originIdx int, prompt bool) []byte {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
buf.Write(bytes.Repeat([]byte{'\b'}, originIdx)) buf.Write(r.CleanOutput())
if prompt { buf.Write(r.prompt)
if r.hasPrompt {
buf.Write(bytes.Repeat([]byte{'\b'}, len(r.prompt)))
}
buf.Write(r.prompt)
}
r.hasPrompt = prompt r.hasPrompt = prompt
buf.Write([]byte(string(r.buf))) buf.Write([]byte(string(r.buf)))
if originLength > len(r.buf) {
buf.Write(bytes.Repeat([]byte{' '}, originLength-len(r.buf)))
buf.Write(bytes.Repeat([]byte{'\b'}, originLength-len(r.buf)))
}
buf.Write(bytes.Repeat([]byte{'\b'}, len(r.buf)-r.idx)) buf.Write(bytes.Repeat([]byte{'\b'}, len(r.buf)-r.idx))
return buf.Bytes() return buf.Bytes()
} }
func (r *RuneBuffer) Clean() { func (r *RuneBuffer) CleanOutput() []byte {
moveToFirst := r.idx buf := bytes.NewBuffer(nil)
if r.hasPrompt { buf.Write([]byte("\033[J"))
moveToFirst += len(r.prompt) for i := 0; i <= 100; i++ {
buf.WriteString("\033[2K\r\b")
} }
r.w.Write(bytes.Repeat([]byte{'\b'}, moveToFirst)) return buf.Bytes()
length := len(r.buf) }
if r.hasPrompt {
length += len(r.prompt)
}
r.w.Write(bytes.Repeat([]byte{' '}, length))
r.w.Write(bytes.Repeat([]byte{'\b'}, length)) func (r *RuneBuffer) Clean() {
r.hasPrompt = false r.w.Write(r.CleanOutput())
} }
func (r *RuneBuffer) ResetScreen() { func (r *RuneBuffer) ResetScreen() {