mirror of
https://github.com/charmbracelet/gum
synced 2024-06-03 06:12:19 +02:00
feat(choose): keep order of selected items (#182)
* feat(choose): keep order of selected items * fix: KeepOrder -> Ordered Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
parent
832c4fc917
commit
08ed3e2519
|
@ -30,6 +30,7 @@ type model struct {
|
||||||
index int
|
index int
|
||||||
limit int
|
limit int
|
||||||
numSelected int
|
numSelected int
|
||||||
|
currentOrder int
|
||||||
paginator paginator.Model
|
paginator paginator.Model
|
||||||
aborted bool
|
aborted bool
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ type model struct {
|
||||||
type item struct {
|
type item struct {
|
||||||
text string
|
text string
|
||||||
selected bool
|
selected bool
|
||||||
|
order int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd { return nil }
|
func (m model) Init() tea.Cmd { return nil }
|
||||||
|
@ -96,7 +98,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m.items[i].selected = true
|
m.items[i].selected = true
|
||||||
|
m.items[i].order = m.currentOrder
|
||||||
m.numSelected++
|
m.numSelected++
|
||||||
|
m.currentOrder++
|
||||||
}
|
}
|
||||||
case "A":
|
case "A":
|
||||||
if m.limit <= 1 {
|
if m.limit <= 1 {
|
||||||
|
@ -104,8 +108,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
}
|
}
|
||||||
for i := range m.items {
|
for i := range m.items {
|
||||||
m.items[i].selected = false
|
m.items[i].selected = false
|
||||||
|
m.items[i].order = 0
|
||||||
}
|
}
|
||||||
m.numSelected = 0
|
m.numSelected = 0
|
||||||
|
m.currentOrder = 0
|
||||||
case "ctrl+c", "esc":
|
case "ctrl+c", "esc":
|
||||||
m.aborted = true
|
m.aborted = true
|
||||||
m.quitting = true
|
m.quitting = true
|
||||||
|
@ -120,7 +126,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
m.numSelected--
|
m.numSelected--
|
||||||
} else if m.numSelected < m.limit {
|
} else if m.numSelected < m.limit {
|
||||||
m.items[m.index].selected = true
|
m.items[m.index].selected = true
|
||||||
|
m.items[m.index].order = m.currentOrder
|
||||||
m.numSelected++
|
m.numSelected++
|
||||||
|
m.currentOrder++
|
||||||
}
|
}
|
||||||
case "enter":
|
case "enter":
|
||||||
m.quitting = true
|
m.quitting = true
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alecthomas/kong"
|
"github.com/alecthomas/kong"
|
||||||
|
@ -56,10 +57,12 @@ func (o Options) Run() error {
|
||||||
hasSelectedItems := len(o.Selected) > 0
|
hasSelectedItems := len(o.Selected) > 0
|
||||||
|
|
||||||
startingIndex := 0
|
startingIndex := 0
|
||||||
|
currentOrder := 0
|
||||||
|
|
||||||
var items = make([]item, len(o.Options))
|
items := make([]item, len(o.Options))
|
||||||
|
|
||||||
for i, option := range o.Options {
|
for i, option := range o.Options {
|
||||||
|
var order int
|
||||||
// Check if the option should be selected.
|
// Check if the option should be selected.
|
||||||
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
|
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
|
||||||
// If the option is selected then increment the current selected count.
|
// If the option is selected then increment the current selected count.
|
||||||
|
@ -71,10 +74,12 @@ func (o Options) Run() error {
|
||||||
isSelected = false
|
isSelected = false
|
||||||
} else {
|
} else {
|
||||||
currentSelected++
|
currentSelected++
|
||||||
|
order = currentOrder
|
||||||
|
currentOrder++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
items[i] = item{text: option, selected: isSelected}
|
items[i] = item{text: option, selected: isSelected, order: order}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the pagination model to display the current and total number of
|
// Use the pagination model to display the current and total number of
|
||||||
|
@ -94,6 +99,7 @@ func (o Options) Run() error {
|
||||||
|
|
||||||
tm, err := tea.NewProgram(model{
|
tm, err := tea.NewProgram(model{
|
||||||
index: startingIndex,
|
index: startingIndex,
|
||||||
|
currentOrder: currentOrder,
|
||||||
height: o.Height,
|
height: o.Height,
|
||||||
cursor: o.Cursor,
|
cursor: o.Cursor,
|
||||||
selectedPrefix: o.SelectedPrefix,
|
selectedPrefix: o.SelectedPrefix,
|
||||||
|
@ -117,6 +123,12 @@ func (o Options) Run() error {
|
||||||
return exit.ErrAborted
|
return exit.ErrAborted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if o.Ordered && o.Limit > 1 {
|
||||||
|
sort.Slice(m.items, func(i, j int) bool {
|
||||||
|
return m.items[i].order < m.items[j].order
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var s strings.Builder
|
var s strings.Builder
|
||||||
|
|
||||||
for _, item := range m.items {
|
for _, item := range m.items {
|
||||||
|
|
|
@ -8,6 +8,7 @@ type Options struct {
|
||||||
|
|
||||||
Limit int `help:"Maximum number of options to pick" default:"1" group:"Selection"`
|
Limit int `help:"Maximum number of options to pick" default:"1" group:"Selection"`
|
||||||
NoLimit bool `help:"Pick unlimited number of options (ignores limit)" group:"Selection"`
|
NoLimit bool `help:"Pick unlimited number of options (ignores limit)" group:"Selection"`
|
||||||
|
Ordered bool `help:"Maintain the order of the selected options" env:"GUM_CHOOSE_ORDERED"`
|
||||||
Height int `help:"Height of the list" default:"10" env:"GUM_CHOOSE_HEIGHT"`
|
Height int `help:"Height of the list" default:"10" env:"GUM_CHOOSE_HEIGHT"`
|
||||||
Cursor string `help:"Prefix to show on item that corresponds to the cursor position" default:"> " env:"GUM_CHOOSE_CURSOR"`
|
Cursor string `help:"Prefix to show on item that corresponds to the cursor position" default:"> " env:"GUM_CHOOSE_CURSOR"`
|
||||||
CursorPrefix string `help:"Prefix to show on the cursor item (hidden if limit is 1)" default:"○ " env:"GUM_CHOOSE_CURSOR_PREFIX"`
|
CursorPrefix string `help:"Prefix to show on the cursor item (hidden if limit is 1)" default:"○ " env:"GUM_CHOOSE_CURSOR_PREFIX"`
|
||||||
|
|
Loading…
Reference in a new issue