diff --git a/README.md b/README.md index 3057c30..c2af4f2 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,9 @@ apk add gum # Android (via termux) pkg install gum + +# Windows (via Scoop) +scoop install charm-gum ``` Or download it: diff --git a/choose/choose.go b/choose/choose.go index 7c3228d..31b1ca7 100644 --- a/choose/choose.go +++ b/choose/choose.go @@ -33,6 +33,7 @@ type model struct { index int limit int numSelected int + currentOrder int paginator paginator.Model aborted bool @@ -47,6 +48,7 @@ type model struct { type item struct { text string selected bool + order int } func (m model) Init() tea.Cmd { @@ -115,7 +117,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { continue } m.items[i].selected = true + m.items[i].order = m.currentOrder m.numSelected++ + m.currentOrder++ } case "A": if m.limit <= 1 { @@ -123,8 +127,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } for i := range m.items { m.items[i].selected = false + m.items[i].order = 0 } m.numSelected = 0 + m.currentOrder = 0 case "ctrl+c", "esc": m.aborted = true m.quitting = true @@ -140,7 +146,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.numSelected-- } else if m.numSelected < m.limit { m.items[m.index].selected = true + m.items[m.index].order = m.currentOrder m.numSelected++ + m.currentOrder++ } case "enter": m.quitting = true diff --git a/choose/command.go b/choose/command.go index 8c7ba7c..791c41a 100644 --- a/choose/command.go +++ b/choose/command.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "sort" "strings" "github.com/alecthomas/kong" @@ -56,10 +57,12 @@ func (o Options) Run() error { hasSelectedItems := len(o.Selected) > 0 startingIndex := 0 + currentOrder := 0 - var items = make([]item, len(o.Options)) + items := make([]item, len(o.Options)) for i, option := range o.Options { + var order int // Check if the option should be selected. isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option) // If the option is selected then increment the current selected count. @@ -71,10 +74,12 @@ func (o Options) Run() error { isSelected = false } else { 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 @@ -94,6 +99,7 @@ func (o Options) Run() error { tm, err := tea.NewProgram(model{ index: startingIndex, + currentOrder: currentOrder, height: o.Height, cursor: o.Cursor, selectedPrefix: o.SelectedPrefix, @@ -119,6 +125,12 @@ func (o Options) Run() error { 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 for _, item := range m.items { diff --git a/confirm/confirm.go b/confirm/confirm.go index 6159e8d..c3aeaa5 100644 --- a/confirm/confirm.go +++ b/confirm/confirm.go @@ -45,9 +45,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil case tea.KeyMsg: switch msg.String() { - case "ctrl+c", "esc": - m.confirmation = false + case "ctrl+c": m.aborted = true + fallthrough + case "esc": + m.confirmation = false m.quitting = true return m, tea.Quit case "q", "n", "N": diff --git a/go.mod b/go.mod index ab043f5..185475b 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/charmbracelet/glamour v0.5.1-0.20220727184942-e70ff2d969da github.com/charmbracelet/lipgloss v0.6.1-0.20220930064401-ae7c84f7b158 github.com/dlclark/regexp2 v1.7.0 // indirect - github.com/dustin/go-humanize v1.0.0 + github.com/dustin/go-humanize v1.0.1 github.com/mattn/go-runewidth v0.0.14 github.com/microcosm-cc/bluemonday v1.0.21 // indirect github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect diff --git a/go.sum b/go.sum index f63b871..df2f910 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo= github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= -github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= -github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=