@@ -43,19 +43,19 @@ type Cursor struct {
43
43
Cursor Pointer
44
44
// what the user entered, and what we will echo back to them, after
45
45
// insertion of the cursor and prefixing with the prompt
46
- Input []rune
46
+ input []rune
47
47
// Put the cursor before this slice
48
48
Position int
49
49
erase bool
50
50
}
51
51
52
- // NewCursor create a new cursor, with the DefaultCurso , the specified input,
52
+ // NewCursor create a new cursor, with the DefaultCursor , the specified input,
53
53
// and position at the end of the specified starting input.
54
- func NewCursor (startingInput string , pointer Pointer , eraseDefault bool ) Cursor {
54
+ func NewCursor (startinginput string , pointer Pointer , eraseDefault bool ) Cursor {
55
55
if pointer == nil {
56
56
pointer = defaultCursor
57
57
}
58
- cur := Cursor {Cursor : pointer , Position : len (startingInput ), Input : []rune (startingInput ), erase : eraseDefault }
58
+ cur := Cursor {Cursor : pointer , Position : len (startinginput ), input : []rune (startinginput ), erase : eraseDefault }
59
59
if eraseDefault {
60
60
cur .Start ()
61
61
} else {
@@ -66,14 +66,14 @@ func NewCursor(startingInput string, pointer Pointer, eraseDefault bool) Cursor
66
66
67
67
func (c * Cursor ) String () string {
68
68
return fmt .Sprintf (
69
- "Cursor: %s, Input %s, Position %d" ,
70
- string (c .Cursor ([]rune ("" ))), string (c .Input ), c .Position )
69
+ "Cursor: %s, input %s, Position %d" ,
70
+ string (c .Cursor ([]rune ("" ))), string (c .input ), c .Position )
71
71
}
72
72
73
- // End is a convenience for c.Place(len(c.Input )) so you don't have to know how I
73
+ // End is a convenience for c.Place(len(c.input )) so you don't have to know how I
74
74
// indexed.
75
75
func (c * Cursor ) End () {
76
- c .Place (len (c .Input ))
76
+ c .Place (len (c .input ))
77
77
}
78
78
79
79
// Start is convenience for c.Place(0) so you don't have to know how I
@@ -84,8 +84,8 @@ func (c *Cursor) Start() {
84
84
85
85
// ensures we are in bounds.
86
86
func (c * Cursor ) correctPosition () {
87
- if c .Position > len (c .Input ) {
88
- c .Position = len (c .Input )
87
+ if c .Position > len (c .input ) {
88
+ c .Position = len (c .input )
89
89
}
90
90
91
91
if c .Position < 0 {
@@ -112,44 +112,42 @@ func format(a []rune, c *Cursor) string {
112
112
return string (out )
113
113
}
114
114
115
- // Format renders the Input with the Cursor appropriately positioned.
115
+ // Format renders the input with the Cursor appropriately positioned.
116
116
func (c * Cursor ) Format () string {
117
- r := c .Input
117
+ r := c .input
118
118
// insert the cursor
119
119
return format (r , c )
120
120
}
121
121
122
- // FormatMask replaces all Input runes with the mask rune.
122
+ // FormatMask replaces all input runes with the mask rune.
123
123
func (c * Cursor ) FormatMask (mask rune ) string {
124
- r := make ([]rune , len (c .Input ))
124
+ r := make ([]rune , len (c .input ))
125
125
for i := range r {
126
126
r [i ] = mask
127
127
}
128
128
return format (r , c )
129
129
}
130
130
131
- // Update inserts newInput into the Input []rune in the appropriate place.
131
+ // Update inserts newinput into the input []rune in the appropriate place.
132
132
// The cursor is moved to the end of the inputed sequence.
133
- func (c * Cursor ) Update (newInput string ) {
134
- a := c .Input
135
- b := []rune (newInput )
133
+ func (c * Cursor ) Update (newinput string ) {
134
+ a := c .input
135
+ b := []rune (newinput )
136
136
i := c .Position
137
137
a = append (a [:i ], append (b , a [i :]... )... )
138
- c .Input = a
138
+ c .input = a
139
139
c .Move (len (b ))
140
140
}
141
141
142
142
// Get returns a copy of the input
143
143
func (c * Cursor ) Get () string {
144
- o := make ([]rune , len (c .Input ))
145
- copy (o , c .Input )
146
- return string (o )
144
+ return string (c .input )
147
145
}
148
146
149
147
// Replace replaces the previous input with whatever is specified, and moves the
150
148
// cursor to the end position
151
149
func (c * Cursor ) Replace (input string ) {
152
- c .Input = []rune (input )
150
+ c .input = []rune (input )
153
151
c .End ()
154
152
}
155
153
@@ -171,16 +169,16 @@ func (c *Cursor) Move(shift int) {
171
169
// It handles being at the beginning or end of the row, and moves the cursor to
172
170
// the appropriate position.
173
171
func (c * Cursor ) Backspace () {
174
- a := c .Input
172
+ a := c .input
175
173
i := c .Position
176
174
if i == 0 {
177
175
// Shrug
178
176
return
179
177
}
180
178
if i == len (a ) {
181
- c .Input = a [:i - 1 ]
179
+ c .input = a [:i - 1 ]
182
180
} else {
183
- c .Input = append (a [:i - 1 ], a [i :]... )
181
+ c .input = append (a [:i - 1 ], a [i :]... )
184
182
}
185
183
// now it's pointing to the i+1th element
186
184
c .Move (- 1 )
@@ -213,7 +211,8 @@ func (c *Cursor) Listen(line []rune, pos int, key rune) ([]rune, int, bool) {
213
211
default :
214
212
if c .erase {
215
213
c .erase = false
216
- c .Update (string (line ))
214
+ c .Replace ("" )
215
+ c .Update (string (key ))
217
216
}
218
217
}
219
218
0 commit comments