Skip to content

Commit 871df91

Browse files
committed
feat(toggle-prompt): default for toggle prompt
1 parent 1027e36 commit 871df91

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
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.1",
3+
"version": "2.0.2",
44
"description": "Advanced logging messages, interactive prompts, loading animations and more in TypeScript",
55
"main": "./lib/index.js",
66
"exports": {

src/prompt/models/config/toggle.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export type AwesomePromptToggleConfig = {
99
* A list of options to display in the toggle.
1010
*/
1111
options: string[];
12+
/**
13+
* A list of default selected options.
14+
*/
15+
default: string[];
1216
};
1317

1418
export type AwesomePromptToggleControl = AwesomePromptBaseControl<string[]>;

src/prompt/models/toggle-prompt.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chalk from 'chalk';
1+
import chalk from 'chalk';
22

33
import { AwesomeLogger } from '../../awesome-logger.js';
44
import { AwesomePromptToggleConfig, AwesomePromptToggleControl } from './config/toggle.js';
@@ -7,7 +7,10 @@ import { KEY_ARROW_DOWN, KEY_ARROW_UP } from '../../utils/ansi-utils.js';
77
import { TerminalSize } from '../../utils/terminal-size.js';
88
import { AwesomePromptBase } from '../prompt-base.js';
99

10-
export class AwesomeTogglePromt extends AwesomePromptBase<string[]> implements AwesomePromptToggleControl {
10+
export class AwesomeTogglePromt
11+
extends AwesomePromptBase<string[]>
12+
implements AwesomePromptToggleControl
13+
{
1114
private _currentHighlightedRow: number;
1215
private readonly _text?: string;
1316
private readonly _options: string[];
@@ -24,10 +27,15 @@ export class AwesomeTogglePromt extends AwesomePromptBase<string[]> implements A
2427
this._currentHighlightedRow = 0;
2528
this._options = config.options ?? [];
2629
this._lines = [...this._options];
27-
this._toggles = this._options.map(() => false);
30+
this._toggles = this._options.map(o => config.default?.includes(o) ?? false);
2831
}
2932

30-
private adjustLine(lineText: string, index: number, highlighted: boolean, selected: boolean = false) {
33+
private adjustLine(
34+
lineText: string,
35+
index: number,
36+
highlighted: boolean,
37+
selected: boolean = false
38+
) {
3139
const newText = `${selected ? chalk.green('[X]') : chalk.gray('[ ]')} ${(highlighted ? chalk.blue : chalk.white)(lineText)}`;
3240
this._lines[index] = newText;
3341
}
@@ -58,7 +66,10 @@ export class AwesomeTogglePromt extends AwesomePromptBase<string[]> implements A
5866
if (this._currentHighlightedRow < this._lines.length - 1) {
5967
const prevHighlightedLine = this._currentHighlightedRow;
6068
this._currentHighlightedRow++;
61-
if (this._currentHighlightedRow - this.scrollAmount > TerminalSize.terminalHeight - 2 - this.fixedLineCount) {
69+
if (
70+
this._currentHighlightedRow - this.scrollAmount >
71+
TerminalSize.terminalHeight - 2 - this.fixedLineCount
72+
) {
6273
this.scrollAmount++;
6374
}
6475
this.renderLines(prevHighlightedLine, this._currentHighlightedRow);
@@ -104,7 +115,9 @@ export class AwesomeTogglePromt extends AwesomePromptBase<string[]> implements A
104115
result.push(option);
105116
}
106117
}
107-
const resultLog = chalk.gray(` - Selected option${result.length > 1 ? 's' : ''}: ${chalk.green(result.join(', '))}`);
118+
const resultLog = chalk.gray(
119+
` - Selected option${result.length > 1 ? 's' : ''}: ${chalk.green(result.join(', '))}`
120+
);
108121
this.setLogger(AwesomeLogger.create('text', { text: resultLog }));
109122
}
110123

0 commit comments

Comments
 (0)