Skip to content

Commit

Permalink
solved merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelFangel committed Sep 17, 2023
2 parents 91d5b75 + 77aa864 commit 0a28b84
Show file tree
Hide file tree
Showing 30 changed files with 414 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
go-version: ~1.18

- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Download Go modules
run: go mod download
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint-soft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
go-version: ^1

- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
go-version: ^1

- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ apk add gum
# Android (via termux)
pkg install gum

# Windows (via Scoop)
# Windows (via WinGet or Scoop)
winget install charmbracelet.gum
scoop install charm-gum
```

Expand Down Expand Up @@ -294,6 +295,8 @@ gum pager < README.md
Display a spinner while running a script or command. The spinner will
automatically stop after the given command exits.

To view or pipe the command's output, use the `--show-output` flag.

```bash
gum spin --spinner dot --title "Buying Bubble Gum..." -- sleep 5
```
Expand Down
33 changes: 28 additions & 5 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ package choose

import (
"strings"
"time"

"github.com/charmbracelet/gum/timeout"

"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -39,6 +42,8 @@ type model struct {
headerStyle lipgloss.Style
itemStyle lipgloss.Style
selectedItemStyle lipgloss.Style
hasTimeout bool
timeout time.Duration
}

type item struct {
Expand All @@ -47,13 +52,27 @@ type item struct {
order int
}

func (m model) Init() tea.Cmd { return nil }
func (m model) Init() tea.Cmd {
return timeout.Init(m.timeout, nil)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
return m, nil

case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 {
m.quitting = true
// If the user hasn't selected any items in a multi-select.
// Then we select the item that they have pressed enter on. If they
// have selected items, then we simply return them.
if m.numSelected < 1 {
m.items[m.index].selected = true
}
return m, tea.Quit
}
m.timeout = msg.TimeoutValue
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
case tea.KeyMsg:
start, end := m.paginator.GetSliceBounds(len(m.items))
switch keypress := msg.String(); keypress {
Expand Down Expand Up @@ -81,10 +100,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "left", "h", "ctrl+b":
m.index = clamp(m.index-m.height, 0, len(m.items)-1)
m.paginator.PrevPage()
case "G":
case "G", "end":
m.index = len(m.items) - 1
m.paginator.Page = m.paginator.TotalPages - 1
case "g":
case "g", "home":
m.index = 0
m.paginator.Page = 0
case "a":
Expand Down Expand Up @@ -151,6 +170,7 @@ func (m model) View() string {
}

var s strings.Builder
var timeoutStr string

start, end := m.paginator.GetSliceBounds(len(m.items))
for i, item := range m.items[start:end] {
Expand All @@ -161,7 +181,10 @@ func (m model) View() string {
}

if item.selected {
s.WriteString(m.selectedItemStyle.Render(m.selectedPrefix + item.text))
if m.hasTimeout {
timeoutStr = timeout.Str(m.timeout)
}
s.WriteString(m.selectedItemStyle.Render(m.selectedPrefix + item.text + timeoutStr))
} else if i == m.index%m.height {
s.WriteString(m.cursorStyle.Render(m.cursorPrefix + item.text))
} else {
Expand Down
5 changes: 4 additions & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (o Options) Run() error {
// If we've set no limit then we can simply select as many options as there
// are so let's set the limit to the number of options.
if o.NoLimit {
o.Limit = len(o.Options)
o.Limit = len(o.Options) + 1
}

if len(o.Selected) > o.Limit {
Expand Down Expand Up @@ -93,6 +93,7 @@ func (o Options) Run() error {
pager.ActiveDot = subduedStyle.Render("•")
pager.InactiveDot = verySubduedStyle.Render("•")
pager.KeyMap = paginator.KeyMap{}
pager.Page = startingIndex / o.Height

// Disable Keybindings since we will control it ourselves.
tm, err := tea.NewProgram(model{
Expand All @@ -112,6 +113,8 @@ func (o Options) Run() error {
itemStyle: o.ItemStyle.ToLipgloss(),
selectedItemStyle: o.SelectedItemStyle.ToLipgloss(),
numSelected: currentSelected,
hasTimeout: o.Timeout > 0,
timeout: o.Timeout,
}, tea.WithOutput(os.Stderr)).Run()

if err != nil {
Expand Down
38 changes: 21 additions & 17 deletions choose/options.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package choose

import "github.com/charmbracelet/gum/style"
import (
"time"

"github.com/charmbracelet/gum/style"
)

// Options is the customization options for the choose command.
type Options struct {
Options []string `arg:"" optional:"" help:"Options to choose from."`

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"`
Header string `help:"Header value" default:"" env:"GUM_CHOOSE_HEADER"`
CursorPrefix string `help:"Prefix to show on the cursor item (hidden if limit is 1)" default:" " env:"GUM_CHOOSE_CURSOR_PREFIX"`
SelectedPrefix string `help:"Prefix to show on selected items (hidden if limit is 1)" default:" " env:"GUM_CHOOSE_SELECTED_PREFIX"`
UnselectedPrefix string `help:"Prefix to show on unselected items (hidden if limit is 1)" default:"" env:"GUM_CHOOSE_UNSELECTED_PREFIX"`
Selected []string `help:"Options that should start as selected" default:"" env:"GUM_CHOOSE_SELECTED"`
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_CHOOSE_HEADER_"`
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"`
Options []string `arg:"" optional:"" help:"Options to choose from."`
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"`
Header string `help:"Header value" default:"" env:"GUM_CHOOSE_HEADER"`
CursorPrefix string `help:"Prefix to show on the cursor item (hidden if limit is 1)" default:"" env:"GUM_CHOOSE_CURSOR_PREFIX"`
SelectedPrefix string `help:"Prefix to show on selected items (hidden if limit is 1)" default:" " env:"GUM_CHOOSE_SELECTED_PREFIX"`
UnselectedPrefix string `help:"Prefix to show on unselected items (hidden if limit is 1)" default:" " env:"GUM_CHOOSE_UNSELECTED_PREFIX"`
Selected []string `help:"Options that should start as selected" default:"" env:"GUM_CHOOSE_SELECTED"`
CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_CHOOSE_HEADER_"`
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"`
Timeout time.Duration `help:"Timeout until choose returns selected element" default:"0" env:"GUM_CCHOOSE_TIMEOUT"` // including timeout command options [Timeout,...]
}
4 changes: 3 additions & 1 deletion confirm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/charmbracelet/gum/internal/exit"

"github.com/alecthomas/kong"
tea "github.com/charmbracelet/bubbletea"

Expand Down Expand Up @@ -31,7 +33,7 @@ func (o Options) Run() error {
}

if m.(model).aborted {
os.Exit(130)
os.Exit(exit.StatusAborted)
} else if m.(model).confirmation {
os.Exit(0)
} else {
Expand Down
64 changes: 22 additions & 42 deletions confirm/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
package confirm

import (
"fmt"
"time"

"github.com/charmbracelet/gum/timeout"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
Expand All @@ -37,21 +38,8 @@ type model struct {
unselectedStyle lipgloss.Style
}

const tickInterval = time.Second

type tickMsg struct{}

func tick() tea.Cmd {
return tea.Tick(tickInterval, func(time.Time) tea.Msg {
return tickMsg{}
})
}

func (m model) Init() tea.Cmd {
if m.timeout > 0 {
return tick()
}
return nil
return timeout.Init(m.timeout, m.defaultSelection)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -61,6 +49,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
m.confirmation = false
m.aborted = true
fallthrough
case "esc":
Expand All @@ -85,14 +74,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.confirmation = true
return m, tea.Quit
}
case tickMsg:
if m.timeout <= 0 {
case timeout.TickTimeoutMsg:

if msg.TimeoutValue <= 0 {
m.quitting = true
m.confirmation = m.defaultSelection
return m, tea.Quit
}
m.timeout -= tickInterval
return m, tick()

m.timeout = msg.TimeoutValue
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
}
return m, nil
}
Expand All @@ -102,27 +93,23 @@ func (m model) View() string {
return ""
}

var aff, neg, timeout, affirmativeTimeout, negativeTimeout string

var aff, neg, timeoutStrYes, timeoutStrNo string
timeoutStrNo = ""
timeoutStrYes = ""
if m.hasTimeout {
timeout = fmt.Sprintf(" (%d)", max(0, int(m.timeout.Seconds())))
}

// set timer based on defaultSelection
if m.defaultSelection {
affirmativeTimeout = m.affirmative + timeout
negativeTimeout = m.negative
} else {
affirmativeTimeout = m.affirmative
negativeTimeout = m.negative + timeout
if m.defaultSelection {
timeoutStrYes = timeout.Str(m.timeout)
} else {
timeoutStrNo = timeout.Str(m.timeout)
}
}

if m.confirmation {
aff = m.selectedStyle.Render(affirmativeTimeout)
neg = m.unselectedStyle.Render(negativeTimeout)
aff = m.selectedStyle.Render(m.affirmative + timeoutStrYes)
neg = m.unselectedStyle.Render(m.negative + timeoutStrNo)
} else {
aff = m.unselectedStyle.Render(affirmativeTimeout)
neg = m.selectedStyle.Render(negativeTimeout)
aff = m.unselectedStyle.Render(m.affirmative + timeoutStrYes)
neg = m.selectedStyle.Render(m.negative + timeoutStrNo)
}

// If the option is intentionally empty, do not show it.
Expand All @@ -132,10 +119,3 @@ func (m model) View() string {

return lipgloss.JoinVertical(lipgloss.Center, m.promptStyle.Render(m.prompt), lipgloss.JoinHorizontal(lipgloss.Left, aff, neg))
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
14 changes: 7 additions & 7 deletions confirm/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

// Options is the customization options for the confirm command.
type Options struct {
Affirmative string `help:"The title of the affirmative action" default:"Yes"`
Negative string `help:"The title of the negative action" default:"No"`
Default bool `help:"Default confirmation action" default:"true"`
Timeout time.Duration `help:"Timeout for confirmation" default:"0" env:"GUM_CONFIRM_TIMEOUT"`
Prompt string `arg:"" help:"Prompt to display." default:"Are you sure?"`
PromptStyle style.Styles `embed:"" prefix:"prompt." help:"The style of the prompt" set:"defaultMargin=1 0 0 0" envprefix:"GUM_CONFIRM_PROMPT_"`
Default bool `help:"Default confirmation action" default:"true"`
Affirmative string `help:"The title of the affirmative action" default:"Yes"`
Negative string `help:"The title of the negative action" default:"No"`
Prompt string `arg:"" help:"Prompt to display." default:"Are you sure?"`
PromptStyle style.Styles `embed:"" prefix:"prompt." help:"The style of the prompt" set:"defaultMargin=1 0 0 0" envprefix:"GUM_CONFIRM_PROMPT_"`
//nolint:staticcheck
SelectedStyle style.Styles `embed:"" prefix:"selected." help:"The style of the selected action" set:"defaultBackground=212" set:"defaultForeground=230" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_SELECTED_"`
//nolint:staticcheck
UnselectedStyle style.Styles `embed:"" prefix:"unselected." help:"The style of the unselected action" set:"defaultBackground=235" set:"defaultForeground=254" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_UNSELECTED_"`
UnselectedStyle style.Styles `embed:"" prefix:"unselected." help:"The style of the unselected action" set:"defaultBackground=235" set:"defaultForeground=254" set:"defaultPadding=0 3" set:"defaultMargin=1 1" envprefix:"GUM_CONFIRM_UNSELECTED_"`
Timeout time.Duration `help:"Timeout until confirm returns selected value or default if provided" default:"0" env:"GUM_CONFIRM_TIMEOUT"`
}
10 changes: 8 additions & 2 deletions file/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"os"
"path/filepath"

"github.com/charmbracelet/gum/internal/exit"

"github.com/alecthomas/kong"
"github.com/charmbracelet/bubbles/filepicker"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/style"
)

Expand Down Expand Up @@ -46,7 +47,12 @@ func (o Options) Run() error {
fp.Styles.Selected = o.SelectedStyle.ToLipgloss()
fp.Styles.FileSize = o.FileSizeStyle.ToLipgloss()

m := model{filepicker: fp}
m := model{
filepicker: fp,
timeout: o.Timeout,
hasTimeout: o.Timeout > 0,
aborted: false,
}

tm, err := tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
if err != nil {
Expand Down
Loading

0 comments on commit 0a28b84

Please sign in to comment.