Skip to content
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

Improved the visual appeal of SelectionPrompt and MultiSelectionPrompt (text wrapping) #1578

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Spectre.Console/Prompts/MultiSelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
}

var grid = new Grid();
grid.AddColumn(new GridColumn().Padding(0, 0, 1, 0).NoWrap());
grid.AddColumns(new GridColumn().Padding(0, 0, 1, 0).NoWrap(), new GridColumn().Padding(0, 0, 1, 0));

if (Title != null)
{
Expand All @@ -260,7 +260,7 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
? ListPromptConstants.GroupSelectedCheckbox : ListPromptConstants.SelectedCheckbox)
: ListPromptConstants.Checkbox;

grid.AddRow(new Markup(indent + prompt + " " + checkbox + " " + text, style));
grid.AddRow(new Markup(indent + prompt + " " + checkbox, style), new Markup(text, style));
}

list.Add(grid);
Expand Down
38 changes: 31 additions & 7 deletions src/Spectre.Console/Prompts/SelectionPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ int IListPromptStrategy<T>.CalculatePageSize(IAnsiConsole console, int totalItem
return requestedPageSize;
}

private static readonly GridColumn[] _columns = [new GridColumn().Padding(0, 0, 1, 0), new GridColumn().Padding(0, 0, 1, 0)];

/// <inheritdoc/>
IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable, int cursorIndex,
IEnumerable<(int Index, ListPromptItem<T> Node)> items, bool skipUnselectableItems, string searchText)
Expand All @@ -171,12 +173,9 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
list.Add(new Markup(Title));
}

var grid = new Grid();
grid.AddColumn(new GridColumn().Padding(0, 0, 1, 0).NoWrap());

if (Title != null)
{
grid.AddEmptyRow();
list.Add(Text.Empty);
}

foreach (var item in items)
Expand All @@ -200,11 +199,36 @@ IRenderable IListPromptStrategy<T>.Render(IAnsiConsole console, bool scrollable,
text = text.Highlight(searchText, searchHighlightStyle);
}

grid.AddRow(new Markup(indent + prompt + " " + text, style));
if (item.Node.IsGroup)
{
list.Add(new Text(indent + prompt + " " + text, style));
}
else
{
IRenderable[] rows = [
new Markup(indent + prompt, style),
new Markup(text, style)
];

var last = list.LastOrDefault();
Grid grid;

// Optimization to not create a new Grid for each option.
if (last is not Grid)
{
grid = new Grid()
.AddColumns(_columns);
list.Add(grid);
}
else
{
grid = (Grid)last;
}

grid.AddRow(rows);
}
}

list.Add(grid);

if (SearchEnabled || scrollable)
{
// Add padding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public void Should_Highlight_Search_Term()
prompt.Show(console);

// Then
console.Output.ShouldContain($"{ESC}[38;5;12m> Item {ESC}[0m{ESC}[1;38;5;12;48;5;11m1{ESC}[0m");
console.Output.ShouldContain($"{ESC}[38;5;12m>{ESC}[0m {ESC}[38;5;12mItem {ESC}[0m{ESC}[1;38;5;12;48;5;11m1{ESC}[0m");
}
}