Skip to content

Commit

Permalink
Merge pull request #10 from Xander1233/develop
Browse files Browse the repository at this point in the history
Add prompt builder
  • Loading branch information
Xander1233 authored Apr 18, 2023
2 parents 0d4cfe0 + c9246f9 commit dffdb9c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xandrrrr/prompt-kit",
"version": "1.5.0",
"version": "1.6.0",
"description": "Build complex terminal interfaces and menues",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
7 changes: 2 additions & 5 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ export class Base {

constructor() {
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
}

public readLine(): Promise<string> {
protected readLine(message = ""): Promise<string> {
return new Promise((resolve) => {
this.rl.question('', (answer) => {
this.rl.question(message, (answer) => {
resolve(answer);
});
});
Expand Down
91 changes: 91 additions & 0 deletions src/Builder/PromptBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Base } from "../Base";

export class PromptBuilder extends Base {

private _message = "";

private _input_validation: (input: string) => boolean = () => true;

private _input_transform: (input: string) => string = (input) => input;

private _input_default?: string;

/**
* Sets the message that will be displayed to the user
* @param message The message to display to the user
* @returns The current instance of the builder
*/
public setMessage(message: string): PromptBuilder {
this._message = message;
return this;
}

/**
* Sets the validation function that will be used to validate the user's input
* @param validation The validation function to use
* @returns The current instance of the builder
*/
public setValidation(validation: (input: string) => boolean): PromptBuilder {
this._input_validation = validation;
return this;
}

/**
* Sets the transform function
* @param transform The transform function to use
* @returns The current instance of the builder
*/
public setTransform(transform: (input: string) => string): PromptBuilder {
this._input_transform = transform;
return this;
}

/**
* Sets the default value. If none is provided, the user will be prompted until they provide a valid input
* @param default_value The default value to use
* @returns The current instance of the builder
*/
public setDefault(default_value: string): PromptBuilder {
this._input_default = default_value;
return this;
}

public prompt(): Promise<string> {
return new Promise((resolve): void => {

const createUI = (error?: string): void => {
if (error) {
console.log(error);
}
console.log(this._message);
this.readLine(`${this._input_default ? `[Default: ${this._input_default}]` : ""} > `).then(handleInput);
}

const validateInput = (input: string): void => {
if (this.validateUserInput(input)) {
resolve(this._input_transform(input));
} else {
createUI("Your input was invalid.");
}
}

const handleInput = (input: string): void => {
if (input.length === 0) {
if (this._input_default) {
resolve(this._input_default);
} else {
createUI("You must provide a value.");
}
} else {
validateInput(input);
}
}

createUI();
});
}

public validateUserInput(input: string): boolean {
return this._input_validation(input);
}
}
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { SelectionBuilder } from "./Builder/SelectionBuilder";
import { PickerBuilder } from "./Builder/PickerBuilder";
import { MessageBuilder } from "./Builder/MessageBuilder";
import { PromptBuilder } from "./Builder/PromptBuilder";

import { Terminal } from "./Terminal";

import { Foreground, Background, Cursor, Text, getColor as getColorFunction } from "./ANSICodes";

if (!process.stdout.isTTY && process.env.PROMPTKIT_DISABLE_TTY_WARNING !== "true") {
Terminal.shared.print("[WARN] This terminal does not support TTY. Some features may not work as expected. To disable this warning, set the environment variable 'PROMPTKIT_DISABLE_TTY_WARNING' to 'true'.");
} else {
process.stdin.setRawMode(true);
}

export { SelectionBuilder, MessageBuilder, PickerBuilder };
export { SelectionBuilder, MessageBuilder, PickerBuilder, PromptBuilder };

export { Terminal };

Expand All @@ -30,6 +33,10 @@ export default class PromptKit {
return MessageBuilder;
}

public static get PromptBuilder(): typeof PromptBuilder {
return PromptBuilder;
}

public static get Terminal(): typeof Terminal {
return Terminal;
}
Expand Down

0 comments on commit dffdb9c

Please sign in to comment.