refactor runebuffer

This commit is contained in:
Cheney 2015-09-22 17:41:14 +08:00
parent f8c012aa53
commit c8f8ec4b96
2 changed files with 29 additions and 35 deletions

View File

@ -39,7 +39,7 @@ func (w *wrapWriter) Write(b []byte) (int, error) {
buf := w.r.buf buf := w.r.buf
buf.Clean() buf.Clean()
n, err := w.target.Write(b) n, err := w.target.Write(b)
w.r.buf.RefreshSet(0, 0) w.r.buf.Refresh()
return n, err return n, err
} }
@ -121,7 +121,7 @@ func (l *Operation) String() (string, error) {
} }
func (l *Operation) Runes() ([]rune, error) { func (l *Operation) Runes() ([]rune, error) {
l.buf.Refresh(0, 0) // print prompt l.buf.Refresh() // print prompt
r := <-l.outchan r := <-l.outchan
if r == nil { if r == nil {
return nil, io.EOF return nil, io.EOF

View File

@ -38,7 +38,8 @@ func (r *RuneBuffer) MoveToLineStart() {
if r.idx == 0 { if r.idx == 0 {
return return
} }
r.Refresh(-1, r.SetIdx(0)) r.idx = 0
r.Refresh()
} }
func (r *RuneBuffer) MoveBackward() { func (r *RuneBuffer) MoveBackward() {
@ -46,7 +47,7 @@ func (r *RuneBuffer) MoveBackward() {
return return
} }
r.idx-- r.idx--
r.Refresh(0, -1) r.Refresh()
} }
func (rb *RuneBuffer) WriteString(s string) { func (rb *RuneBuffer) WriteString(s string) {
@ -61,7 +62,7 @@ func (rb *RuneBuffer) WriteRunes(r []rune) {
tail := append(r, rb.buf[rb.idx:]...) tail := append(r, rb.buf[rb.idx:]...)
rb.buf = append(rb.buf[:rb.idx], tail...) rb.buf = append(rb.buf[:rb.idx], tail...)
rb.idx++ rb.idx++
rb.Refresh(1, 1) rb.Refresh()
} }
func (r *RuneBuffer) MoveForward() { func (r *RuneBuffer) MoveForward() {
@ -69,7 +70,7 @@ func (r *RuneBuffer) MoveForward() {
return return
} }
r.idx++ r.idx++
r.Refresh(0, 1) r.Refresh()
} }
func (r *RuneBuffer) Delete() { func (r *RuneBuffer) Delete() {
@ -77,7 +78,7 @@ func (r *RuneBuffer) Delete() {
return return
} }
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...) r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
r.Refresh(-1, 0) r.Refresh()
} }
func (r *RuneBuffer) DeleteWord() { func (r *RuneBuffer) DeleteWord() {
@ -91,7 +92,7 @@ func (r *RuneBuffer) DeleteWord() {
for i := init + 1; i < len(r.buf); i++ { for i := init + 1; i < len(r.buf); i++ {
if r.buf[i] != ' ' && r.buf[i-1] == ' ' { if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
r.buf = append(r.buf[:r.idx], r.buf[i-1:]...) r.buf = append(r.buf[:r.idx], r.buf[i-1:]...)
r.Refresh(r.idx-i+1, 0) r.Refresh()
return return
} }
} }
@ -104,11 +105,13 @@ func (r *RuneBuffer) MoveToPrevWord() {
} }
for i := r.idx - 1; i > 0; i-- { for i := r.idx - 1; i > 0; i-- {
if r.buf[i] != ' ' && r.buf[i-1] == ' ' { if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
r.Refresh(0, r.SetIdx(i)) r.idx = i
r.Refresh()
return return
} }
} }
r.Refresh(0, r.SetIdx(0)) r.idx = 0
r.Refresh()
} }
func (r *RuneBuffer) SetIdx(idx int) (change int) { func (r *RuneBuffer) SetIdx(idx int) (change int) {
@ -118,19 +121,20 @@ func (r *RuneBuffer) SetIdx(idx int) (change int) {
} }
func (r *RuneBuffer) Kill() { func (r *RuneBuffer) Kill() {
length := len(r.buf)
r.buf = r.buf[:r.idx] r.buf = r.buf[:r.idx]
r.Refresh(r.idx-length, 0) r.Refresh()
} }
func (r *RuneBuffer) MoveToNextWord() { func (r *RuneBuffer) MoveToNextWord() {
for i := r.idx + 1; i < len(r.buf); i++ { for i := r.idx + 1; i < len(r.buf); i++ {
if r.buf[i] != ' ' && r.buf[i-1] == ' ' { if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
r.Refresh(0, r.SetIdx(i)) r.idx = i
r.Refresh()
return return
} }
} }
r.Refresh(0, r.SetIdx(len(r.buf))) r.idx = len(r.buf)
r.Refresh()
} }
func (r *RuneBuffer) BackEscapeWord() { func (r *RuneBuffer) BackEscapeWord() {
@ -139,16 +143,16 @@ func (r *RuneBuffer) BackEscapeWord() {
} }
for i := r.idx - 1; i > 0; i-- { for i := r.idx - 1; i > 0; i-- {
if r.buf[i] != ' ' && r.buf[i-1] == ' ' { if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
change := i - r.idx
r.buf = append(r.buf[:i], r.buf[r.idx:]...) r.buf = append(r.buf[:i], r.buf[r.idx:]...)
r.Refresh(change, r.SetIdx(i)) r.idx = i
r.Refresh()
return return
} }
} }
length := len(r.buf)
r.buf = r.buf[:0] r.buf = r.buf[:0]
r.Refresh(-length, r.SetIdx(0)) r.idx = 0
r.Refresh()
} }
func (r *RuneBuffer) Backspace() { func (r *RuneBuffer) Backspace() {
@ -157,30 +161,25 @@ func (r *RuneBuffer) Backspace() {
} }
r.idx-- r.idx--
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...) r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
r.Refresh(-1, -1) r.Refresh()
} }
func (r *RuneBuffer) MoveToLineEnd() { func (r *RuneBuffer) MoveToLineEnd() {
if r.idx == len(r.buf) { if r.idx == len(r.buf) {
return return
} }
r.Refresh(0, r.SetIdx(len(r.buf))) r.idx = len(r.buf)
r.Refresh()
} }
func (r *RuneBuffer) Refresh(chlen, chidx int) { func (r *RuneBuffer) Refresh() {
s := r.Output(len(r.buf)-chlen, r.idx-chidx, true) r.w.Write(r.Output())
r.w.Write(s)
} }
func (r *RuneBuffer) RefreshSet(originLength, originIdx int) { func (r *RuneBuffer) Output() []byte {
r.w.Write(r.Output(originLength, originIdx, true))
}
func (r *RuneBuffer) Output(originLength, originIdx int, prompt bool) []byte {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
buf.Write(r.CleanOutput()) buf.Write(r.CleanOutput())
buf.Write(r.prompt) buf.Write(r.prompt)
r.hasPrompt = prompt
buf.Write([]byte(string(r.buf))) buf.Write([]byte(string(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()
@ -199,10 +198,6 @@ func (r *RuneBuffer) Clean() {
r.w.Write(r.CleanOutput()) r.w.Write(r.CleanOutput())
} }
func (r *RuneBuffer) ResetScreen() {
r.w.Write(r.Output(0, 0, false))
}
func (r *RuneBuffer) Reset() []rune { func (r *RuneBuffer) Reset() []rune {
ret := r.buf ret := r.buf
r.buf = r.buf[:0] r.buf = r.buf[:0]
@ -212,8 +207,7 @@ func (r *RuneBuffer) Reset() []rune {
} }
func (r *RuneBuffer) Set(buf []rune) { func (r *RuneBuffer) Set(buf []rune) {
length, idx := len(r.buf), r.idx
r.buf = buf r.buf = buf
r.idx = len(r.buf) r.idx = len(r.buf)
r.RefreshSet(length, idx) r.Refresh()
} }