Merge branch 'main' into feature/218/Adding_timeout_to_most_commands

This commit is contained in:
Dieter Eickstaedt 2023-01-19 19:19:47 +01:00 committed by GitHub
commit 3efc3b5485
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 7 deletions

View file

@ -123,6 +123,9 @@ apk add gum
# Android (via termux) # Android (via termux)
pkg install gum pkg install gum
# Windows (via Scoop)
scoop install charm-gum
``` ```
Or download it: Or download it:

View file

@ -33,6 +33,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
@ -47,6 +48,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 { func (m model) Init() tea.Cmd {
@ -115,7 +117,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 {
@ -123,8 +127,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
@ -140,7 +146,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

View file

@ -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,
@ -119,6 +125,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 {

View file

@ -45,9 +45,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil return m, nil
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "esc": case "ctrl+c":
m.confirmation = false
m.aborted = true m.aborted = true
fallthrough
case "esc":
m.confirmation = false
m.quitting = true m.quitting = true
return m, tea.Quit return m, tea.Quit
case "q", "n", "N": case "q", "n", "N":

2
go.mod
View file

@ -11,7 +11,7 @@ require (
github.com/charmbracelet/glamour v0.5.1-0.20220727184942-e70ff2d969da github.com/charmbracelet/glamour v0.5.1-0.20220727184942-e70ff2d969da
github.com/charmbracelet/lipgloss v0.6.1-0.20220930064401-ae7c84f7b158 github.com/charmbracelet/lipgloss v0.6.1-0.20220930064401-ae7c84f7b158
github.com/dlclark/regexp2 v1.7.0 // indirect 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/mattn/go-runewidth v0.0.14
github.com/microcosm-cc/bluemonday v1.0.21 // indirect github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect

4
go.sum
View file

@ -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.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 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= 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.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= 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 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=