-
Notifications
You must be signed in to change notification settings - Fork 9
fix(menu): improve framework list visibility and make None exclusive in AI config selections #1788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ivanvpetrov
wants to merge
8
commits into
master
Choose a base branch
from
ipetrov/fix-cli-menu-behaviour
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63c3e03
fix(menu): platform page size increased to fit WebComponents option
ivanvpetrov 5ad5861
feat(menu): custom multiselect implemented. Active deselection upon s…
ivanvpetrov 05c605d
fix(tests): exclusive checkbox
ivanvpetrov 697f414
fix(menu): addressed copilot comments
ivanvpetrov 6ef91e1
feat(menu): custom exclusive checkbox code coverage
ivanvpetrov cebd5cd
feat(menu): inquirer wrapper code coverage
ivanvpetrov 54eff13
Merge branch 'master' into ipetrov/fix-cli-menu-behaviour
kdinev a2eec1d
Merge branch 'master' into ipetrov/fix-cli-menu-behaviour
ivanvpetrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,238 @@ | ||
| import { | ||
| createPrompt, | ||
| isDownKey, | ||
| isEnterKey, | ||
| isNumberKey, | ||
| isSpaceKey, | ||
| isUpKey, | ||
| makeTheme, | ||
| useKeypress, | ||
| usePrefix, | ||
| useState, | ||
| type Status, | ||
| } from "@inquirer/core"; | ||
| import { Separator } from "@inquirer/prompts"; | ||
| import { styleText } from "node:util"; | ||
| import type { PartialDeep } from "@inquirer/type"; | ||
| import type { Theme } from "@inquirer/core"; | ||
|
|
||
| export type ExclusiveCheckboxChoice<Value> = { | ||
| value: Value; | ||
| name?: string; | ||
| checked?: boolean; | ||
| disabled?: boolean | string; | ||
| }; | ||
|
|
||
| type NormalizedChoice<Value> = { | ||
| value: Value; | ||
| name: string; | ||
| checked: boolean; | ||
| disabled: boolean | string; | ||
| }; | ||
|
|
||
| type ExclusiveCheckboxConfig<Value = string> = { | ||
| message: string; | ||
| choices: ReadonlyArray<Value | ExclusiveCheckboxChoice<Value> | Separator>; | ||
| required?: boolean; | ||
| exclusiveValues?: readonly Value[]; | ||
| pageSize?: number; | ||
| loop?: boolean; | ||
| theme?: PartialDeep<Theme>; | ||
| }; | ||
|
|
||
| function normalizeChoice<Value>(choice: Value | ExclusiveCheckboxChoice<Value> | Separator): NormalizedChoice<Value> | Separator { | ||
| if (Separator.isSeparator(choice)) { | ||
| return choice; | ||
| } | ||
|
|
||
| if (typeof choice === "object" && choice !== null && "value" in choice) { | ||
| const objectChoice = choice as ExclusiveCheckboxChoice<Value>; | ||
| const name = objectChoice.name ?? String(objectChoice.value); | ||
| return { | ||
| value: objectChoice.value, | ||
| name, | ||
| checked: !!objectChoice.checked, | ||
| disabled: objectChoice.disabled ?? false | ||
| }; | ||
| } | ||
|
|
||
| const name = String(choice); | ||
| return { | ||
| value: choice as Value, | ||
| name, | ||
| checked: false, | ||
| disabled: false | ||
| }; | ||
| } | ||
|
|
||
| function isSelectable<Value>(choice: NormalizedChoice<Value> | Separator): choice is NormalizedChoice<Value> { | ||
| return !Separator.isSeparator(choice) && !choice.disabled; | ||
| } | ||
|
|
||
| function isChecked<Value>(choice: NormalizedChoice<Value> | Separator): choice is NormalizedChoice<Value> { | ||
| return !Separator.isSeparator(choice) && choice.checked; | ||
| } | ||
|
|
||
| function toggleExclusiveChoice<Value>( | ||
| items: Array<NormalizedChoice<Value> | Separator>, | ||
| index: number, | ||
| exclusiveValues: readonly Value[], | ||
| ): Array<NormalizedChoice<Value> | Separator> { | ||
| const choice = items[index]; | ||
| if (!isSelectable(choice)) { | ||
| return items; | ||
| } | ||
|
|
||
| const toggledOn = !choice.checked; | ||
| const isExclusive = exclusiveValues.some(value => Object.is(value, choice.value)); | ||
|
|
||
| return items.map((item, itemIndex) => { | ||
| if (Separator.isSeparator(item)) { | ||
| return item; | ||
| } | ||
|
|
||
| if (itemIndex === index) { | ||
| return { ...item, checked: toggledOn }; | ||
| } | ||
|
|
||
| if (toggledOn && isExclusive) { | ||
| return item.disabled ? item : { ...item, checked: false }; | ||
| } | ||
|
|
||
| if (toggledOn && exclusiveValues.some(value => Object.is(value, item.value))) { | ||
| return item.disabled ? item : { ...item, checked: false }; | ||
| } | ||
|
|
||
| return item; | ||
| }); | ||
| } | ||
|
|
||
| function moveActiveIndex<Value>( | ||
| items: Array<NormalizedChoice<Value> | Separator>, | ||
| active: number, | ||
| direction: 1 | -1, | ||
| loop: boolean, | ||
| ): number { | ||
| let next = active; | ||
| for (let i = 0; i < items.length; i++) { | ||
| next += direction; | ||
| if (next < 0) { | ||
| next = loop ? items.length - 1 : 0; | ||
| } | ||
| if (next >= items.length) { | ||
| next = loop ? 0 : items.length - 1; | ||
| } | ||
| if (isSelectable(items[next])) { | ||
| return next; | ||
| } | ||
| } | ||
| return active; | ||
| } | ||
|
|
||
| export function applyExclusiveToggle<Value>( | ||
| items: Array<NormalizedChoice<Value> | Separator>, | ||
| index: number, | ||
| exclusiveValues: readonly Value[], | ||
| ): Array<NormalizedChoice<Value> | Separator> { | ||
| return toggleExclusiveChoice(items, index, exclusiveValues); | ||
| } | ||
|
|
||
| export const exclusiveCheckboxTesting = { | ||
| normalizeChoice, | ||
| moveActiveIndex, | ||
| isSelectable, | ||
| isChecked, | ||
| }; | ||
|
|
||
| export const exclusiveCheckbox = createPrompt<string[], ExclusiveCheckboxConfig<string>>((config, done) => { | ||
| const theme = makeTheme(config.theme); | ||
| const [status, setStatus] = useState<Status>("idle"); | ||
| const [error, setError] = useState<string>(); | ||
| const [items, setItems] = useState<Array<NormalizedChoice<string> | Separator>>( | ||
| config.choices.map(normalizeChoice), | ||
| ); | ||
| const firstSelectable = items.findIndex(isSelectable); | ||
| const [active, setActive] = useState(firstSelectable >= 0 ? firstSelectable : 0); | ||
| const prefix = usePrefix({ status, theme }); | ||
| const exclusiveValues = config.exclusiveValues ?? []; | ||
|
|
||
| useKeypress((key) => { | ||
| if (isUpKey(key)) { | ||
| setActive(moveActiveIndex(items, active, -1, config.loop ?? true)); | ||
| return; | ||
| } | ||
|
|
||
| if (isDownKey(key)) { | ||
| setActive(moveActiveIndex(items, active, 1, config.loop ?? true)); | ||
| return; | ||
| } | ||
|
|
||
| if (isSpaceKey(key)) { | ||
| setItems(toggleExclusiveChoice(items, active, exclusiveValues)); | ||
| setError(undefined); | ||
| return; | ||
| } | ||
|
|
||
| if (isNumberKey(key)) { | ||
| const selectedIndex = Number(key.name) - 1; | ||
| let selectableIndex = -1; | ||
| const position = items.findIndex((item) => { | ||
| if (Separator.isSeparator(item)) { | ||
| return false; | ||
| } | ||
| if (item.disabled) { | ||
| return false; | ||
| } | ||
| selectableIndex++; | ||
| return selectableIndex === selectedIndex; | ||
| }); | ||
| if (position >= 0) { | ||
| setActive(position); | ||
| setItems(toggleExclusiveChoice(items, position, exclusiveValues)); | ||
| setError(undefined); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (isEnterKey(key)) { | ||
| const selected = items.filter(isChecked); | ||
| if (config.required && selected.length === 0) { | ||
| setError("Select at least one option."); | ||
| return; | ||
| } | ||
| setStatus("done"); | ||
| done(selected.map(choice => choice.value)); | ||
| } | ||
| }); | ||
|
|
||
| const renderItem = (item: NormalizedChoice<string> | Separator, index: number, isActive: boolean) => { | ||
| if (Separator.isSeparator(item)) { | ||
| return ` ${item.separator}`; | ||
| } | ||
|
|
||
| const cursor = isActive ? ">" : " "; | ||
| const checkbox = item.checked ? "[x]" : "[ ]"; | ||
| const label = item.checked ? (item.name) : item.name; | ||
| const line = `${cursor} ${checkbox} ${label}`; | ||
| return isActive ? theme.style.highlight(line) : line; | ||
| }; | ||
|
|
||
| if (status === "done") { | ||
| const answer = items.filter(isChecked).map(choice => choice.name).join(", "); | ||
| return `${prefix} ${config.message}\n${styleText("cyan", answer)}`; | ||
| } | ||
|
|
||
| const renderedItems = items | ||
| .map((item, index) => renderItem(item, index, index === active)) | ||
| .join("\n"); | ||
|
|
||
| const helpLine = styleText("dim", "Use ↑↓ to navigate, space to toggle, enter to submit"); | ||
| const lines = [ | ||
| `${prefix} ${config.message}`, | ||
| renderedItems, | ||
| error ? styleText("red", error) : undefined, | ||
| helpLine | ||
| ].filter(Boolean); | ||
|
|
||
| return lines.join("\n"); | ||
| }); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.