-
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.
- Loading branch information
1 parent
f5603a3
commit a83d2f8
Showing
9 changed files
with
135 additions
and
67 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,25 @@ | ||
--- | ||
"@clack/prompts": minor | ||
"@clack/core": minor | ||
--- | ||
|
||
Adds a new `updateSettings()` function to support new global keybindings. | ||
|
||
`updateSettings()` accepts an `aliases` object that maps custom keys to an action (`up | down | left | right | space | enter | cancel`). | ||
|
||
```ts | ||
import { updateSettings } from "@clack/prompts"; | ||
|
||
// Support custom keybindings | ||
updateSettings({ | ||
aliases: { | ||
w: "up", | ||
a: "left", | ||
s: "down", | ||
d: "right", | ||
}, | ||
}); | ||
``` | ||
|
||
> [!WARNING] | ||
> In order to enforce consistent, user-friendly defaults across the ecosystem, `updateSettings` does not support disabling Clack's default keybindings. |
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,14 @@ | ||
--- | ||
"@clack/prompts": minor | ||
"@clack/core": minor | ||
--- | ||
|
||
Updates default keybindings to support Vim motion shortcuts and map the `escape` key to cancel (`ctrl+c`). | ||
|
||
| alias | action | | ||
|------- |-------- | | ||
| `k` | up | | ||
| `l` | right | | ||
| `j` | down | | ||
| `h` | left | | ||
| `esc` | cancel | |
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 was deleted.
Oops, something went wrong.
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,73 @@ | ||
const actions = ['up', 'down', 'left', 'right', 'space', 'enter', 'cancel'] as const; | ||
export type Action = (typeof actions)[number]; | ||
|
||
/** Global settings for Clack programs, stored in memory */ | ||
interface InternalClackSettings { | ||
actions: Set<Action>; | ||
aliases: Map<string, Action>; | ||
} | ||
|
||
export const settings: InternalClackSettings = { | ||
actions: new Set(actions), | ||
aliases: new Map<string, Action>([ | ||
// vim support | ||
['k', 'up'], | ||
['j', 'down'], | ||
['h', 'left'], | ||
['l', 'right'], | ||
['\x03', 'cancel'], | ||
// opinionated defaults! | ||
['escape', 'cancel'], | ||
]), | ||
}; | ||
|
||
export interface ClackSettings { | ||
/** | ||
* Set custom global aliases for the default actions. | ||
* This will not overwrite existing aliases, it will only add new ones! | ||
* | ||
* @param aliases - An object that maps aliases to actions | ||
* @default { k: 'up', j: 'down', h: 'left', l: 'right', '\x03': 'cancel', 'escape': 'cancel' } | ||
*/ | ||
aliases: Record<string, Action>; | ||
} | ||
|
||
export function updateSettings(updates: ClackSettings) { | ||
for (const _key in updates) { | ||
const key = _key as keyof ClackSettings; | ||
if (!Object.hasOwn(updates, key)) continue; | ||
const value = updates[key]; | ||
|
||
switch (key) { | ||
case 'aliases': { | ||
for (const alias in value) { | ||
if (!Object.hasOwn(value, alias)) continue; | ||
if (!settings.aliases.has(alias)) { | ||
settings.aliases.set(alias, value[alias]); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check if a key is an alias for a default action | ||
* @param key - The raw key which might match to an action | ||
* @param action - The action to match | ||
* @returns boolean | ||
*/ | ||
export function isActionKey(key: string | Array<string | undefined>, action: Action) { | ||
if (typeof key === 'string') { | ||
return settings.aliases.get(key) === action; | ||
} | ||
|
||
for (const value of key) { | ||
if (value === undefined) continue; | ||
if (isActionKey(value, action)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
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