1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2013-2015 Bowery, Inc.
package prompt
import (
"os"
"unicode/utf8"
)
// Buffer contains state for line editing and writing.
type Buffer struct {
Out *os.File
Prompt string
Echo bool
Cols int
pos int
size int
data []rune
}
// NewBuffer creates a buffer writing to out if echo is true.
func NewBuffer(prompt string, out *os.File, echo bool) *Buffer {
return &Buffer{
Out: out,
Prompt: prompt,
Echo: echo,
}
}
// String returns the data as a string.
func (buf *Buffer) String() string {
return string(buf.data[:buf.size])
}
// Insert inserts characters at the cursors position.
func (buf *Buffer) Insert(rs ...rune) error {
rsLen := len(rs)
total := buf.size + rsLen
if total > len(buf.data) {
buf.data = append(buf.data, make([]rune, rsLen)...)
}
// Shift characters to make room in the correct pos.
if buf.size != buf.pos {
copy(buf.data[buf.pos+rsLen:buf.size+rsLen], buf.data[buf.pos:buf.size])
}
for _, r := range rs {
buf.data[buf.pos] = r
buf.pos++
buf.size++
}
return buf.Refresh()
}
// Set sets the content in the buffer.
func (buf *Buffer) Set(rs ...rune) error {
rsLen := len(rs)
buf.data = rs
buf.pos = rsLen
buf.size = rsLen
return buf.Refresh()
}
// Start moves the cursor to the start.
func (buf *Buffer) Start() error {
if buf.pos <= 0 {
return nil
}
buf.pos = 0
return buf.Refresh()
}
// End moves the cursor to the end.
func (buf *Buffer) End() error {
if buf.pos >= buf.size {
return nil
}
buf.pos = buf.size
return buf.Refresh()
}
// Left moves the cursor one character left.
func (buf *Buffer) Left() error {
if buf.pos <= 0 {
return nil
}
buf.pos--
return buf.Refresh()
}
// Right moves the cursor one character right.
func (buf *Buffer) Right() error {
if buf.pos >= buf.size {
return nil
}
buf.pos++
return buf.Refresh()
}
// Del removes the character at the cursor position.
func (buf *Buffer) Del() error {
if buf.pos >= buf.size {
return nil
}
// Shift characters after position back one.
copy(buf.data[buf.pos:], buf.data[buf.pos+1:buf.size])
buf.size--
return buf.Refresh()
}
// DelLeft removes the character to the left.
func (buf *Buffer) DelLeft() error {
if buf.pos <= 0 {
return nil
}
// Shift characters from position back one.
copy(buf.data[buf.pos-1:], buf.data[buf.pos:buf.size])
buf.pos--
buf.size--
return buf.Refresh()
}
// EndLine ends the line with CRLF.
func (buf *Buffer) EndLine() error {
_, err := buf.Out.Write(crlf)
return err
}
// toBytes converts a slice of runes to its equivalent in bytes.
func toBytes(runes []rune) []byte {
var bytes []byte
char := make([]byte, utf8.UTFMax)
for _, r := range runes {
n := utf8.EncodeRune(char, r)
bytes = append(bytes, char[:n]...)
}
return bytes
}
|