1- import type { Key , ReadLine } from 'node:readline' ;
2-
31import { stdin , stdout } from 'node:process' ;
4- import readline from 'node:readline' ;
2+ import readline , { type Key , type ReadLine } from 'node:readline' ;
53import { Readable , Writable } from 'node:stream' ;
64import { WriteStream } from 'node:tty' ;
75import { cursor , erase } from 'sisteransi' ;
86import wrap from 'wrap-ansi' ;
97
10- import { type InferSetType , aliases , keys , hasAliasKey } from '../utils' ;
11-
12- function diffLines ( a : string , b : string ) {
13- if ( a === b ) return ;
14-
15- const aLines = a . split ( '\n' ) ;
16- const bLines = b . split ( '\n' ) ;
17- const diff : number [ ] = [ ] ;
18-
19- for ( let i = 0 ; i < Math . max ( aLines . length , bLines . length ) ; i ++ ) {
20- if ( aLines [ i ] !== bLines [ i ] ) diff . push ( i ) ;
21- }
22-
23- return diff ;
24- }
8+ import { ALIASES , CANCEL_SYMBOL , diffLines , hasAliasKey , KEYS , setRawMode } from '../utils' ;
259
26- const cancel = Symbol ( 'clack:cancel' ) ;
27- export function isCancel ( value : unknown ) : value is symbol {
28- return value === cancel ;
29- }
30-
31- function setRawMode ( input : Readable , value : boolean ) {
32- if ( ( input as typeof stdin ) . isTTY ) ( input as typeof stdin ) . setRawMode ( value ) ;
33- }
10+ import type { ClackEvents , ClackState , InferSetType } from '../types' ;
3411
3512export interface PromptOptions < Self extends Prompt > {
3613 render ( this : Omit < Self , 'prompt' > ) : string | void ;
@@ -42,24 +19,6 @@ export interface PromptOptions<Self extends Prompt> {
4219 debug ?: boolean ;
4320}
4421
45- export type State = 'initial' | 'active' | 'cancel' | 'submit' | 'error' ;
46-
47- /**
48- * Typed event emitter for clack
49- */
50- interface ClackHooks {
51- 'initial' : ( value ?: any ) => void ;
52- 'active' : ( value ?: any ) => void ;
53- 'cancel' : ( value ?: any ) => void ;
54- 'submit' : ( value ?: any ) => void ;
55- 'error' : ( value ?: any ) => void ;
56- 'cursor' : ( key ?: InferSetType < typeof keys > ) => void ;
57- 'key' : ( key ?: string ) => void ;
58- 'value' : ( value ?: string ) => void ;
59- 'confirm' : ( value ?: boolean ) => void ;
60- 'finalize' : ( ) => void ;
61- }
62-
6322export default class Prompt {
6423 protected input : Readable ;
6524 protected output : Writable ;
@@ -72,20 +31,12 @@ export default class Prompt {
7231 private _subscribers = new Map < string , { cb : ( ...args : any ) => any ; once ?: boolean } [ ] > ( ) ;
7332 protected _cursor = 0 ;
7433
75- public state : State = 'initial' ;
34+ public state : ClackState = 'initial' ;
7635 public error = '' ;
7736 public value : any ;
7837
79- constructor (
80- options : PromptOptions < Prompt > ,
81- trackValue : boolean = true
82- ) {
83- const {
84- input = stdin ,
85- output = stdout ,
86- render,
87- ...opts
88- } = options ;
38+ constructor ( options : PromptOptions < Prompt > , trackValue : boolean = true ) {
39+ const { input = stdin , output = stdout , render, ...opts } = options ;
8940
9041 this . opts = opts ;
9142 this . onKeypress = this . onKeypress . bind ( this ) ;
@@ -109,7 +60,10 @@ export default class Prompt {
10960 * Set a subscriber with opts
11061 * @param event - The event name
11162 */
112- private setSubscriber < T extends keyof ClackHooks > ( event : T , opts : { cb : ClackHooks [ T ] ; once ?: boolean } ) {
63+ private setSubscriber < T extends keyof ClackEvents > (
64+ event : T ,
65+ opts : { cb : ClackEvents [ T ] ; once ?: boolean }
66+ ) {
11367 const params = this . _subscribers . get ( event ) ?? [ ] ;
11468 params . push ( opts ) ;
11569 this . _subscribers . set ( event , params ) ;
@@ -120,7 +74,7 @@ export default class Prompt {
12074 * @param event - The event name
12175 * @param cb - The callback
12276 */
123- public on < T extends keyof ClackHooks > ( event : T , cb : ClackHooks [ T ] ) {
77+ public on < T extends keyof ClackEvents > ( event : T , cb : ClackEvents [ T ] ) {
12478 this . setSubscriber ( event , { cb } ) ;
12579 }
12680
@@ -129,7 +83,7 @@ export default class Prompt {
12983 * @param event - The event name
13084 * @param cb - The callback
13185 */
132- public once < T extends keyof ClackHooks > ( event : T , cb : ClackHooks [ T ] ) {
86+ public once < T extends keyof ClackEvents > ( event : T , cb : ClackEvents [ T ] ) {
13387 this . setSubscriber ( event , { cb, once : true } ) ;
13488 }
13589
@@ -138,7 +92,7 @@ export default class Prompt {
13892 * @param event - The event name
13993 * @param data - The data to pass to the callback
14094 */
141- public emit < T extends keyof ClackHooks > ( event : T , ...data : Parameters < ClackHooks [ T ] > ) {
95+ public emit < T extends keyof ClackEvents > ( event : T , ...data : Parameters < ClackEvents [ T ] > ) {
14296 const cbs = this . _subscribers . get ( event ) ?? [ ] ;
14397 const cleanup : ( ( ) => void ) [ ] = [ ] ;
14498
@@ -197,7 +151,7 @@ export default class Prompt {
197151 this . output . write ( cursor . show ) ;
198152 this . output . off ( 'resize' , this . render ) ;
199153 setRawMode ( this . input , false ) ;
200- resolve ( cancel ) ;
154+ resolve ( CANCEL_SYMBOL ) ;
201155 } ) ;
202156 } ) ;
203157 }
@@ -206,11 +160,11 @@ export default class Prompt {
206160 if ( this . state === 'error' ) {
207161 this . state = 'active' ;
208162 }
209- if ( key ?. name && ! this . _track && aliases . has ( key . name ) ) {
210- this . emit ( 'cursor' , aliases . get ( key . name ) ) ;
163+ if ( key ?. name && ! this . _track && ALIASES . has ( key . name ) ) {
164+ this . emit ( 'cursor' , ALIASES . get ( key . name ) ) ;
211165 }
212- if ( key ?. name && keys . has ( key . name as InferSetType < typeof keys > ) ) {
213- this . emit ( 'cursor' , key . name as InferSetType < typeof keys > ) ;
166+ if ( key ?. name && KEYS . has ( key . name as InferSetType < typeof KEYS > ) ) {
167+ this . emit ( 'cursor' , key . name as InferSetType < typeof KEYS > ) ;
214168 }
215169 if ( char && ( char . toLowerCase ( ) === 'y' || char . toLowerCase ( ) === 'n' ) ) {
216170 this . emit ( 'confirm' , char . toLowerCase ( ) === 'y' ) ;
0 commit comments