readline/runebuf.go

337 lines
5.6 KiB
Go
Raw Normal View History

2015-09-20 18:14:29 +03:00
package readline
import (
"bytes"
"io"
)
type RuneBuffer struct {
2015-09-22 13:16:24 +03:00
buf []rune
idx int
2015-09-23 09:52:45 +03:00
prompt []rune
2015-09-22 13:16:24 +03:00
w io.Writer
2015-09-27 05:12:15 +03:00
cleanInScreen bool
2015-09-20 18:14:29 +03:00
}
func NewRuneBuffer(w io.Writer, prompt string) *RuneBuffer {
rb := &RuneBuffer{
2015-09-23 09:52:45 +03:00
prompt: []rune(prompt),
2015-09-21 17:27:40 +03:00
w: w,
2015-09-20 18:14:29 +03:00
}
return rb
}
func (r *RuneBuffer) CurrentWidth(x int) int {
return RunesWidth(r.buf[:x])
}
2015-09-23 09:52:45 +03:00
func (r *RuneBuffer) PromptLen() int {
return RunesWidth(RunesColorFilter(r.prompt))
2015-09-23 09:52:45 +03:00
}
2015-09-25 17:56:00 +03:00
func (r *RuneBuffer) RuneSlice(i int) []rune {
if i > 0 {
rs := make([]rune, i)
copy(rs, r.buf[r.idx:r.idx+i])
return rs
}
rs := make([]rune, -i)
copy(rs, r.buf[r.idx+i:r.idx])
return rs
}
2015-09-20 18:14:29 +03:00
func (r *RuneBuffer) Runes() []rune {
2015-09-25 17:56:00 +03:00
newr := make([]rune, len(r.buf))
copy(newr, r.buf)
return newr
2015-09-20 18:14:29 +03:00
}
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
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx = 0
})
2015-09-20 18:14:29 +03:00
}
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
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx--
})
2015-09-20 18:14:29 +03:00
}
2015-09-23 08:52:26 +03:00
func (r *RuneBuffer) WriteString(s string) {
r.WriteRunes([]rune(s))
2015-09-20 18:14:29 +03:00
}
2015-09-23 08:52:26 +03:00
func (r *RuneBuffer) WriteRune(s rune) {
r.WriteRunes([]rune{s})
2015-09-20 18:14:29 +03:00
}
2015-09-23 08:52:26 +03:00
func (r *RuneBuffer) WriteRunes(s []rune) {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
tail := append(s, r.buf[r.idx:]...)
r.buf = append(r.buf[:r.idx], tail...)
r.idx += len(s)
})
2015-09-20 18:14:29 +03:00
}
2015-09-21 08:13:30 +03:00
func (r *RuneBuffer) MoveForward() {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
if r.idx == len(r.buf) {
return
}
r.idx++
})
2015-09-20 18:14:29 +03:00
}
func (r *RuneBuffer) Delete() {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
if r.idx == len(r.buf) {
return
}
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
})
2015-09-20 18:14:29 +03:00
}
func (r *RuneBuffer) DeleteWord() {
if r.idx == len(r.buf) {
return
}
2015-09-21 17:51:48 +03:00
init := r.idx
2015-09-23 06:59:39 +03:00
for init < len(r.buf) && IsWordBreak(r.buf[init]) {
2015-09-21 17:51:48 +03:00
init++
}
for i := init + 1; i < len(r.buf); i++ {
2015-09-23 06:59:39 +03:00
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.buf = append(r.buf[:r.idx], r.buf[i-1:]...)
})
2015-09-20 18:14:29 +03:00
return
}
}
2015-09-21 17:51:48 +03:00
r.Kill()
2015-09-20 18:14:29 +03:00
}
func (r *RuneBuffer) MoveToPrevWord() {
if r.idx == 0 {
return
}
for i := r.idx - 1; i > 0; i-- {
2015-09-23 06:59:39 +03:00
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx = i
})
2015-09-20 18:14:29 +03:00
return
}
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx = 0
})
2015-09-20 18:14:29 +03:00
}
func (r *RuneBuffer) SetIdx(idx int) (change int) {
i := r.idx
r.idx = idx
return r.idx - i
}
2015-09-21 17:51:48 +03:00
func (r *RuneBuffer) Kill() {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.buf = r.buf[:r.idx]
})
2015-09-21 17:51:48 +03:00
}
2015-09-23 08:03:13 +03:00
func (r *RuneBuffer) Transpose() {
2015-09-23 06:59:39 +03:00
if len(r.buf) < 2 {
if len(r.buf) == 1 {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx++
})
2015-09-23 06:59:39 +03:00
}
return
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
if r.idx == 0 {
r.idx = 1
} else if r.idx >= len(r.buf) {
r.idx = len(r.buf) - 1
}
r.buf[r.idx], r.buf[r.idx-1] = r.buf[r.idx-1], r.buf[r.idx]
r.idx++
})
2015-09-23 06:46:56 +03:00
}
2015-09-20 18:14:29 +03:00
func (r *RuneBuffer) MoveToNextWord() {
for i := r.idx + 1; i < len(r.buf); i++ {
2015-09-23 06:59:39 +03:00
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx = i
})
2015-09-20 18:14:29 +03:00
return
}
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx = len(r.buf)
})
2015-09-20 18:14:29 +03:00
}
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-- {
2015-09-23 06:59:39 +03:00
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.buf = append(r.buf[:i], r.buf[r.idx:]...)
r.idx = i
})
2015-09-21 16:00:48 +03:00
return
}
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.buf = r.buf[:0]
r.idx = 0
})
2015-09-21 16:00:48 +03:00
}
func (r *RuneBuffer) Backspace() {
2015-09-20 18:14:29 +03:00
if r.idx == 0 {
return
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx--
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
})
2015-09-20 18:14:29 +03:00
}
func (r *RuneBuffer) MoveToLineEnd() {
if r.idx == len(r.buf) {
return
}
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.idx = len(r.buf)
})
2015-09-20 18:14:29 +03:00
}
2015-09-22 18:01:15 +03:00
func (r *RuneBuffer) LineCount() int {
2015-09-23 09:52:45 +03:00
return LineCount(RunesWidth(r.buf) + r.PromptLen())
2015-09-22 18:01:15 +03:00
}
func (r *RuneBuffer) IdxLine() int {
2015-09-23 09:52:45 +03:00
totalWidth := RunesWidth(r.buf[:r.idx]) + r.PromptLen()
2015-09-22 18:01:15 +03:00
w := getWidth()
2015-09-27 05:12:15 +03:00
line := totalWidth / w
if totalWidth%w == 0 {
line--
2015-09-22 18:01:15 +03:00
}
return line
}
func (r *RuneBuffer) CursorLineCount() int {
return r.LineCount() - r.IdxLine()
}
2015-09-27 05:12:15 +03:00
func (r *RuneBuffer) Refresh(f func()) {
r.Clean()
if f != nil {
f()
}
r.w.Write(r.output())
r.cleanInScreen = false
2015-09-20 18:14:29 +03:00
}
2015-09-27 05:12:15 +03:00
func (r *RuneBuffer) output() []byte {
2015-09-20 18:14:29 +03:00
buf := bytes.NewBuffer(nil)
2015-09-23 09:52:45 +03:00
buf.WriteString(string(r.prompt))
2015-09-20 18:14:29 +03:00
buf.Write([]byte(string(r.buf)))
2015-09-23 06:10:36 +03:00
if len(r.buf) > r.idx {
buf.Write(bytes.Repeat([]byte{'\b'}, RunesWidth(r.buf[r.idx:])))
2015-09-23 06:10:36 +03:00
}
2015-09-20 18:14:29 +03:00
return buf.Bytes()
}
2015-09-22 12:36:52 +03:00
func (r *RuneBuffer) CleanOutput() []byte {
buf := bytes.NewBuffer(nil)
2015-09-22 13:16:24 +03:00
buf.Write([]byte("\033[J")) // just like ^k :)
2015-09-27 05:12:15 +03:00
idxLine := r.IdxLine()
if idxLine == 0 {
buf.WriteString("\033[2K\r")
return buf.Bytes()
}
for i := 0; i < idxLine; i++ {
2015-09-22 12:36:52 +03:00
buf.WriteString("\033[2K\r\b")
2015-09-21 17:27:40 +03:00
}
2015-09-27 05:12:15 +03:00
buf.WriteString("\033[2K\r")
2015-09-22 12:36:52 +03:00
return buf.Bytes()
}
2015-09-21 17:27:40 +03:00
2015-09-22 12:36:52 +03:00
func (r *RuneBuffer) Clean() {
2015-09-27 05:12:15 +03:00
if r.cleanInScreen {
return
}
r.cleanInScreen = true
2015-09-22 12:36:52 +03:00
r.w.Write(r.CleanOutput())
2015-09-21 17:27:40 +03:00
}
2015-09-20 18:14:29 +03:00
func (r *RuneBuffer) Reset() []rune {
ret := r.buf
r.buf = r.buf[:0]
r.idx = 0
return ret
}
2015-09-21 08:13:30 +03:00
func (r *RuneBuffer) calWidth(m int) int {
if m > 0 {
return RunesWidth(r.buf[r.idx : r.idx+m])
}
return RunesWidth(r.buf[r.idx+m : r.idx])
}
2015-09-23 06:10:36 +03:00
func (r *RuneBuffer) SetStyle(start, end int, style string) {
if end < start {
panic("end < start")
}
// goto start
move := start - r.idx
2015-09-23 06:10:36 +03:00
if move > 0 {
r.w.Write([]byte(string(r.buf[r.idx : r.idx+move])))
} else {
r.w.Write(bytes.Repeat([]byte("\b"), r.calWidth(move)))
2015-09-23 06:10:36 +03:00
}
r.w.Write([]byte("\033[" + style))
r.w.Write([]byte(string(r.buf[start:end])))
r.w.Write([]byte("\033[0m"))
// TODO: move back
2015-09-23 06:10:36 +03:00
}
2015-09-22 18:01:15 +03:00
func (r *RuneBuffer) SetWithIdx(idx int, buf []rune) {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
r.buf = buf
r.idx = idx
})
2015-09-21 08:13:30 +03:00
}
2015-09-22 18:01:15 +03:00
func (r *RuneBuffer) Set(buf []rune) {
r.SetWithIdx(len(buf), buf)
}