2015-09-20 18:14:29 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RuneBuffer struct {
|
|
|
|
buf []rune
|
|
|
|
idx int
|
|
|
|
prompt []byte
|
|
|
|
w io.Writer
|
2015-09-21 17:27:40 +03:00
|
|
|
hasPrompt bool
|
2015-09-20 18:14:29 +03:00
|
|
|
lastWritten int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer {
|
|
|
|
rb := &RuneBuffer{
|
2015-09-21 17:27:40 +03:00
|
|
|
prompt: []byte(prompt),
|
|
|
|
w: w,
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
return rb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Runes() []rune {
|
|
|
|
return r.buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Pos() int {
|
|
|
|
return r.idx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Len() int {
|
|
|
|
return len(r.buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) MoveToLineStart() {
|
|
|
|
if r.idx == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Refresh(-1, r.SetIdx(0))
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:13:30 +03:00
|
|
|
func (r *RuneBuffer) MoveBackward() {
|
2015-09-20 18:14:29 +03:00
|
|
|
if r.idx == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.idx--
|
|
|
|
r.Refresh(0, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb *RuneBuffer) WriteString(s string) {
|
|
|
|
rb.WriteRunes([]rune(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb *RuneBuffer) WriteRune(r rune) {
|
|
|
|
rb.WriteRunes([]rune{r})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rb *RuneBuffer) WriteRunes(r []rune) {
|
|
|
|
tail := append(r, rb.buf[rb.idx:]...)
|
|
|
|
rb.buf = append(rb.buf[:rb.idx], tail...)
|
|
|
|
rb.idx++
|
|
|
|
rb.Refresh(1, 1)
|
|
|
|
}
|
|
|
|
|
2015-09-21 08:13:30 +03:00
|
|
|
func (r *RuneBuffer) MoveForward() {
|
2015-09-20 18:14:29 +03:00
|
|
|
if r.idx == len(r.buf) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.idx++
|
|
|
|
r.Refresh(0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Delete() {
|
|
|
|
if r.idx == len(r.buf) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
|
|
|
|
r.Refresh(-1, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) DeleteWord() {
|
|
|
|
if r.idx == len(r.buf) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := r.idx + 1; i < len(r.buf); i++ {
|
|
|
|
if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
|
|
|
|
r.buf = append(r.buf[:r.idx], r.buf[i-1:]...)
|
|
|
|
r.Refresh(r.idx-i+1, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length := len(r.buf)
|
|
|
|
r.buf = r.buf[:r.idx]
|
|
|
|
r.Refresh(length-r.idx, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) MoveToPrevWord() {
|
|
|
|
if r.idx == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := r.idx - 1; i > 0; i-- {
|
|
|
|
if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
|
|
|
|
r.Refresh(0, r.SetIdx(i))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Refresh(0, r.SetIdx(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) SetIdx(idx int) (change int) {
|
|
|
|
i := r.idx
|
|
|
|
r.idx = idx
|
|
|
|
return r.idx - i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) MoveToNextWord() {
|
|
|
|
for i := r.idx + 1; i < len(r.buf); i++ {
|
|
|
|
if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
|
|
|
|
r.Refresh(0, r.SetIdx(i))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Refresh(0, r.SetIdx(len(r.buf)))
|
|
|
|
}
|
|
|
|
|
2015-09-21 16:00:48 +03:00
|
|
|
func (r *RuneBuffer) BackEscapeWord() {
|
|
|
|
if r.idx == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := r.idx - 1; i > 0; i-- {
|
|
|
|
if r.buf[i] != ' ' && r.buf[i-1] == ' ' {
|
|
|
|
change := i - r.idx
|
|
|
|
r.buf = append(r.buf[:i], r.buf[r.idx:]...)
|
|
|
|
r.Refresh(change, r.SetIdx(i))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length := len(r.buf)
|
|
|
|
r.buf = r.buf[:0]
|
|
|
|
r.Refresh(-length, r.SetIdx(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Backspace() {
|
2015-09-20 18:14:29 +03:00
|
|
|
if r.idx == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.idx--
|
|
|
|
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
|
|
|
|
r.Refresh(-1, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) MoveToLineEnd() {
|
|
|
|
if r.idx == len(r.buf) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Refresh(0, r.SetIdx(len(r.buf)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Refresh(chlen, chidx int) {
|
2015-09-21 17:27:40 +03:00
|
|
|
s := r.Output(len(r.buf)-chlen, r.idx-chidx, true)
|
2015-09-20 18:14:29 +03:00
|
|
|
r.w.Write(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) RefreshSet(originLength, originIdx int) {
|
2015-09-21 17:27:40 +03:00
|
|
|
r.w.Write(r.Output(originLength, originIdx, true))
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
2015-09-21 17:27:40 +03:00
|
|
|
func (r *RuneBuffer) Output(originLength, originIdx int, prompt bool) []byte {
|
2015-09-20 18:14:29 +03:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2015-09-21 17:27:40 +03:00
|
|
|
buf.Write(bytes.Repeat([]byte{'\b'}, originIdx))
|
|
|
|
if prompt {
|
|
|
|
if r.hasPrompt {
|
|
|
|
buf.Write(bytes.Repeat([]byte{'\b'}, len(r.prompt)))
|
|
|
|
}
|
|
|
|
buf.Write(r.prompt)
|
|
|
|
}
|
|
|
|
r.hasPrompt = prompt
|
2015-09-20 18:14:29 +03:00
|
|
|
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))
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Clean() {
|
|
|
|
moveToFirst := r.idx
|
2015-09-21 17:27:40 +03:00
|
|
|
if r.hasPrompt {
|
|
|
|
moveToFirst += len(r.prompt)
|
|
|
|
}
|
2015-09-20 18:14:29 +03:00
|
|
|
r.w.Write(bytes.Repeat([]byte{'\b'}, moveToFirst))
|
2015-09-21 17:27:40 +03:00
|
|
|
length := len(r.buf)
|
|
|
|
if r.hasPrompt {
|
|
|
|
length += len(r.prompt)
|
|
|
|
}
|
2015-09-20 18:14:29 +03:00
|
|
|
r.w.Write(bytes.Repeat([]byte{' '}, length))
|
2015-09-21 17:27:40 +03:00
|
|
|
|
2015-09-20 18:14:29 +03:00
|
|
|
r.w.Write(bytes.Repeat([]byte{'\b'}, length))
|
2015-09-21 17:27:40 +03:00
|
|
|
r.hasPrompt = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) ResetScreen() {
|
|
|
|
r.w.Write(r.Output(0, 0, false))
|
2015-09-20 18:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuneBuffer) Reset() []rune {
|
|
|
|
ret := r.buf
|
|
|
|
r.buf = r.buf[:0]
|
|
|
|
r.idx = 0
|
2015-09-21 17:27:40 +03:00
|
|
|
r.hasPrompt = false
|
2015-09-20 18:14:29 +03:00
|
|
|
return ret
|
|
|
|
}
|
2015-09-21 08:13:30 +03:00
|
|
|
|
|
|
|
func (r *RuneBuffer) Set(buf []rune) {
|
|
|
|
length, idx := len(r.buf), r.idx
|
|
|
|
r.buf = buf
|
|
|
|
r.idx = len(r.buf)
|
|
|
|
r.RefreshSet(length, idx)
|
|
|
|
}
|