-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve types event emitter & global aliases (#147)
- Loading branch information
Showing
11 changed files
with
209 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clack/core': patch | ||
--- | ||
|
||
Improves types for events and interaction states. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { KEYS } from './utils'; | ||
|
||
export type InferSetType<T> = T extends Set<infer U> ? U : never; | ||
|
||
/** | ||
* The state of the prompt | ||
*/ | ||
export type ClackState = 'initial' | 'active' | 'cancel' | 'submit' | 'error'; | ||
|
||
/** | ||
* Typed event emitter for clack | ||
*/ | ||
export interface ClackEvents { | ||
initial: (value?: any) => void; | ||
active: (value?: any) => void; | ||
cancel: (value?: any) => void; | ||
submit: (value?: any) => void; | ||
error: (value?: any) => void; | ||
cursor: (key?: InferSetType<typeof KEYS>) => void; | ||
key: (key?: string) => void; | ||
value: (value?: string) => void; | ||
confirm: (value?: boolean) => void; | ||
finalize: () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { InferSetType } from '../types'; | ||
|
||
const DEFAULT_KEYS = ['up', 'down', 'left', 'right', 'space', 'enter', 'cancel'] as const; | ||
export const KEYS = new Set(DEFAULT_KEYS); | ||
|
||
export const ALIASES = new Map<string, InferSetType<typeof KEYS>>([ | ||
['k', 'up'], | ||
['j', 'down'], | ||
['h', 'left'], | ||
['l', 'right'], | ||
['\x03', 'cancel'], | ||
]); | ||
|
||
/** | ||
* Set custom global aliases for the default keys - This will not overwrite existing aliases just add new ones | ||
* | ||
* @param aliases - A map of aliases to keys | ||
* @default | ||
* new Map([['k', 'up'], ['j', 'down'], ['h', 'left'], ['l', 'right'], ['\x03', 'cancel'],]) | ||
*/ | ||
export function setGlobalAliases(alias: Array<[string, InferSetType<typeof KEYS>]>) { | ||
for (const [newAlias, key] of alias) { | ||
if (!ALIASES.has(newAlias)) { | ||
ALIASES.set(newAlias, key); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check if a key is an alias for a default key | ||
* @param key - The key to check for | ||
* @param type - The type of key to check for | ||
* @returns boolean | ||
*/ | ||
export function hasAliasKey( | ||
key: string | Array<string | undefined>, | ||
type: InferSetType<typeof KEYS> | ||
) { | ||
if (typeof key === 'string') { | ||
return ALIASES.has(key) && ALIASES.get(key) === type; | ||
} | ||
|
||
return key | ||
.map((n) => { | ||
if (n !== undefined && ALIASES.has(n) && ALIASES.get(n) === type) return true; | ||
return false; | ||
}) | ||
.includes(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function diffLines(a: string, b: string) { | ||
if (a === b) return; | ||
|
||
const aLines = a.split('\n'); | ||
const bLines = b.split('\n'); | ||
const diff: number[] = []; | ||
|
||
for (let i = 0; i < Math.max(aLines.length, bLines.length); i++) { | ||
if (aLines[i] !== bLines[i]) diff.push(i); | ||
} | ||
|
||
return diff; | ||
} |
Oops, something went wrong.