Skip to content

Commit a0bca41

Browse files
cpreston321natemoo-re
authored andcommitted
chore: format
1 parent 51e12bc commit a0bca41

File tree

6 files changed

+93
-83
lines changed

6 files changed

+93
-83
lines changed

packages/core/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ export { default as ConfirmPrompt } from './prompts/confirm';
22
export { default as GroupMultiSelectPrompt } from './prompts/group-multiselect';
33
export { default as MultiSelectPrompt } from './prompts/multi-select';
44
export { default as PasswordPrompt } from './prompts/password';
5-
export { default as Prompt, isCancel, type State } from './prompts/prompt';
5+
export { default as Prompt } from './prompts/prompt';
66
export { default as SelectPrompt } from './prompts/select';
77
export { default as SelectKeyPrompt } from './prompts/select-key';
88
export { default as TextPrompt } from './prompts/text';
9-
export { block, setGlobalAliases } from './utils';
9+
export type { ClackState as State } from './types';
10+
export { block, isCancel, setGlobalAliases } from './utils';
11+

packages/core/src/prompts/prompt.ts

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
1-
import type { Key, ReadLine } from 'node:readline';
2-
31
import { stdin, stdout } from 'node:process';
4-
import readline from 'node:readline';
2+
import readline, { type Key, type ReadLine } from 'node:readline';
53
import { Readable, Writable } from 'node:stream';
64
import { WriteStream } from 'node:tty';
75
import { cursor, erase } from 'sisteransi';
86
import 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

3512
export 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-
6322
export 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');

packages/core/src/types.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { KEYS } from './utils';
2+
3+
export type InferSetType<T> = T extends Set<infer U> ? U : never;
4+
5+
/**
6+
* The state of the prompt
7+
*/
8+
export type ClackState = 'initial' | 'active' | 'cancel' | 'submit' | 'error';
9+
10+
/**
11+
* Typed event emitter for clack
12+
*/
13+
export interface ClackEvents {
14+
initial: (value?: any) => void;
15+
active: (value?: any) => void;
16+
cancel: (value?: any) => void;
17+
submit: (value?: any) => void;
18+
error: (value?: any) => void;
19+
cursor: (key?: InferSetType<typeof KEYS>) => void;
20+
key: (key?: string) => void;
21+
value: (value?: string) => void;
22+
confirm: (value?: boolean) => void;
23+
finalize: () => void;
24+
}

packages/core/src/utils/aliases.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
2-
export type InferSetType<T> = T extends Set<infer U> ? U : never;
1+
import type { InferSetType } from '../types';
32

43
const DEFAULT_KEYS = ['up', 'down', 'left', 'right', 'space', 'enter', 'cancel'] as const;
5-
export const keys = new Set(DEFAULT_KEYS);
4+
export const KEYS = new Set(DEFAULT_KEYS);
65

7-
export const aliases = new Map<string, InferSetType<typeof keys>>([
6+
export const ALIASES = new Map<string, InferSetType<typeof KEYS>>([
87
['k', 'up'],
98
['j', 'down'],
109
['h', 'left'],
@@ -19,10 +18,10 @@ export const aliases = new Map<string, InferSetType<typeof keys>>([
1918
* @default
2019
* new Map([['k', 'up'], ['j', 'down'], ['h', 'left'], ['l', 'right'], ['\x03', 'cancel'],])
2120
*/
22-
export function setGlobalAliases(alias: Array<[string, InferSetType<typeof keys>]>) {
21+
export function setGlobalAliases(alias: Array<[string, InferSetType<typeof KEYS>]>) {
2322
for (const [newAlias, key] of alias) {
24-
if (!aliases.has(newAlias)) {
25-
aliases.set(newAlias, key);
23+
if (!ALIASES.has(newAlias)) {
24+
ALIASES.set(newAlias, key);
2625
}
2726
}
2827
}
@@ -33,13 +32,18 @@ export function setGlobalAliases(alias: Array<[string, InferSetType<typeof keys>
3332
* @param type - The type of key to check for
3433
* @returns boolean
3534
*/
36-
export function hasAliasKey(key: string | Array<string | undefined>, type: InferSetType<typeof keys>) {
35+
export function hasAliasKey(
36+
key: string | Array<string | undefined>,
37+
type: InferSetType<typeof KEYS>
38+
) {
3739
if (typeof key === 'string') {
38-
return aliases.has(key) && aliases.get(key) === type;
40+
return ALIASES.has(key) && ALIASES.get(key) === type;
3941
}
4042

41-
return key.map((n) => {
42-
if (n !== undefined && aliases.has(n) && aliases.get(n) === type) return true;
43-
return false;
44-
}).includes(true);
43+
return key
44+
.map((n) => {
45+
if (n !== undefined && ALIASES.has(n) && ALIASES.get(n) === type) return true;
46+
return false;
47+
})
48+
.includes(true);
4549
}

packages/core/src/utils/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import type { Key } from 'node:readline';
2-
31
import { stdin, stdout } from 'node:process';
2+
import type { Key } from 'node:readline';
43
import * as readline from 'node:readline';
4+
import type { Readable } from 'node:stream';
55
import { cursor } from 'sisteransi';
66
import { hasAliasKey } from './aliases';
77

88
const isWindows = globalThis.process.platform.startsWith('win');
99

10+
export * from './aliases';
11+
export * from './string';
12+
13+
export const CANCEL_SYMBOL = Symbol('clack:cancel');
14+
15+
export function isCancel(value: unknown): value is symbol {
16+
return value === CANCEL_SYMBOL;
17+
}
18+
19+
export function setRawMode(input: Readable, value: boolean) {
20+
const i = input as typeof stdin;
21+
22+
if (i.isTTY) i.setRawMode(value);
23+
}
24+
1025
export function block({
1126
input = stdin,
1227
output = stdout,
@@ -54,5 +69,3 @@ export function block({
5469
rl.close();
5570
};
5671
}
57-
58-
export * from './aliases';

packages/core/src/utils/string.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function diffLines(a: string, b: string) {
2+
if (a === b) return;
3+
4+
const aLines = a.split('\n');
5+
const bLines = b.split('\n');
6+
const diff: number[] = [];
7+
8+
for (let i = 0; i < Math.max(aLines.length, bLines.length); i++) {
9+
if (aLines[i] !== bLines[i]) diff.push(i);
10+
}
11+
12+
return diff;
13+
}

0 commit comments

Comments
 (0)