@@ -25,6 +25,7 @@ class TextPrompt extends Prompt {
25
25
this . value = `` ;
26
26
this . errorMsg = opts . error || `Please Enter A Valid Value` ;
27
27
this . cursor = Number ( ! ! this . initial ) ;
28
+ this . cursorOffset = 0 ;
28
29
this . clear = clear ( `` , this . out . columns ) ;
29
30
this . render ( ) ;
30
31
}
@@ -48,6 +49,7 @@ class TextPrompt extends Prompt {
48
49
reset ( ) {
49
50
this . value = `` ;
50
51
this . cursor = Number ( ! ! this . initial ) ;
52
+ this . cursorOffset = 0 ;
51
53
this . fire ( ) ;
52
54
this . render ( ) ;
53
55
}
@@ -78,6 +80,8 @@ class TextPrompt extends Prompt {
78
80
79
81
async submit ( ) {
80
82
this . value = this . value || this . initial ;
83
+ this . cursorOffset = 0 ;
84
+ this . cursor = this . rendered . length ;
81
85
await this . validate ( ) ;
82
86
if ( this . error ) {
83
87
this . red = true ;
@@ -104,6 +108,7 @@ class TextPrompt extends Prompt {
104
108
moveCursor ( n ) {
105
109
if ( this . placeholder ) return ;
106
110
this . cursor = this . cursor + n ;
111
+ this . cursorOffset += n ;
107
112
}
108
113
109
114
_ ( c , key ) {
@@ -116,12 +121,17 @@ class TextPrompt extends Prompt {
116
121
}
117
122
118
123
delete ( ) {
119
- if ( this . cursor === 0 ) return this . bell ( ) ;
124
+ if ( this . isCursorAtStart ( ) ) return this . bell ( ) ;
120
125
let s1 = this . value . slice ( 0 , this . cursor - 1 ) ;
121
126
let s2 = this . value . slice ( this . cursor ) ;
122
127
this . value = `${ s1 } ${ s2 } ` ;
123
128
this . red = false ;
124
- this . moveCursor ( - 1 ) ;
129
+ if ( this . isCursorAtStart ( ) ) {
130
+ this . cursorOffset = 0
131
+ } else {
132
+ this . cursorOffset ++ ;
133
+ this . moveCursor ( - 1 ) ;
134
+ }
125
135
this . render ( ) ;
126
136
}
127
137
@@ -131,6 +141,11 @@ class TextPrompt extends Prompt {
131
141
let s2 = this . value . slice ( this . cursor + 1 ) ;
132
142
this . value = `${ s1 } ${ s2 } ` ;
133
143
this . red = false ;
144
+ if ( this . isCursorAtEnd ( ) ) {
145
+ this . cursorOffset = 0 ;
146
+ } else {
147
+ this . cursorOffset ++ ;
148
+ }
134
149
this . render ( ) ;
135
150
}
136
151
@@ -156,6 +171,14 @@ class TextPrompt extends Prompt {
156
171
this . render ( ) ;
157
172
}
158
173
174
+ isCursorAtStart ( ) {
175
+ return this . cursor === 0 || ( this . placeholder && this . cursor === 1 ) ;
176
+ }
177
+
178
+ isCursorAtEnd ( ) {
179
+ return this . cursor === this . rendered . length || ( this . placeholder && this . cursor === this . rendered . length + 1 )
180
+ }
181
+
159
182
render ( ) {
160
183
if ( this . closed ) return ;
161
184
if ( ! this . firstRender ) {
@@ -178,8 +201,8 @@ class TextPrompt extends Prompt {
178
201
. reduce ( ( a , l , i ) => a + `\n${ i ? ' ' : figures . pointerSmall } ${ color . red ( ) . italic ( l ) } ` , `` ) ;
179
202
}
180
203
181
- this . out . write ( erase . line + cursor . to ( 0 ) + this . outputText + cursor . save + this . outputError + cursor . restore ) ;
204
+ this . out . write ( erase . line + cursor . to ( 0 ) + this . outputText + cursor . save + this . outputError + cursor . restore + cursor . move ( this . cursorOffset , 0 ) ) ;
182
205
}
183
206
}
184
207
185
- module . exports = TextPrompt ;
208
+ module . exports = TextPrompt ;
0 commit comments