Skip to content

Commit

Permalink
all
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Nov 12, 2024
1 parent b0802a7 commit 451a032
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 61 deletions.
4 changes: 2 additions & 2 deletions ansi/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@windmill-labs/cliffy-ansi",
"version": "1.0.0-rc.5",
"name": "@cliffy/ansi",
"version": "1.0.0-rc.7",
"exports": {
".": "./ansi.ts",
"./ansi-escapes": "./ansi_escapes.ts",
Expand Down
4 changes: 2 additions & 2 deletions command/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@windmill-labs/cliffy-command",
"version": "1.0.0-rc.5",
"name": "@cliffy/command",
"version": "1.0.0-rc.7",
"exports": {
".": "./mod.ts",
"./completions": "./completions/mod.ts",
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@cliffy/internal": "jsr:@windmill-labs/cliffy-internal@1.0.0-rc.5",
"@cliffy/keycode": "jsr:@windmill-labs/cliffy-keycode@1.0.0-rc.5",
"@cliffy/keypress": "jsr:@windmill-labs/cliffy-keypress@1.0.0-rc.5",
"@cliffy/prompt": "jsr:@windmill-labs/cliffy-prompt@1.0.0-rc.5",
"@cliffy/prompt": "jsr:@windmill-labs/cliffy-prompt@1.0.0-rc.6",
"@cliffy/table": "jsr:@windmill-labs/cliffy-table@1.0.0-rc.5",
"@cliffy/testing": "jsr:@windmill-labs/cliffy-testing@1.0.0-rc.5",
"@std/assert": "jsr:@std/assert@1.0.0-rc.2",
Expand Down
10 changes: 7 additions & 3 deletions internal/deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@windmill-labs/cliffy-internal",
"version": "1.0.0-rc.5",
"name": "@cliffy/internal",
"version": "1.0.0-rc.7",
"exports": {
"./runtime/delete-env": "./runtime/delete_env.ts",
"./runtime/exit": "./runtime/exit.ts",
"./runtime/get-args": "./runtime/get_args.ts",
"./runtime/get-columns": "./runtime/get_columns.ts",
Expand All @@ -14,8 +15,11 @@
"./runtime/read": "./runtime/read.ts",
"./runtime/read-dir": "./runtime/read_dir.ts",
"./runtime/read-sync": "./runtime/read_sync.ts",
"./runtime/runtime-name": "./runtime/runtime_name.ts",
"./runtime/set-env": "./runtime/set_env.ts",
"./runtime/set-raw": "./runtime/set_raw.ts",
"./runtime/stat": "./runtime/stat.ts",
"./runtime/write-sync": "./runtime/write_sync.ts"
"./runtime/write-sync": "./runtime/write_sync.ts",
"./testing/test": "./testing/test/mod.ts"
}
}
4 changes: 2 additions & 2 deletions keycode/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@windmill-labs/cliffy-keycode",
"version": "1.0.0-rc.5",
"name": "@cliffy/keycode",
"version": "1.0.0-rc.7",
"exports": "./mod.ts"
}
4 changes: 2 additions & 2 deletions keypress/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@windmill-labs/cliffy-keypress",
"version": "1.0.0-rc.5",
"name": "@cliffy/keypress",
"version": "1.0.0-rc.7",
"exports": "./mod.ts"
}
81 changes: 38 additions & 43 deletions prompt/_generic_prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ export interface StaticGenericPrompt<TValue, TOptions> {

// deno-lint-ignore no-explicit-any
export type InferPromptOptions<TPrompt extends GenericPrompt<any, any>> =
Parameters<
TPrompt["getDefaultSettings"]
>[0];
Parameters<TPrompt["getDefaultSettings"]>[0];

// deno-lint-ignore no-explicit-any
export type InferPromptValue<TPrompt extends GenericPrompt<any, any>> = Awaited<
ReturnType<
TPrompt["prompt"]
>
ReturnType<TPrompt["prompt"]>
>;

/** Generic prompt options. */
Expand Down Expand Up @@ -111,10 +107,7 @@ export interface GenericPromptKeys {
}

/** Generic prompt representation. */
export abstract class GenericPrompt<
TValue,
TRawValue,
> {
export abstract class GenericPrompt<TValue, TRawValue> {
protected static injectedValue: unknown | undefined;
protected abstract readonly settings: GenericPromptSettings<
TValue,
Expand All @@ -141,7 +134,7 @@ export abstract class GenericPrompt<
}

public getDefaultSettings(
options: GenericPromptOptions<TValue, TRawValue>,
options: GenericPromptOptions<TValue, TRawValue>
): GenericPromptSettings<TValue, TRawValue> {
return {
...options,
Expand Down Expand Up @@ -187,7 +180,7 @@ export abstract class GenericPrompt<
await this.render();
this.#lastError = undefined;

if (!await this.read()) {
if (!(await this.read())) {
return this.#execute();
}

Expand All @@ -200,7 +193,7 @@ export abstract class GenericPrompt<

if (successMessage) {
this.settings.writer.writeSync(
this.#encoder.encode(successMessage + "\n"),
this.#encoder.encode(successMessage + "\n")
);
}

Expand All @@ -213,21 +206,17 @@ export abstract class GenericPrompt<
/** Render prompt. */
protected async render(): Promise<void> {
const result: [string, string | undefined, string | undefined] =
await Promise.all([
this.message(),
this.body?.(),
this.footer(),
]);
await Promise.all([this.message(), this.body?.(), this.footer()]);

const content: string = result.filter(Boolean).join("\n");
const lines = content.split("\n");

const columns = getColumns();
const linesCount: number = columns
? lines.reduce((prev, next) => {
const length = stripAnsiCode(next).length;
return prev + (length > columns ? Math.ceil(length / columns) : 1);
}, 0)
const length = stripAnsiCode(next).length;
return prev + (length > columns ? Math.ceil(length / columns) : 1);
}, 0)
: content.split("\n").length;

const y: number = linesCount - this.cursor.y - 1;
Expand Down Expand Up @@ -269,14 +258,18 @@ export abstract class GenericPrompt<
}

protected message(): string {
return `${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) + this.defaults();
return (
`${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) +
this.defaults()
);
}

protected defaults(): string {
let defaultMessage = "";
if (
typeof this.settings.default !== "undefined" && !this.settings.hideDefault
typeof this.settings.default !== "undefined" &&
!this.settings.hideDefault
) {
defaultMessage += dim(` (${this.format(this.settings.default)})`);
}
Expand All @@ -285,10 +278,15 @@ export abstract class GenericPrompt<

/** Get prompt success message. */
protected success(value: TValue): string | undefined {
return `${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) + this.defaults() +
" " + this.settings.pointer +
" " + green(this.format(value));
return (
`${this.settings.indent}${this.settings.prefix}` +
bold(this.settings.message) +
this.defaults() +
" " +
this.settings.pointer +
" " +
green(this.format(value))
);
}

protected body?(): string | undefined | Promise<string | undefined>;
Expand All @@ -306,7 +304,7 @@ export abstract class GenericPrompt<
protected hint(): string | undefined {
return this.settings.hint
? this.settings.indent +
italic(brightBlue(dim(`${Figures.POINTER} `) + this.settings.hint))
italic(brightBlue(dim(`${Figures.POINTER} `) + this.settings.hint))
: undefined;
}

Expand Down Expand Up @@ -363,14 +361,11 @@ export abstract class GenericPrompt<

/** Read user input from stdin. */
#readChar = async (): Promise<Uint8Array> => {
const buffer = new Uint8Array(8);
const buffer = new Uint8Array(256);
const isTty = this.settings.reader.isTerminal();

if (isTty) {
this.settings.reader.setRaw(
true,
{ cbreak: this.settings.cbreak },
);
this.settings.reader.setRaw(true, { cbreak: this.settings.cbreak });
}
const nread: number | null = await this.settings.reader.read(buffer);

Expand Down Expand Up @@ -416,10 +411,9 @@ export abstract class GenericPrompt<
this.#value = undefined;
this.#lastError = undefined;

const validation =
await (this.settings.validate
? this.settings.validate(value)
: this.validate(value));
const validation = await (this.settings.validate
? this.settings.validate(value)
: this.validate(value));

if (validation === false) {
this.#lastError = `Invalid answer.`;
Expand All @@ -439,15 +433,16 @@ export abstract class GenericPrompt<
protected isKey<TKey extends unknown, TName extends keyof TKey>(
keys: TKey | undefined,
name: TName,
event: KeyCode,
event: KeyCode
): boolean {
// deno-lint-ignore no-explicit-any
const keyNames: Array<unknown> | undefined = keys?.[name] as any;
return typeof keyNames !== "undefined" && (
(typeof event.name !== "undefined" &&
return (
typeof keyNames !== "undefined" &&
((typeof event.name !== "undefined" &&
keyNames.indexOf(event.name) !== -1) ||
(typeof event.sequence !== "undefined" &&
keyNames.indexOf(event.sequence) !== -1)
(typeof event.sequence !== "undefined" &&
keyNames.indexOf(event.sequence) !== -1))
);
}
}
4 changes: 2 additions & 2 deletions prompt/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@windmill-labs/cliffy-prompt",
"version": "1.0.0-rc.5",
"name": "@cliffy/prompt",
"version": "1.0.0-rc.7",
"exports": {
".": "./mod.ts",
"./checkbox": "./checkbox.ts",
Expand Down
4 changes: 2 additions & 2 deletions table/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@windmill-labs/cliffy-table",
"version": "1.0.0-rc.5",
"name": "@cliffy/table",
"version": "1.0.0-rc.7",
"exports": "./mod.ts"
}
4 changes: 2 additions & 2 deletions testing/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@windmill-labs/cliffy-testing",
"version": "1.0.0-rc.5",
"name": "@cliffy/testing",
"version": "1.0.0-rc.7",
"exports": "./mod.ts"
}

0 comments on commit 451a032

Please sign in to comment.