Skip to content

Commit

Permalink
feat: add WithButtonAlignment function for button positioning (char…
Browse files Browse the repository at this point in the history
…mbracelet#427)

* feat: Adjust `Confirm Buttons` within the Confirm Field

- Added in method to adjust the position of the confirm buttons,
  within the confirm field
- If the user does not adjust the position of the confirm buttons,
  it defaults to the center of the confirm view field
- If the user provides the position of the confirm buttons,
  it overrides the position of the buttons in the confirm view field

* docs(confirm): add godoc to WithButtonAlignment

---------

Co-authored-by: pbj <pbj@pop-os.localdomain>
  • Loading branch information
bashbunni and pbj authored Oct 11, 2024
1 parent c9b2c9c commit 983a507
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions field_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@ type Confirm struct {
focused bool

// options
width int
height int
inline bool
accessible bool
theme *Theme
keymap ConfirmKeyMap
width int
height int
inline bool
accessible bool
theme *Theme
keymap ConfirmKeyMap
buttonAlignment lipgloss.Position
}

// NewConfirm returns a new confirm field.
func NewConfirm() *Confirm {
return &Confirm{
accessor: &EmbeddedAccessor[bool]{},
id: nextID(),
title: Eval[string]{cache: make(map[uint64]string)},
description: Eval[string]{cache: make(map[uint64]string)},
affirmative: "Yes",
negative: "No",
validate: func(bool) error { return nil },
accessor: &EmbeddedAccessor[bool]{},
id: nextID(),
title: Eval[string]{cache: make(map[uint64]string)},
description: Eval[string]{cache: make(map[uint64]string)},
affirmative: "Yes",
negative: "No",
validate: func(bool) error { return nil },
buttonAlignment: lipgloss.Center,
}
}

Expand Down Expand Up @@ -268,7 +270,7 @@ func (c *Confirm) View() string {

c.keymap.Accept.SetHelp("y", c.affirmative)

buttonsRow := lipgloss.JoinHorizontal(lipgloss.Center, affirmative, negative)
buttonsRow := lipgloss.JoinHorizontal(c.buttonAlignment, affirmative, negative)

promptWidth := lipgloss.Width(sb.String())
buttonsWidth := lipgloss.Width(buttonsRow)
Expand All @@ -278,7 +280,7 @@ func (c *Confirm) View() string {
renderWidth = buttonsWidth
}

style := lipgloss.NewStyle().Width(renderWidth).Align(lipgloss.Center)
style := lipgloss.NewStyle().Width(renderWidth).Align(c.buttonAlignment)

sb.WriteString(style.Render(buttonsRow))
return styles.Base.Render(sb.String())
Expand Down Expand Up @@ -350,6 +352,12 @@ func (c *Confirm) WithPosition(p FieldPosition) Field {
return c
}

// WithButtonAlignment sets the button position of the confirm field.
func (c *Confirm) WithButtonAlignment(p lipgloss.Position) *Confirm {
c.buttonAlignment = p
return c
}

// GetKey returns the key of the field.
func (c *Confirm) GetKey() string {
return c.key
Expand Down

0 comments on commit 983a507

Please sign in to comment.