readline/runebuf.go

479 lines
8.1 KiB
Go
Raw Normal View History

2015-09-20 18:14:29 +03:00
package readline
import (
"bytes"
2016-03-05 10:27:12 +03:00
"fmt"
2015-09-20 18:14:29 +03:00
"io"
2015-11-20 15:56:42 +03:00
"strings"
2015-10-04 16:56:34 +03:00
"github.com/chzyer/readline/runes"
2015-09-20 18:14:29 +03:00
)
2015-10-02 05:37:21 +03:00
type runeBufferBck struct {
buf []rune
idx int
}
2015-09-20 18:14:29 +03:00
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
interactive bool
cfg *Config
2015-10-02 05:37:21 +03:00
bck *runeBufferBck
}
func (r *RuneBuffer) Backup() {
r.bck = &runeBufferBck{r.buf, r.idx}
}
func (r *RuneBuffer) Restore() {
r.Refresh(func() {
if r.bck == nil {
return
}
r.buf = r.bck.buf
r.idx = r.bck.idx
})
2015-09-20 18:14:29 +03:00
}
2016-03-05 05:46:11 +03:00
func NewRuneBuffer(w io.Writer, prompt string, cfg *Config) *RuneBuffer {
2015-09-20 18:14:29 +03:00
rb := &RuneBuffer{
w: w,
interactive: cfg.useInteractive(),
cfg: cfg,
2015-09-20 18:14:29 +03:00
}
2015-09-27 13:54:26 +03:00
rb.SetPrompt(prompt)
2015-09-20 18:14:29 +03:00
return rb
}
func (r *RuneBuffer) SetConfig(cfg *Config) {
r.cfg = cfg
r.interactive = cfg.useInteractive()
}
2015-11-20 15:56:42 +03:00
func (r *RuneBuffer) SetMask(m rune) {
2016-03-05 05:46:11 +03:00
r.cfg.MaskRune = m
2015-11-20 15:56:42 +03:00
}
func (r *RuneBuffer) CurrentWidth(x int) int {
2015-10-04 16:56:34 +03:00
return runes.WidthAll(r.buf[:x])
}
2015-09-23 09:52:45 +03:00
func (r *RuneBuffer) PromptLen() int {
2015-10-04 16:56:34 +03:00
return runes.WidthAll(runes.ColorFilter(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() {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
2015-09-28 06:13:39 +03:00
if r.idx == 0 {
return
}
2015-09-27 05:12:15 +03:00
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-27 05:12:15 +03:00
r.Refresh(func() {
2015-09-28 06:13:39 +03:00
if r.idx == 0 {
return
}
2015-09-27 05:12:15 +03:00
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) IsCursorInEnd() bool {
return r.idx == len(r.buf)
}
func (r *RuneBuffer) Replace(ch rune) {
r.Refresh(func() {
r.buf[r.idx] = ch
})
}
2015-10-02 05:37:21 +03:00
func (r *RuneBuffer) Erase() {
r.Refresh(func() {
r.idx = 0
r.buf = r.buf[:0]
})
}
2015-10-01 17:44:43 +03:00
func (r *RuneBuffer) Delete() (success bool) {
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-10-01 17:44:43 +03:00
success = true
2015-09-27 05:12:15 +03:00
})
2015-10-01 17:44:43 +03:00
return
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
}
2015-10-01 17:44:43 +03:00
func (r *RuneBuffer) MoveToPrevWord() (success bool) {
2015-09-28 06:13:39 +03:00
r.Refresh(func() {
if r.idx == 0 {
2015-09-20 18:14:29 +03:00
return
}
2015-09-28 06:13:39 +03:00
for i := r.idx - 1; i > 0; i-- {
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
r.idx = i
2015-10-01 17:44:43 +03:00
success = true
2015-09-28 06:13:39 +03:00
return
}
}
2015-09-27 05:12:15 +03:00
r.idx = 0
2015-10-01 17:44:43 +03:00
success = true
2015-09-27 05:12:15 +03:00
})
2015-10-01 17:44:43 +03:00
return
2015-09-20 18:14:29 +03:00
}
2015-09-28 06:13:39 +03:00
func (r *RuneBuffer) KillFront() {
r.Refresh(func() {
if r.idx == 0 {
return
}
length := len(r.buf) - r.idx
copy(r.buf[:length], r.buf[r.idx:])
r.idx = 0
r.buf = r.buf[:length]
})
2015-09-20 18:14:29 +03:00
}
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-28 06:13:39 +03:00
r.Refresh(func() {
2015-09-23 06:59:39 +03:00
if len(r.buf) == 1 {
2015-09-28 06:13:39 +03:00
r.idx++
2015-09-23 06:59:39 +03:00
}
2015-09-28 06:13:39 +03:00
if len(r.buf) < 2 {
return
}
2015-09-27 05:12:15 +03:00
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() {
2015-09-28 06:13:39 +03:00
r.Refresh(func() {
for i := r.idx + 1; i < len(r.buf); i++ {
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
2015-09-27 05:12:15 +03:00
r.idx = i
2015-09-28 06:13:39 +03:00
return
}
2015-09-20 18:14:29 +03:00
}
2015-09-28 06:13:39 +03:00
2015-09-27 05:12:15 +03:00
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() {
2015-09-28 06:13:39 +03:00
r.Refresh(func() {
if r.idx == 0 {
return
}
for i := r.idx - 1; i > 0; i-- {
if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) {
2015-09-27 05:12:15 +03:00
r.buf = append(r.buf[:i], r.buf[r.idx:]...)
r.idx = i
2015-09-28 06:13:39 +03:00
return
}
2015-09-21 16:00:48 +03:00
}
2015-09-27 05:12:15 +03:00
r.buf = r.buf[:0]
r.idx = 0
})
2015-09-21 16:00:48 +03:00
}
func (r *RuneBuffer) Backspace() {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
2015-09-28 06:13:39 +03:00
if r.idx == 0 {
return
}
2015-09-27 05:12:15 +03:00
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() {
2015-09-27 05:12:15 +03:00
r.Refresh(func() {
2015-09-28 06:13:39 +03:00
if r.idx == len(r.buf) {
return
}
2015-09-27 05:12:15 +03:00
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 {
2016-03-05 10:27:12 +03:00
return LineCount(r.cfg.FuncGetWidth(),
runes.WidthAll(r.buf)+r.PromptLen())
2015-09-22 18:01:15 +03:00
}
2015-10-01 17:44:43 +03:00
func (r *RuneBuffer) MoveTo(ch rune, prevChar, reverse bool) (success bool) {
r.Refresh(func() {
if reverse {
for i := r.idx - 1; i >= 0; i-- {
if r.buf[i] == ch {
r.idx = i
if prevChar {
r.idx++
}
success = true
return
}
}
return
}
for i := r.idx + 1; i < len(r.buf); i++ {
if r.buf[i] == ch {
r.idx = i
if prevChar {
r.idx--
}
success = true
return
}
}
})
return
}
2015-09-22 18:01:15 +03:00
func (r *RuneBuffer) IdxLine() int {
2016-03-05 10:27:12 +03:00
sw := r.cfg.FuncGetWidth()
sp := SplitByLine(r.PromptLen(), sw, r.buf[:r.idx])
return len(sp) - 1
2015-09-22 18:01:15 +03:00
}
func (r *RuneBuffer) CursorLineCount() int {
return r.LineCount() - r.IdxLine()
}
2015-09-27 05:12:15 +03:00
func (r *RuneBuffer) Refresh(f func()) {
if !r.interactive {
if f != nil {
f()
}
return
}
2015-09-27 05:12:15 +03:00
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))
2016-03-05 05:46:11 +03:00
if r.cfg.EnableMask && len(r.buf) > 0 {
buf.Write([]byte(strings.Repeat(string(r.cfg.MaskRune), len(r.buf)-1)))
2015-11-20 15:56:42 +03:00
if r.buf[len(r.buf)-1] == '\n' {
buf.Write([]byte{'\n'})
} else {
2016-03-05 05:46:11 +03:00
buf.Write([]byte(string(r.cfg.MaskRune)))
2015-11-20 15:56:42 +03:00
}
2016-03-05 10:27:12 +03:00
if len(r.buf) > r.idx {
buf.Write(runes.Backspace(r.buf[r.idx:]))
}
2015-11-20 15:56:42 +03:00
} else {
2016-03-05 10:27:12 +03:00
sw := r.cfg.FuncGetWidth()
sp := SplitByLine(r.PromptLen(), sw, r.buf)
written := 0
idxInLine := 0
for idx, s := range sp {
buf.Write([]byte(s))
if r.idx > written && r.idx < written+len(s) {
idxInLine = r.idx - written
}
written += len(s)
if idx < len(sp)-1 && !isWindows {
buf.Write([]byte{'\n'})
}
}
if len(r.buf) > r.idx {
targetLine := r.IdxLine()
currentLine := len(sp) - 1
// assert currentLine >= targetLine
if targetLine == 0 {
idxInLine += r.PromptLen()
}
buf.WriteString("\r")
if currentLine > targetLine {
buf.WriteString(fmt.Sprintf(
"\033[%vA", currentLine-targetLine,
))
}
if idxInLine > 0 {
buf.WriteString(fmt.Sprintf(
"\033[%vC", idxInLine,
))
}
}
2015-09-23 06:10:36 +03:00
}
2015-09-20 18:14:29 +03:00
return buf.Bytes()
}
func (r *RuneBuffer) Reset() []rune {
2016-02-17 17:20:03 +03:00
ret := runes.Copy(r.buf)
2015-09-20 18:14:29 +03:00
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 {
2015-10-04 16:56:34 +03:00
return runes.WidthAll(r.buf[r.idx : r.idx+m])
}
2015-10-04 16:56:34 +03:00
return runes.WidthAll(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
}
2015-09-29 12:49:58 +03:00
r.w.Write([]byte("\033[" + style + "m"))
2015-09-23 06:10:36 +03:00
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)
}
2015-09-28 19:26:49 +03:00
func (r *RuneBuffer) SetPrompt(prompt string) {
r.prompt = []rune(prompt)
}
func (r *RuneBuffer) cleanOutput() []byte {
buf := bytes.NewBuffer(nil)
buf.Write([]byte("\033[J")) // just like ^k :)
idxLine := r.IdxLine()
2015-09-28 19:26:49 +03:00
if idxLine == 0 {
buf.WriteString("\033[2K\r")
return buf.Bytes()
}
for i := 0; i < idxLine; i++ {
buf.WriteString("\033[2K\r\033[A")
2015-09-28 19:26:49 +03:00
}
buf.WriteString("\033[2K\r")
2015-09-28 19:26:49 +03:00
return buf.Bytes()
}
func (r *RuneBuffer) Clean() {
if r.cleanInScreen || !r.interactive {
2015-09-28 19:26:49 +03:00
return
}
r.cleanInScreen = true
r.w.Write(r.cleanOutput())
}