Skip to content

Commit 1a7d004

Browse files
committed
repair the _examples
This time I actually ran them all. I also opted to have get return the internal slice rather than make multiple copies of the input on each invocation. It just sort of bothered me to leave it like that.
1 parent 75b34d0 commit 1a7d004

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

cursor.go

+26-27
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ type Cursor struct {
4343
Cursor Pointer
4444
// what the user entered, and what we will echo back to them, after
4545
// insertion of the cursor and prefixing with the prompt
46-
Input []rune
46+
input []rune
4747
// Put the cursor before this slice
4848
Position int
4949
erase bool
5050
}
5151

52-
// NewCursor create a new cursor, with the DefaultCurso, the specified input,
52+
// NewCursor create a new cursor, with the DefaultCursor, the specified input,
5353
// 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 {
5555
if pointer == nil {
5656
pointer = defaultCursor
5757
}
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}
5959
if eraseDefault {
6060
cur.Start()
6161
} else {
@@ -66,14 +66,14 @@ func NewCursor(startingInput string, pointer Pointer, eraseDefault bool) Cursor
6666

6767
func (c *Cursor) String() string {
6868
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)
7171
}
7272

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
7474
// indexed.
7575
func (c *Cursor) End() {
76-
c.Place(len(c.Input))
76+
c.Place(len(c.input))
7777
}
7878

7979
// Start is convenience for c.Place(0) so you don't have to know how I
@@ -84,8 +84,8 @@ func (c *Cursor) Start() {
8484

8585
// ensures we are in bounds.
8686
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)
8989
}
9090

9191
if c.Position < 0 {
@@ -112,44 +112,42 @@ func format(a []rune, c *Cursor) string {
112112
return string(out)
113113
}
114114

115-
// Format renders the Input with the Cursor appropriately positioned.
115+
// Format renders the input with the Cursor appropriately positioned.
116116
func (c *Cursor) Format() string {
117-
r := c.Input
117+
r := c.input
118118
// insert the cursor
119119
return format(r, c)
120120
}
121121

122-
// FormatMask replaces all Input runes with the mask rune.
122+
// FormatMask replaces all input runes with the mask rune.
123123
func (c *Cursor) FormatMask(mask rune) string {
124-
r := make([]rune, len(c.Input))
124+
r := make([]rune, len(c.input))
125125
for i := range r {
126126
r[i] = mask
127127
}
128128
return format(r, c)
129129
}
130130

131-
// Update inserts newInput into the Input []rune in the appropriate place.
131+
// Update inserts newinput into the input []rune in the appropriate place.
132132
// 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)
136136
i := c.Position
137137
a = append(a[:i], append(b, a[i:]...)...)
138-
c.Input = a
138+
c.input = a
139139
c.Move(len(b))
140140
}
141141

142142
// Get returns a copy of the input
143143
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)
147145
}
148146

149147
// Replace replaces the previous input with whatever is specified, and moves the
150148
// cursor to the end position
151149
func (c *Cursor) Replace(input string) {
152-
c.Input = []rune(input)
150+
c.input = []rune(input)
153151
c.End()
154152
}
155153

@@ -171,16 +169,16 @@ func (c *Cursor) Move(shift int) {
171169
// It handles being at the beginning or end of the row, and moves the cursor to
172170
// the appropriate position.
173171
func (c *Cursor) Backspace() {
174-
a := c.Input
172+
a := c.input
175173
i := c.Position
176174
if i == 0 {
177175
// Shrug
178176
return
179177
}
180178
if i == len(a) {
181-
c.Input = a[:i-1]
179+
c.input = a[:i-1]
182180
} else {
183-
c.Input = append(a[:i-1], a[i:]...)
181+
c.input = append(a[:i-1], a[i:]...)
184182
}
185183
// now it's pointing to the i+1th element
186184
c.Move(-1)
@@ -213,7 +211,8 @@ func (c *Cursor) Listen(line []rune, pos int, key rune) ([]rune, int, bool) {
213211
default:
214212
if c.erase {
215213
c.erase = false
216-
c.Update(string(line))
214+
c.Replace("")
215+
c.Update(string(key))
217216
}
218217
}
219218

cursor_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestCursor(t *testing.T) {
2727
})
2828

2929
t.Run("Cursor at end, append additional", func(t *testing.T) {
30-
cursor := Cursor{Input: []rune("a"), Cursor: pipeCursor}
30+
cursor := Cursor{input: []rune("a"), Cursor: pipeCursor}
3131
cursor.End()
3232
f := cursor.Format()
3333
if f != "a|" {
@@ -41,8 +41,8 @@ func TestCursor(t *testing.T) {
4141
})
4242

4343
t.Run("Cursor at at end, backspace", func(t *testing.T) {
44-
cursor := Cursor{Input: []rune("default"), Cursor: pipeCursor}
45-
cursor.Place(len(cursor.Input))
44+
cursor := Cursor{input: []rune("default"), Cursor: pipeCursor}
45+
cursor.Place(len(cursor.input))
4646
cursor.Backspace()
4747

4848
if cursor.Format() != "defaul|" {
@@ -56,7 +56,7 @@ func TestCursor(t *testing.T) {
5656
})
5757

5858
t.Run("Cursor at beginning, append additional", func(t *testing.T) {
59-
cursor := Cursor{Input: []rune("default"), Cursor: pipeCursor}
59+
cursor := Cursor{input: []rune("default"), Cursor: pipeCursor}
6060
t.Log("init", cursor.String())
6161
cursor.Backspace()
6262
if cursor.Format() != "|default" {
@@ -82,7 +82,7 @@ func TestCursor(t *testing.T) {
8282
})
8383

8484
t.Run("Move", func(t *testing.T) {
85-
cursor := Cursor{Input: []rune("default"), Cursor: pipeCursor}
85+
cursor := Cursor{input: []rune("default"), Cursor: pipeCursor}
8686
if cursor.Format() != "|default" {
8787
t.Errorf("expected |default; found %s", cursor.Format())
8888
}

prompt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (p *Prompt) Run() (string, error) {
228228

229229
if p.IsConfirm {
230230
lowerDefault := strings.ToLower(p.Default)
231-
if strings.ToLower(echo) != "y" && (lowerDefault != "y" || (lowerDefault == "y" && echo != "")) {
231+
if strings.ToLower(cur.Get()) != "y" && (lowerDefault != "y" || (lowerDefault == "y" && cur.Get() != "")) {
232232
prompt = render(p.Templates.invalid, p.Label)
233233
err = ErrAbort
234234
}

select.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ func (s *Select) innerRun(starting int, top rune) (int, string, error) {
252252
}
253253

254254
cur.Backspace()
255-
if len(cur.Input) > 0 {
256-
s.list.Search(string(cur.Input))
255+
if len(cur.Get()) > 0 {
256+
s.list.Search(string(cur.Get()))
257257
} else {
258258
s.list.CancelSearch()
259259
}
@@ -264,7 +264,7 @@ func (s *Select) innerRun(starting int, top rune) (int, string, error) {
264264
default:
265265
if canSearch && searchMode {
266266
cur.Update(string(line))
267-
s.list.Search(string(cur.Input))
267+
s.list.Search(string(cur.Get()))
268268
}
269269
}
270270

0 commit comments

Comments
 (0)