Skip to content

Commit

Permalink
refactor(prompt): remove read-line module and move methods to generic…
Browse files Browse the repository at this point in the history
… prompt class
  • Loading branch information
c4spar committed Aug 5, 2020
1 parent a181cbb commit dd1de10
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
26 changes: 24 additions & 2 deletions packages/prompt/lib/generic-prompt.ts
@@ -1,10 +1,10 @@
import { encode } from 'https://deno.land/std@0.63.0/encoding/utf8.ts';
import { blue, bold, dim, green, red, yellow } from 'https://deno.land/std@0.63.0/fmt/colors.ts';
import { AnsiEscape } from '../../ansi-escape/lib/ansi-escape.ts';
import { KeyCode } from '../../keycode/lib/key-code.ts';
import { KeyEvent } from '../../keycode/lib/key-event.ts';
import format from '../../x/format.ts';
import { Figures } from './figures.ts';
import { readKeySync } from './read-line.ts';

export type ValidateResult = string | boolean | Promise<string | boolean>;

Expand Down Expand Up @@ -134,7 +134,7 @@ export abstract class GenericPrompt<T, V, S extends GenericPromptSettings<T, V>,
return this.validateValue( value );
}

const events: KeyEvent[] = await readKeySync();
const events: KeyEvent[] = await this.readKey();

if ( !events.length ) {
return false;
Expand All @@ -153,6 +153,28 @@ export abstract class GenericPrompt<T, V, S extends GenericPromptSettings<T, V>,
return false;
}

protected async readKey(): Promise<KeyEvent[]> {

const data: Uint8Array = await this.readChar();

return data.length ? KeyCode.parse( data ) : [];
}

protected async readChar(): Promise<Uint8Array> {

const buffer = new Uint8Array( 8 );

Deno.setRaw( Deno.stdin.rid, true );
const nread: number | null = await Deno.stdin.read( buffer );
Deno.setRaw( Deno.stdin.rid, false );

if ( nread === null ) {
return buffer;
}

return buffer.subarray( 0, nread );
}

protected transformValue( value: V ): T | undefined {
return this.settings.transform ? this.settings.transform( value ) : this.transform( value );
}
Expand Down
30 changes: 0 additions & 30 deletions packages/prompt/lib/read-line.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/prompt/mod.ts
@@ -1,5 +1,4 @@
export * from './lib/figures.ts';
export * from './lib/read-line.ts';
export * from './lib/generic-prompt.ts';
export * from './lib/generic-input.ts';
export * from './lib/generic-list.ts';
Expand Down

0 comments on commit dd1de10

Please sign in to comment.