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

Add Table layout for Multiselect/Select #83

Closed
JoshuaWilkes opened this issue Dec 22, 2023 · 3 comments
Closed

Add Table layout for Multiselect/Select #83

JoshuaWilkes opened this issue Dec 22, 2023 · 3 comments

Comments

@JoshuaWilkes
Copy link

Hey All, I've been looking into this library and the other projects bubbletea etc.

I've often had a need for a select or multiselect input for CLIs that can display table data as the options.
Ideally with a

  • fixed heading
  • scrollable
  • filterable

It is possible to use the existing multiSelect and format the option value with spacing between fields however there is no header option, and it would also need an overridable filter function.

Extending on the work in #81 I have put together an example input that is styled as a table.

image

PR in my fork is here JoshuaWilkes#1

To pull this off I borrowed from this example https://github.com/charmbracelet/bubbletea/blob/master/examples/table/main.go
And I had to drop the generics support so the the Option could be replaced with a Row which has a string slice []string{} for each column value.

Is this something you would consider adding in?

Thanks!

@JoshuaWilkes
Copy link
Author

As an alternative I suppose it would also be reasonable to use the Description as the header.
Or maybe have an optional Header added as well instead of a specific table mode for the Field type.

@JoshuaWilkes
Copy link
Author

Coming back to this,

As a workaround you can format the description and options to look like a table.
Something along the lines of this to render your header and rows

	cols := []Column{{Title: "hello", Width: 20}, {Title: "world", Width: 15}, {Title: "new", Width: 10}, {Title: "column", Width: 30}}
	var s = make([]string, 0, len(cols))
	for _, col := range cols {
		style := lipgloss.NewStyle().Width(col.Width).MaxWidth(col.Width).Inline(true)
		renderedCell := style.Render(runewidth.Truncate(col.Title, col.Width, "…"))
		s = append(s, lipgloss.NewStyle().Bold(true).Padding(0, 1).Render(renderedCell))
	}
	header := lipgloss.JoinHorizontal(lipgloss.Left, s...)
	rows := []string{}
	var cells = make([]string, 0, 4)
	for i, value := range []string{"a", "b", "c", "d"} {
		style := lipgloss.NewStyle().Width(cols[i].Width).MaxWidth(cols[i].Width).Inline(true)
		renderedCell := lipgloss.NewStyle().Bold(true).Padding(0, 1).Render(style.Render(runewidth.Truncate(value, cols[i].Width, "…")))
		cells = append(cells, renderedCell)
	}

	row := lipgloss.JoinHorizontal(lipgloss.Left, cells...)
	rows = append(rows, row, row)
	
	
	
	huh.NewSelect[string]().
				Options(huh.NewOptions(rows...)...).
				Title("Choose your burger with columns").
				Description(header)
image

@maaslalani
Copy link
Contributor

Hey! Thank you so much for creating this issue!

We definitely want to introduce new field types in the future and table may be one of them.

I'm just going to consolidate to clean up some issues and close this in favour of #61 to have all the ideas in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants