Skip to content

Commit

Permalink
refactor(prompt): refactor prompt's (#122)
Browse files Browse the repository at this point in the history
* rename getPrompt to getHeader

* rename screen property to tty

* rename tempalte methods

* restore cursor in generic prompt class

* remove header method, add input method and rename some properties

* fix toggle prompt

* rename mapItem to mapOption

* make some properties and methods private

* refactor event handling
  • Loading branch information
c4spar committed Dec 9, 2020
1 parent 2fda6e0 commit 63351d0
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 258 deletions.
69 changes: 28 additions & 41 deletions prompt/_generic_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ export interface GenericInputPromptSettings<T>
/** Generic input prompt representation. */
export abstract class GenericInput<T, S extends GenericInputPromptSettings<T>>
extends GenericPrompt<T, string, S> {
protected input = "";
protected index = 0;
#promptPosition: number = 0;
protected inputValue = "";
protected inputIndex = 0;

/**
* Inject prompt value. Can be used for unit tests or pre selections.
Expand Down Expand Up @@ -60,35 +59,25 @@ 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 message(): string {
const message: string = super.message() + " " + this.settings.pointer + " ";
this.cursor.x = stripColor(message).length + this.inputIndex + 1;
return message + this.input();
}

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

/**
* Set prompt message.
* @param message Prompt message.
*/
protected async render(message: string): Promise<void> {
await super.render(message);
this.screen.cursorTo(this.#promptPosition + this.index + 1);
protected input(): string {
return underline(this.inputValue);
}

/**
* Handle user input event.
* @param event Key event.
*/
protected handleEvent(event: KeyEvent): boolean {
protected async handleEvent(event: KeyEvent): Promise<void> {
switch (true) {
case event.name === "c":
if (event.ctrl) {
this.screen.cursorShow();
this.tty.cursorShow();
return Deno.exit(0);
}
if (event.sequence) {
Expand Down Expand Up @@ -119,62 +108,60 @@ export abstract class GenericInput<T, S extends GenericInputPromptSettings<T>>
break;

case this.isKey(this.settings.keys, "submit", event):
return true;
await this.submit();
break;

default:
if (event.sequence && !event.meta && !event.ctrl) {
this.addChar(event.sequence);
}
break;
}

return false;
}

/**
* Add character to current input.
* @param char Char to add.
*/
protected addChar(char: string): void {
this.input = this.input.slice(0, this.index) + char +
this.input.slice(this.index);
this.index++;
this.inputValue = this.inputValue.slice(0, this.inputIndex) + char +
this.inputValue.slice(this.inputIndex);
this.inputIndex++;
}

/** Move prompt cursor left. */
protected moveCursorLeft(): void {
if (this.index > 0) {
this.index--;
if (this.inputIndex > 0) {
this.inputIndex--;
}
}

/** Move prompt cursor right. */
protected moveCursorRight(): void {
if (this.index < this.input.length) {
this.index++;
if (this.inputIndex < this.inputValue.length) {
this.inputIndex++;
}
}

/** Delete char left. */
protected deleteChar(): void {
if (this.index > 0) {
this.index--;
this.screen.cursorBackward(1);
this.input = this.input.slice(0, this.index) +
this.input.slice(this.index + 1);
if (this.inputIndex > 0) {
this.inputIndex--;
this.tty.cursorBackward(1);
this.inputValue = this.inputValue.slice(0, this.inputIndex) +
this.inputValue.slice(this.inputIndex + 1);
}
}

/** Delete char right. */
protected deleteCharRight(): void {
if (this.index < this.input.length) {
this.input = this.input.slice(0, this.index) +
this.input.slice(this.index + 1);
if (this.inputIndex < this.inputValue.length) {
this.inputValue = this.inputValue.slice(0, this.inputIndex) +
this.inputValue.slice(this.inputIndex + 1);
}
}

/** Get input input. */
protected getValue(): string {
return this.input;
return this.inputValue;
}
}
16 changes: 9 additions & 7 deletions prompt/_generic_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>

/**
* Set list option defaults.
* @param item List option.
* @param option List option.
*/
protected static mapItem(item: GenericListOption): GenericListOptionSettings {
protected static mapOption(
option: GenericListOption,
): GenericListOptionSettings {
return {
value: item.value,
name: typeof item.name === "undefined" ? item.value : item.name,
disabled: !!item.disabled,
value: option.value,
name: typeof option.name === "undefined" ? option.value : option.name,
disabled: !!option.disabled,
};
}

/** Render options. */
protected getBody(): string | undefined | Promise<string | undefined> {
protected body(): string | undefined | Promise<string | undefined> {
const body: Array<string> = [];
const height: number = this.getListHeight();
for (let i = this.index; i < this.index + height; i++) {
Expand All @@ -90,7 +92,7 @@ export abstract class GenericList<T, V, S extends GenericListSettings<T, V>>

/** Read user input. */
protected read(): Promise<boolean> {
this.screen.cursorHide();
this.tty.cursorHide();
return super.read();
}

Expand Down

0 comments on commit 63351d0

Please sign in to comment.