Skip to content

Commit 1027e36

Browse files
committed
text prompt default
1 parent 868a206 commit 1027e36

File tree

4 files changed

+103
-28
lines changed

4 files changed

+103
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "awesome-logging",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Advanced logging messages, interactive prompts, loading animations and more in TypeScript",
55
"main": "./lib/index.js",
66
"exports": {

src/prompt/models/text-prompt.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
import chalk from 'chalk';
1+
import chalk from 'chalk';
22

33
import { AwesomeLogger } from '../../awesome-logger.js';
44
import { AwesomePromptTextConfig, AwesomePromptTextControl } from './config/text.js';
55
import { AwesomeLoggerTextControl } from '../../logger/models/config/text.js';
66
import { CONTROL_PREFIX, KEY_ARROW_LEFT, KEY_ARROW_RIGHT } from '../../utils/ansi-utils.js';
77
import { 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 }));

src/test.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,32 @@ AwesomeLogger.log('awd5');
274274
// });
275275
// a.result.then(x => console.log('result = ' + x));
276276

277-
const awd = AwesomeLogger.log('checklist', {
278-
items: [
279-
{ text: 'item1', state: 'pending' },
280-
{ text: 'item2', state: 'pending' },
281-
{ text: 'item3', state: 'pending' },
282-
{ text: 'item4', state: 'pending' },
283-
{ text: 'item5', state: 'pending' },
284-
{ text: 'item6', state: 'pending' },
285-
{ text: 'item7', state: 'pending' },
286-
{ text: 'item8', state: 'pending' },
287-
{ text: 'item9', state: 'pending' },
288-
{ text: 'item10', state: 'pending' },
289-
],
290-
logAllFinalStates: false,
291-
});
277+
// const awd = AwesomeLogger.log('checklist', {
278+
// items: [
279+
// { text: 'item1', state: 'pending' },
280+
// { text: 'item2', state: 'pending' },
281+
// { text: 'item3', state: 'pending' },
282+
// { text: 'item4', state: 'pending' },
283+
// { text: 'item5', state: 'pending' },
284+
// { text: 'item6', state: 'pending' },
285+
// { text: 'item7', state: 'pending' },
286+
// { text: 'item8', state: 'pending' },
287+
// { text: 'item9', state: 'pending' },
288+
// { text: 'item10', state: 'pending' },
289+
// ],
290+
// logAllFinalStates: false,
291+
// });
292+
293+
// awd.changeState(0, 'succeeded');
294+
// awd.changeState(3, 'succeeded');
292295

293-
awd.changeState(0, 'succeeded');
294-
awd.changeState(3, 'succeeded');
296+
// awd.end();
295297

296-
awd.end();
298+
const defaultText = AwesomeLogger.prompt('text', {
299+
text: 'Please enter your phone number:',
300+
default: '+49 ',
301+
});
302+
303+
await defaultText.result.then(r => {
304+
AwesomeLogger.log(`You entered: ${r}`);
305+
});

test/text-prompt.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,36 @@ describe('Text Prompt', () => {
2828
t.sendKey('enter');
2929
});
3030

31+
test('ask for text with default', done => {
32+
const c = AwesomeLogger.prompt('text', { text: 'enter text', default: 'my answer!' });
33+
expect(t.allLines).toStrictEqual(['', 'enter text', 'my answer!']);
34+
c.result.then(r => {
35+
expect(r).toBe('nope');
36+
expect(t.allLines).toStrictEqual(['', ' - Input: nope']);
37+
done();
38+
});
39+
t.sendText('nope');
40+
expect(c.getCurrentAnswer()).toBe('nope');
41+
t.sendKey('enter');
42+
});
43+
44+
test('ask for text with default arrow', done => {
45+
const c = AwesomeLogger.prompt('text', { text: 'enter text', default: 'my answer!' });
46+
expect(t.allLines).toStrictEqual(['', 'enter text', 'my answer!']);
47+
c.result.then(r => {
48+
expect(r).toBe('my cool answer!');
49+
expect(t.allLines).toStrictEqual(['', ' - Input: my cool answer!']);
50+
done();
51+
});
52+
t.sendKey('left');
53+
t.sendKey('right');
54+
t.sendKey('right');
55+
t.sendKey('right');
56+
t.sendText('cool ');
57+
expect(c.getCurrentAnswer()).toBe('my cool answer!');
58+
t.sendKey('enter');
59+
});
60+
3161
test('Text prompt with optional hints', done => {
3262
const c = AwesomeLogger.prompt('text', {
3363
text: 'enter text',

0 commit comments

Comments
 (0)