Skip to content

Commit

Permalink
Merge branch 'main' into output-on-error
Browse files Browse the repository at this point in the history
  • Loading branch information
jelle-bigbridge authored Jul 20, 2023
2 parents 868d06c + d1ad453 commit 13a37df
Show file tree
Hide file tree
Showing 25 changed files with 397 additions and 169 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,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
29 changes: 26 additions & 3 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 @@ -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
2 changes: 2 additions & 0 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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
18 changes: 17 additions & 1 deletion file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@
package file

import (
"time"

"github.com/charmbracelet/bubbles/filepicker"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/timeout"
)

type model struct {
filepicker filepicker.Model
selectedPath string
aborted bool
quitting bool
timeout time.Duration
hasTimeout bool
}

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

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -37,6 +45,14 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.quitting = true
return m, tea.Quit
}
case timeout.TickTimeoutMsg:
if msg.TimeoutValue <= 0 {
m.quitting = true
m.aborted = true
return m, tea.Quit
}
m.timeout = msg.TimeoutValue
return m, timeout.Tick(msg.TimeoutValue, msg.Data)
}
var cmd tea.Cmd
m.filepicker, cmd = m.filepicker.Update(msg)
Expand Down
9 changes: 7 additions & 2 deletions file/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package file

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

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

// Options are the options for the file command.
type Options struct {
Expand All @@ -21,5 +25,6 @@ type Options struct {
//nolint:staticcheck
SelectedStyle style.Styles `embed:"" prefix:"selected." help:"The style to use for the selected item" set:"defaultBold=true" set:"defaultForeground=212" envprefix:"GUM_FILE_SELECTED_"`
//nolint:staticcheck
FileSizeStyle style.Styles `embed:"" prefix:"file-size." help:"The style to use for file sizes" set:"defaultWidth=8" set:"defaultAlign=right" set:"defaultForeground=240" envprefix:"GUM_FILE_FILE_SIZE_"`
FileSizeStyle style.Styles `embed:"" prefix:"file-size." help:"The style to use for file sizes" set:"defaultWidth=8" set:"defaultAlign=right" set:"defaultForeground=240" envprefix:"GUM_FILE_FILE_SIZE_"`
Timeout time.Duration `help:"Timeout until command aborts without a selection" default:"0" env:"GUM_FILE_TIMEOUT"`
}
Loading

0 comments on commit 13a37df

Please sign in to comment.