From 08ed3e251974347397ca74ba52de48491815a08a Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Fri, 13 Jan 2023 00:06:11 +0530 Subject: [PATCH 1/4] feat(choose): keep order of selected items (#182) * feat(choose): keep order of selected items * fix: KeepOrder -> Ordered Co-authored-by: Maas Lalani --- choose/choose.go | 8 ++++++++ choose/command.go | 16 ++++++++++++++-- choose/options.go | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/choose/choose.go b/choose/choose.go index 9aa6f93..0e898fa 100644 --- a/choose/choose.go +++ b/choose/choose.go @@ -30,6 +30,7 @@ type model struct { index int limit int numSelected int + currentOrder int paginator paginator.Model aborted bool @@ -42,6 +43,7 @@ type model struct { type item struct { text string selected bool + order int } func (m model) Init() tea.Cmd { return nil } @@ -96,7 +98,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 { @@ -104,8 +108,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 @@ -120,7 +126,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 6dd6b99..6c39788 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, @@ -117,6 +123,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/choose/options.go b/choose/options.go index 68eb412..f6ee123 100644 --- a/choose/options.go +++ b/choose/options.go @@ -8,6 +8,7 @@ type Options struct { 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"` + 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"` 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"` From 36ef76185c49baf998a2ff6c0e8da948410d2579 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Fri, 13 Jan 2023 12:08:06 -0500 Subject: [PATCH 2/4] fix(confirm): esc should not exit with 130 --- confirm/confirm.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/confirm/confirm.go b/confirm/confirm.go index b532e52..7e6eeb1 100644 --- a/confirm/confirm.go +++ b/confirm/confirm.go @@ -58,9 +58,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": From 80f3598efd8abba8179c0e5e4370be231a03a20a Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 16 Jan 2023 04:31:07 +0000 Subject: [PATCH 3/4] feat(deps): bump github.com/dustin/go-humanize from 1.0.0 to 1.0.1 Bumps [github.com/dustin/go-humanize](https://github.com/dustin/go-humanize) from 1.0.0 to 1.0.1. - [Release notes](https://github.com/dustin/go-humanize/releases) - [Commits](https://github.com/dustin/go-humanize/compare/v1.0.0...v1.0.1) --- updated-dependencies: - dependency-name: github.com/dustin/go-humanize dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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= From 78bb3b5f06a353457ae3b6a1611c9113814a6bf4 Mon Sep 17 00:00:00 2001 From: Kris Schneider <101912712+Zliced13@users.noreply.github.com> Date: Sun, 18 Dec 2022 15:51:19 -0700 Subject: [PATCH 4/4] Added Scoop installation instructions for gum --- README.md | 3 +++ 1 file changed, 3 insertions(+) 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: