1- import chalk from 'chalk' ;
1+ import chalk from 'chalk' ;
22
33import { AwesomeLogger } from '../../awesome-logger.js' ;
44import { AwesomePromptTextConfig , AwesomePromptTextControl } from './config/text.js' ;
55import { AwesomeLoggerTextControl } from '../../logger/models/config/text.js' ;
66import { CONTROL_PREFIX , KEY_ARROW_LEFT , KEY_ARROW_RIGHT } from '../../utils/ansi-utils.js' ;
77import { AwesomePromptBase } from '../prompt-base.js' ;
88
9- export class AwesomeTextPromt extends AwesomePromptBase < string > implements AwesomePromptTextControl {
9+ export class AwesomeTextPromt
10+ extends AwesomePromptBase < string >
11+ implements AwesomePromptTextControl
12+ {
1013 private readonly _questionLogger : AwesomeLoggerTextControl ;
1114 private readonly _hintLogger : AwesomeLoggerTextControl ;
1215 private readonly _answerLogger : AwesomeLoggerTextControl ;
1316 private readonly _cfg : AwesomePromptTextConfig ;
1417 private readonly _hints : string [ ] ;
1518 private _currentAnswer : string ;
1619 private _cursorPos : number ;
20+ private _isUnchangedDefault : boolean = false ;
1721
1822 constructor ( config : Partial < AwesomePromptTextConfig > ) {
1923 const questionLogger = AwesomeLogger . create ( 'text' ) ;
2024 const answerLogger = AwesomeLogger . create ( 'text' ) ;
2125 const hintLogger = AwesomeLogger . create ( 'text' ) ;
22- const multiLogger = AwesomeLogger . create ( 'multi' , { children : [ questionLogger , hintLogger , answerLogger ] } ) ;
26+ const multiLogger = AwesomeLogger . create ( 'multi' , {
27+ children : [ questionLogger , hintLogger , answerLogger ] ,
28+ } ) ;
2329 super ( multiLogger ) ;
2430 this . _cfg = {
2531 allowOnlyHints : config . allowOnlyHints ?? false ,
@@ -28,9 +34,10 @@ export class AwesomeTextPromt extends AwesomePromptBase<string> implements Aweso
2834 default : config . default ?? '' ,
2935 fuzzyAutoComplete : config . fuzzyAutoComplete ?? false ,
3036 hints : getHintsArray ( ) ,
31- validators : config . validators ?? [ ]
37+ validators : config . validators ?? [ ] ,
3238 } ;
3339 this . _hints = this . _cfg . hints as string [ ] ;
40+ this . _isUnchangedDefault = ! ! this . _cfg . default ;
3441
3542 this . _currentAnswer = '' ;
3643 this . _questionLogger = questionLogger ;
@@ -67,10 +74,15 @@ export class AwesomeTextPromt extends AwesomePromptBase<string> implements Aweso
6774 let autoCompleteMatch : string | null = null ;
6875 let cursorRendered = false ;
6976
70- if ( this . _cfg . fuzzyAutoComplete && this . _hints . length > 0 ) {
77+ if ( this . _isUnchangedDefault ) {
78+ return chalk . gray ( this . _cfg . default ) ;
79+ } else if ( this . _cfg . fuzzyAutoComplete && this . _hints . length > 0 ) {
7180 autoCompleteMatch = this . getFuzzyAutocompleteMatch ( autoCompleteMatch ) ;
7281 } else if ( this . _hints . length > 0 ) {
73- ( { autoCompleteMatch, cursorRendered } = this . getAutocompleteMatch ( autoCompleteMatch , cursorRendered ) ) ;
82+ ( { autoCompleteMatch, cursorRendered } = this . getAutocompleteMatch (
83+ autoCompleteMatch ,
84+ cursorRendered
85+ ) ) ;
7486 }
7587
7688 let answerText : string ;
@@ -125,7 +137,7 @@ export class AwesomeTextPromt extends AwesomePromptBase<string> implements Aweso
125137
126138 public init ( ) {
127139 this . _questionLogger . setText ( this . _cfg . text ) ;
128- if ( this . _hints . length > 0 ) {
140+ if ( this . _hints . length > 0 || this . _isUnchangedDefault ) {
129141 this . _answerLogger . setText ( this . getAnswerText ( ) ) ;
130142 } else {
131143 this . _answerLogger . setText ( chalk . gray ( 'type your answer here...' ) ) ;
@@ -175,24 +187,34 @@ export class AwesomeTextPromt extends AwesomePromptBase<string> implements Aweso
175187 }
176188
177189 private gotRightKey ( ) {
190+ const keptDefault = this . handleDefaultOnInput ( true ) ;
191+ if ( keptDefault ) {
192+ this . _cursorPos = this . _currentAnswer . length ;
193+ return ;
194+ }
178195 if ( this . _cursorPos < this . _currentAnswer . length ) {
179196 this . _cursorPos ++ ;
180197 }
181198 }
182199
183200 private gotLeftKey ( ) {
201+ this . handleDefaultOnInput ( true ) ;
184202 if ( this . _cursorPos > 0 ) {
185203 this . _cursorPos -- ;
186204 }
187205 }
188206
189207 private gotPrintableCharacterKey ( key : string ) {
208+ this . handleDefaultOnInput ( ) ;
190209 this . _currentAnswer =
191- this . _currentAnswer . substring ( 0 , this . _cursorPos ) + key + this . _currentAnswer . substring ( this . _cursorPos ) ;
210+ this . _currentAnswer . substring ( 0 , this . _cursorPos ) +
211+ key +
212+ this . _currentAnswer . substring ( this . _cursorPos ) ;
192213 this . _cursorPos += key . length ;
193214 }
194215
195216 private gotTabKey ( ) {
217+ this . handleDefaultOnInput ( true ) ;
196218 const fittingAutocomplete = this . findPartialMatch ( this . _currentAnswer , this . _hints ) ;
197219 if ( fittingAutocomplete ) {
198220 this . _currentAnswer = fittingAutocomplete ;
@@ -201,16 +223,19 @@ export class AwesomeTextPromt extends AwesomePromptBase<string> implements Aweso
201223 }
202224
203225 private gotBackspaceKey ( ) {
226+ this . handleDefaultOnInput ( ) ;
204227 if ( this . _currentAnswer . length > 0 ) {
205228 if ( this . _cursorPos > 0 ) {
206229 this . _currentAnswer =
207- this . _currentAnswer . substring ( 0 , this . _cursorPos - 1 ) + this . _currentAnswer . substring ( this . _cursorPos ) ;
230+ this . _currentAnswer . substring ( 0 , this . _cursorPos - 1 ) +
231+ this . _currentAnswer . substring ( this . _cursorPos ) ;
208232 this . _cursorPos -- ;
209233 }
210234 }
211235 }
212236
213237 private gotEnterKey ( ) {
238+ this . handleDefaultOnInput ( true ) ;
214239 if ( this . isValid ( this . _currentAnswer ) ) {
215240 this . inputFinished ( this . _currentAnswer ) ;
216241 } else {
@@ -219,6 +244,17 @@ export class AwesomeTextPromt extends AwesomePromptBase<string> implements Aweso
219244 }
220245 }
221246
247+ private handleDefaultOnInput ( keepDefault = false ) {
248+ if ( this . _isUnchangedDefault ) {
249+ this . _isUnchangedDefault = false ;
250+ if ( keepDefault ) {
251+ this . _currentAnswer = this . _cfg . default ;
252+ return true ;
253+ }
254+ }
255+ return false ;
256+ }
257+
222258 protected prepareResultLogger ( ) : void {
223259 const resultLog = `${ chalk . gray ( ' - Input: ' ) } ${ chalk . green ( this . _currentAnswer ) } ` ;
224260 this . setLogger ( AwesomeLogger . create ( 'text' , { text : resultLog } ) ) ;
0 commit comments