Skip to content

Commit

Permalink
refactor(prompt): refactor prompt's part 2 (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Nov 29, 2020
1 parent b5ecced commit 2fda6e0
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 285 deletions.
25 changes: 15 additions & 10 deletions prompt/_generic_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export abstract class GenericInput<T, S extends GenericInputPromptSettings<T>>
extends GenericPrompt<T, string, S> {
protected input = "";
protected index = 0;
#promptPosition: number = 0;

/**
* Inject prompt value. Can be used for unit tests or pre selections.
Expand Down Expand Up @@ -59,20 +60,24 @@ export abstract class GenericInput<T, S extends GenericInputPromptSettings<T>>
});
}

protected getMessage(): string {
const message: string = super.getMessage() +
" " + this.settings.pointer + " ";
this.#promptPosition = stripColor(message).length;
return message;
}

protected getPrompt(): string {
return this.getMessage() + underline(this.input);
}

/**
* Set prompt message.
* @param message Prompt message.
*/
protected render(message: string) {
message += " " + this.settings.pointer + " ";

const length = stripColor(message).length;

message += underline(this.input);

this.write(message);

this.screen.cursorTo(length + this.index + 1);
protected async render(message: string): Promise<void> {
await super.render(message);
this.screen.cursorTo(this.#promptPosition + this.index + 1);
}

/**
Expand Down
59 changes: 25 additions & 34 deletions prompt/_generic_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,30 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
};
}

/**
* Set prompt message.
* @param message Prompt message.
*/
protected render(message: string): void {
this.writeLine(message);
this.writeListItems();
/** Render options. */
protected getBody(): string | undefined | Promise<string | undefined> {
const body: Array<string> = [];
const height: number = this.getListHeight();
for (let i = this.index; i < this.index + height; i++) {
body.push(
this.getListItem(
this.settings.options[i],
this.selected === i,
),
);
}
return body.join("\n");
}

/** Clear prompt output. */
protected clear(): void {
// clear list
this.screen.eraseLines(this.height() + 2);
// clear message and reset cursor
super.clear();
}
/**
* Render option.
* @param item Option.
* @param isSelected Set to true if option is selected.
*/
protected abstract getListItem(
item: GenericListOptionSettings,
isSelected?: boolean,
): string;

/** Read user input. */
protected read(): Promise<boolean> {
Expand All @@ -98,7 +106,7 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
}
} else {
this.selected = this.settings.options.length - 1;
this.index = this.settings.options.length - this.height();
this.index = this.settings.options.length - this.getListHeight();
if (this.settings.options[this.selected].disabled) {
this.selectPrevious();
}
Expand All @@ -109,7 +117,7 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
protected selectNext(): void {
if (this.selected < this.settings.options.length - 1) {
this.selected++;
if (this.selected >= this.index + this.height()) {
if (this.selected >= this.index + this.getListHeight()) {
this.index++;
}
if (this.settings.options[this.selected].disabled) {
Expand All @@ -123,25 +131,8 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>
}
}

/** Render options. */
protected writeListItems(): void {
for (let i = this.index; i < this.index + this.height(); i++) {
this.writeListItem(this.settings.options[i], this.selected === i);
}
}

/**
* Render option.
* @param item Option.
* @param isSelected Set to true if option is selected.
*/
protected abstract writeListItem(
item: GenericListOptionSettings,
isSelected?: boolean,
): void;

/** Get options row height. */
protected height(): number {
protected getListHeight(): number {
return Math.min(
this.settings.options.length,
this.settings.maxRows || this.settings.options.length,
Expand Down

0 comments on commit 2fda6e0

Please sign in to comment.