Skip to content

Commit

Permalink
feat(confirm(selection)): add help view
Browse files Browse the repository at this point in the history
  • Loading branch information
fzdwx committed Aug 17, 2022
1 parent 2ba57fd commit 14bad27
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions _examples/confirm_selection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func main() {

val, _ := inf.NewConfirmWithSelection(
//confirm.WithDisableOutputResult(),
//confirm.WithDisableShowHelp(),
confirm.WithDefaultYes(),
).Display()

Expand Down
14 changes: 14 additions & 0 deletions components/selection/confirm/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ type KeyMap struct {
Quit key.Binding
}

func (k KeyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Switch, k.Choice}
}

func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Switch, k.Choice},
{k.Quit},
}
}

// Confirm with components.Selection
type Confirm struct {
startUp *components.StartUp
Expand All @@ -23,6 +34,7 @@ type Confirm struct {
No string
OutputResult bool
DefaultVal bool
ShowHelp bool

FocusSymbol string
UnFocusSymbol string
Expand All @@ -48,6 +60,7 @@ func WithSelection(ops ...Option) *Confirm {
KeyMap: DefaultKeyBinding(),
OutputResult: true,
DefaultVal: false,
ShowHelp: true,
ops: ops,

FocusSymbol: FocusSymbol,
Expand Down Expand Up @@ -83,6 +96,7 @@ func (c *Confirm) init() {
c.inner = newInner(components.NewSelection([]string{c.No, c.Yes}))
c.inner.focusInterval = c.FocusInterval
c.inner.unFocusInterval = c.UnFocusInterval
c.inner.ShowHelp = c.ShowHelp
// default true
c.inner.DefaultVal = c.DefaultVal
c.inner.keyMap = c.KeyMap
Expand Down
16 changes: 14 additions & 2 deletions components/selection/confirm/inner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package confirm

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/fzdwx/infinite/components"
Expand All @@ -11,7 +12,10 @@ import (
type switchIt int

type inner struct {
selection *components.Selection
selection *components.Selection
help help.Model
ShowHelp bool

focusPrompt string
unFocusPrompt string
focusInterval string
Expand All @@ -25,7 +29,11 @@ type inner struct {
}

func newInner(selection *components.Selection) *inner {
return &inner{selection: selection}
return &inner{
selection: selection,
help: help.New(),
ShowHelp: true,
}
}

func (i *inner) Init() tea.Cmd {
Expand Down Expand Up @@ -67,6 +75,10 @@ func (i *inner) View() string {
Write(i.interval()).
Write(rows[1]).Space().Write("/").Space().Write(rows[2])

if i.ShowHelp {
row.NewLine().Write(i.help.View(i.keyMap))
}

if i.outputResult {
row.NewLine()
}
Expand Down
12 changes: 10 additions & 2 deletions components/selection/confirm/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

var (
Yes = "Yes"
No = "No"
Yes = "Yes"
No = "No"
ShowHelp = true

FocusSymbol = theme.DefaultTheme.FocusSymbol
UnFocusSymbol = theme.DefaultTheme.UnFocusSymbol
Expand Down Expand Up @@ -43,6 +44,13 @@ func DefaultKeyBinding() KeyMap {

type Option func(c *Confirm)

// WithDisableShowHelp disable show help info.
func WithDisableShowHelp() Option {
return func(c *Confirm) {
c.ShowHelp = false
}
}

// WithDefaultYes set Confirm default val is `true`
func WithDefaultYes() Option {
return func(c *Confirm) {
Expand Down

0 comments on commit 14bad27

Please sign in to comment.