- Notifications
You must be signed in to change notification settings - Fork929
chore: replace AlecAivazis/survey with charmbracelet/bubbletea#14475
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
28 commits Select commitHold shift + click to select a range
3934cb6
chore: replace survery library with bubbletea
DanielleMaywood164402b
chore: fix nolint comments
DanielleMaywood360fb95
chore: add message and colour to select prompts
DanielleMaywood7cf35d1
chore: make SelectedOptions private
DanielleMaywoodb5092a6
chore: add filtering for multiselect, improve filtering algo
DanielleMaywoode4c010e
chore: allow vertical wrap on select/multiselect
DanielleMaywood2b6cd6e
chore: fix failing jobs
DanielleMaywood35fe461
chore: update charmbracelet/bubbletea to v1.0.0
DanielleMaywood5982185
chore: remove windows testing workaround for survey library
DanielleMaywoodaffbfbe
chore: update bubbletea to v1.1.0
DanielleMaywood33196cd
chore: reimplement testing workaround
DanielleMaywood409cec5
chore: use survey's default page size
DanielleMaywood52ed127
chore: ensure no underflow on page bottom
DanielleMaywood60c83d0
chore: remove multi-select test from cmd/cliui
DanielleMaywood2b621ce
chore: disable HideSearch from cmd/cliui select
DanielleMaywood8a3cd26
chore: use strings.Builder, fix select all logic, apply nitpicks
DanielleMaywood6e59ecb
chore: use strings.Builder
DanielleMaywood579c73b
chore: move m.search.Update out of case
DanielleMaywood9078242
chore: use tea.WithContext(inv.Context()) for tea program
DanielleMaywoodf2ed4fa
chore: return nil as err already handled
DanielleMaywood7784110
chore: use const instead of magic number
DanielleMaywoodf598836
chore: handle signals ourself instead of bubbletea
DanielleMaywood8a7f1bc
chore: update flake.nix
DanielleMaywood77804e0
chore: update flake.nix
DanielleMaywood2904b7c
chore: update flake.nix
DanielleMaywood0b99afe
chore: update flake.nix
DanielleMaywoodb173fb0
chore: update flake.nix
DanielleMaywood966f772
chore: update flake.nix
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
chore: add message and colour to select prompts
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit360fb95329dfd81219b5aa8f2acd37ae3f52df94
There are no files selected for viewing
97 changes: 73 additions & 24 deletionscli/cliui/select.go
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 |
---|---|---|
@@ -10,6 +10,7 @@ import ( | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/pretty" | ||
"github.com/coder/serpent" | ||
) | ||
@@ -80,6 +81,7 @@ func Select(inv *serpent.Invocation, opts SelectOptions) (string, error) { | ||
hideSearch: opts.HideSearch, | ||
options: opts.Options, | ||
height: opts.Size, | ||
message: opts.Message, | ||
} | ||
if initialModel.height == 0 { | ||
@@ -110,6 +112,7 @@ type selectModel struct { | ||
options []string | ||
cursor int | ||
height int | ||
message string | ||
selected string | ||
canceled bool | ||
hideSearch bool | ||
@@ -170,22 +173,33 @@ func (m selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
func (m selectModel) View() string { | ||
var s string | ||
msg := pretty.Sprintf(pretty.Bold(), "? %s", m.message) | ||
if m.selected == "" { | ||
if m.hideSearch { | ||
s += fmt.Sprintf("%s [Use arrows to move]\n", msg) | ||
} else { | ||
s += fmt.Sprintf("%s %s[Use arrows to move, type to filter]\n", msg, m.search.View()) | ||
} | ||
options, start := m.viewableOptions() | ||
for i, option := range options { | ||
// Is this the currently selected option? | ||
style := pretty.Wrap(" ", "") | ||
if m.cursor == start+i { | ||
style = pretty.Style{ | ||
pretty.Wrap("> ", ""), | ||
pretty.FgColor(Green), | ||
} | ||
} | ||
s += pretty.Sprint(style, option) | ||
s += "\n" | ||
} | ||
} else { | ||
selected := pretty.Sprint(DefaultStyles.Keyword, m.selected) | ||
s += fmt.Sprintf("%s %s\n", msg, selected) | ||
} | ||
return s | ||
@@ -238,11 +252,18 @@ func MultiSelect(inv *serpent.Invocation, opts MultiSelectOptions) ([]string, er | ||
options := make([]multiSelectOption, len(opts.Options)) | ||
for i, option := range opts.Options { | ||
options[i].option = option | ||
for _, d := range opts.Defaults { | ||
if option == d { | ||
options[i].chosen = true | ||
} | ||
} | ||
} | ||
initialModel := multiSelectModel{ | ||
search: textinput.New(), | ||
options: options, | ||
message: opts.Message, | ||
} | ||
initialModel.search.Prompt = "" | ||
@@ -278,7 +299,9 @@ type multiSelectModel struct { | ||
search textinput.Model | ||
options []multiSelectOption | ||
cursor int | ||
message string | ||
canceled bool | ||
selected bool | ||
} | ||
func (multiSelectModel) Init() tea.Cmd { | ||
@@ -297,12 +320,13 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
case tea.KeyEnter: | ||
if len(m.options) != 0 { | ||
m.selected = true | ||
return m, tea.Quit | ||
} | ||
case tea.KeySpace: | ||
if len(m.options) != 0 { | ||
m.options[m.cursor].chosen =!m.options[m.cursor].chosen | ||
} | ||
case tea.KeyUp: | ||
@@ -343,21 +367,46 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
} | ||
func (m multiSelectModel) View() string { | ||
var s string | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
msg := pretty.Sprintf(pretty.Bold(), "? %s", m.message) | ||
if !m.selected { | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
s += fmt.Sprintf("%s %s[Use arrows to move, space to select, <right> to all, <left> to none, type to filter]\n", msg, m.search.View()) | ||
for i, option := range m.options { | ||
cursor := " " | ||
if m.cursor == i { | ||
cursor = pretty.Sprint(pretty.FgColor(Green), "> ") | ||
} | ||
chosen := "[ ]" | ||
if option.chosen { | ||
chosen = pretty.Sprint(pretty.FgColor(Green), "[x]") | ||
} | ||
o := option.option | ||
if m.cursor == i { | ||
o = pretty.Sprint(pretty.FgColor(Green), o) | ||
} | ||
s += fmt.Sprintf("%s%s %s\n", cursor, chosen, o) | ||
} | ||
} else { | ||
selected := pretty.Sprint(DefaultStyles.Keyword, strings.Join(m.SelectedOptions(), ", ")) | ||
s += fmt.Sprintf("%s %s\n",msg, selected) | ||
} | ||
return s | ||
} | ||
func (m multiSelectModel) SelectedOptions() []string { | ||
selected := []string{} | ||
for _, o := range m.options { | ||
if o.chosen { | ||
selected = append(selected, o.option) | ||
} | ||
} | ||
return selected | ||
} |
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.