-
Notifications
You must be signed in to change notification settings - Fork 86
Fix AskUserQuestion to support multiline #4119
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { wrapTextWithAnsi, type SelectItem } from '@earendil-works/pi-tui'; | ||
| import chalk from '@studio/common/lib/chalk'; | ||
|
|
||
| const DESCRIPTION_INDENT = ' '; // aligns descriptions under the numbered labels ("→ 1. ") | ||
| const MIN_DESCRIPTION_WIDTH = 10; | ||
|
|
||
| /** | ||
| * Multi-line option rows for AskUserQuestion: full label with the wrapped | ||
| * description indented below — no truncation, newlines preserved. | ||
| */ | ||
| export function buildOptionPickerLines( | ||
| items: SelectItem[], | ||
| selectedValue: string | undefined, | ||
| width: number | ||
| ): string[] { | ||
| const descriptionWidth = Math.max( MIN_DESCRIPTION_WIDTH, width - DESCRIPTION_INDENT.length - 2 ); | ||
| const lines: string[] = []; | ||
|
|
||
| for ( const item of items ) { | ||
| const isSelected = item.value === selectedValue; | ||
| item.label.split( '\n' ).forEach( ( labelLine, index ) => { | ||
| const marker = index === 0 && isSelected ? '→ ' : ' '; | ||
| lines.push( isSelected ? chalk.blue( marker + labelLine ) : marker + labelLine ); | ||
| } ); | ||
| if ( item.description ) { | ||
| for ( const descriptionLine of wrapTextWithAnsi( item.description, descriptionWidth ) ) { | ||
| lines.push( chalk.dim( DESCRIPTION_INDENT + descriptionLine ) ); | ||
| } | ||
| } | ||
| } | ||
| return lines; | ||
| } | ||
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,75 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { buildOptionPickerLines } from '../option-picker'; | ||
|
|
||
| // eslint-disable-next-line no-control-regex | ||
| const stripAnsi = ( text: string ) => text.replace( /\u001b\[[0-9;]*m/g, '' ); | ||
|
|
||
| const ITEMS = [ | ||
| { | ||
| value: 'Blocks + products', | ||
| label: '1. Blocks + products (Recommended)', | ||
| description: 'Native, fully editable WordPress blocks. Best launchpad for a redesign.', | ||
| }, | ||
| { | ||
| value: 'Theme replication', | ||
| label: '2. Theme replication', | ||
| description: 'Pixel-accurate replica of the source.', | ||
| }, | ||
| { value: '__other__', label: 'Other (type my own)' }, | ||
| ]; | ||
|
|
||
| describe( 'buildOptionPickerLines', () => { | ||
| it( 'renders each option label followed by its indented description', () => { | ||
| const lines = buildOptionPickerLines( ITEMS, 'Blocks + products', 100 ).map( stripAnsi ); | ||
| expect( lines ).toEqual( [ | ||
| '→ 1. Blocks + products (Recommended)', | ||
| ' Native, fully editable WordPress blocks. Best launchpad for a redesign.', | ||
| ' 2. Theme replication', | ||
| ' Pixel-accurate replica of the source.', | ||
| ' Other (type my own)', | ||
| ] ); | ||
| } ); | ||
|
|
||
| it( 'marks the selected item with an arrow prefix', () => { | ||
| const lines = buildOptionPickerLines( ITEMS, 'Theme replication', 100 ).map( stripAnsi ); | ||
| expect( lines[ 0 ] ).toBe( ' 1. Blocks + products (Recommended)' ); | ||
| expect( lines[ 2 ] ).toBe( '→ 2. Theme replication' ); | ||
| } ); | ||
|
|
||
| it( 'wraps long descriptions across multiple indented lines without truncation', () => { | ||
| const lines = buildOptionPickerLines( ITEMS, undefined, 40 ).map( stripAnsi ); | ||
| const descriptionLines = lines.filter( ( line ) => line.startsWith( ' ' ) ); | ||
| expect( descriptionLines.length ).toBeGreaterThan( 2 ); | ||
| expect( descriptionLines.join( ' ' ).replace( /\s+/g, ' ' ).trim() ).toContain( | ||
| 'Best launchpad for a redesign.' | ||
| ); | ||
| for ( const line of lines ) { | ||
| expect( line.length ).toBeLessThanOrEqual( 40 ); | ||
| } | ||
| } ); | ||
|
|
||
| it( 'preserves explicit newlines in labels and descriptions', () => { | ||
| const lines = buildOptionPickerLines( | ||
| [ | ||
| { | ||
| value: 'a', | ||
| label: '1. First line\nsecond label line', | ||
| description: 'Para one.\nPara two.', | ||
| }, | ||
| ], | ||
| 'a', | ||
| 80 | ||
| ).map( stripAnsi ); | ||
| expect( lines ).toEqual( [ | ||
| '→ 1. First line', | ||
| ' second label line', | ||
| ' Para one.', | ||
| ' Para two.', | ||
| ] ); | ||
| } ); | ||
|
|
||
| it( 'keeps the descriptionless last item on the final line for the Other inline input', () => { | ||
| const lines = buildOptionPickerLines( ITEMS, '__other__', 100 ).map( stripAnsi ); | ||
| expect( lines[ lines.length - 1 ] ).toBe( '→ Other (type my own)' ); | ||
| } ); | ||
| } ); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name of the function is pretty self-explanatory so I would remove this or maybe just shorten to mention the gist. Not a strong preference if you find that keeping this is helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shortened it