From 983a50776b31e2a973009db96420e8fde01c0f87 Mon Sep 17 00:00:00 2001 From: bashbunni <15822994+bashbunni@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:44:33 -0700 Subject: [PATCH] feat: add `WithButtonAlignment` function for button positioning (#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 --- field_confirm.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/field_confirm.go b/field_confirm.go index 708033fc..c2a88ed5 100644 --- a/field_confirm.go +++ b/field_confirm.go @@ -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, } } @@ -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) @@ -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()) @@ -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