Skip to content

Commit

Permalink
feat(confirm): remove vertical stacked buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Nov 27, 2024
1 parent f8b3922 commit d5e3f5e
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions field_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,47 +241,32 @@ func (c *Confirm) View() string {
styles := c.activeStyles()

var sb strings.Builder
if c.inline {
sb.WriteString(styles.Title.Render(c.title.val))
} else {
sb.WriteString(styles.Title.Width(c.width).Render(c.title.val))
}

sb.WriteString(styles.Title.Render(c.title.val))
if c.err != nil {
sb.WriteString(styles.ErrorIndicator.String())
}

description := styles.Description.Render(c.description.val)

if !c.inline && (c.description.val != "" || c.description.fn != nil) {
sb.WriteString("\n")
}
sb.WriteString(description)

if !c.inline {
sb.WriteString(styles.Description.Width(c.width).Render(c.description.val))
sb.WriteString("\n")
sb.WriteString("\n")
} else {
sb.WriteString(styles.Description.Render(c.description.val))
}

var negative string
var affirmative string

// Calculate padding to make buttons equal. The narrowest button has padding
// added to its right.
negativePad := styles.BlurredButton.GetHorizontalPadding() / 2
affirmativePad := styles.BlurredButton.GetHorizontalPadding() / 2
if lipgloss.Width(c.negative) < lipgloss.Width(c.affirmative) {
negativePad += (lipgloss.Width(c.affirmative) - lipgloss.Width(c.negative))
} else if lipgloss.Width(c.affirmative) < lipgloss.Width(c.negative) {
affirmativePad += (lipgloss.Width(c.negative) - lipgloss.Width(c.affirmative))
}

if c.negative != "" {
if c.accessor.Get() {
affirmative = styles.FocusedButton.PaddingRight(affirmativePad).Render(c.affirmative)
negative = styles.BlurredButton.PaddingRight(negativePad).Render(c.negative)
affirmative = styles.FocusedButton.Render(c.affirmative)
negative = styles.BlurredButton.Render(c.negative)
} else {
affirmative = styles.BlurredButton.PaddingRight(affirmativePad).Render(c.affirmative)
negative = styles.FocusedButton.PaddingRight(negativePad).Render(c.negative)
affirmative = styles.BlurredButton.Render(c.affirmative)
negative = styles.FocusedButton.Render(c.negative)
}
c.keymap.Reject.SetHelp("n", c.negative)
} else {
Expand All @@ -290,13 +275,20 @@ func (c *Confirm) View() string {
}

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

buttonsRow := lipgloss.JoinHorizontal(c.buttonAlignment, affirmative, negative)
// Align the buttons vertically if the window is too narrow.
if c.width < lipgloss.Width(buttonsRow) {
buttonsRow = lipgloss.JoinVertical(lipgloss.Left, affirmative, negative)

promptWidth := lipgloss.Width(sb.String())
buttonsWidth := lipgloss.Width(buttonsRow)

renderWidth := promptWidth
if buttonsWidth > renderWidth {
renderWidth = buttonsWidth
}

sb.WriteString(buttonsRow)
style := lipgloss.NewStyle().Width(renderWidth).Align(c.buttonAlignment)

sb.WriteString(style.Render(buttonsRow))
return styles.Base.Render(sb.String())
}

Expand Down

0 comments on commit d5e3f5e

Please sign in to comment.