Skip to content

Commit

Permalink
added spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd committed Feb 7, 2024
1 parent 027d280 commit c55705a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions internal/tui/pay_invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/btcsuite/btcd/btcutil"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
Expand All @@ -22,6 +23,7 @@ type PayInvoiceModel struct {
keys keyMap
form *huh.Form
invoiceState PaymentState
spinner spinner.Model
}

// PaymentState indicates the state of a Bolt 11 invoice payment
Expand Down Expand Up @@ -51,6 +53,10 @@ func newPayInvoiceModel(service *lndclient.GrpcLndServices, base *BaseModel) *Pa
m.base.pushView(&m)
m.form = getInvoicePaymentForm()
m.invoiceState = PaymentStateNone
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
m.spinner = s

return &m
}
Expand All @@ -71,7 +77,7 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {

case key.Matches(msg, Keymap.Enter):
if m.form.State == huh.StateCompleted {
if m.form.State == huh.StateCompleted && m.invoiceState == PaymentStateNone {
m.invoiceState = PaymentStateSending
return m, paymentCreatedMsg
}
Expand All @@ -83,6 +89,8 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

var cmds []tea.Cmd
// Tick the spinner
m.spinner, _ = m.spinner.Update(msg)

// Process the form
if m.form != nil {
Expand Down Expand Up @@ -111,15 +119,15 @@ func getInvoicePaymentForm() *huh.Form {
}

func (m PayInvoiceModel) Init() tea.Cmd {
return nil
return m.spinner.Tick
}

func (m PayInvoiceModel) View() string {
s := m.styles
v := strings.TrimSuffix(m.form.View(), "\n")
form := lipgloss.DefaultRenderer().NewStyle().Margin(1, 0).Render(v)
if m.invoiceState == PaymentStateSending {
return lipgloss.JoinVertical(lipgloss.Left, "sending")
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(m.getPaymentPendingView()))
} else if m.invoiceState == PaymentStateSettled {
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(m.getPaymentSettledView()))
} else if m.form.State == huh.StateCompleted {
Expand All @@ -129,6 +137,12 @@ func (m PayInvoiceModel) View() string {
return lipgloss.JoinVertical(lipgloss.Left, form)
}

func (m PayInvoiceModel) getPaymentPendingView() string {

return m.styles.HeaderText.Render("Invoice in flight") + "\n\n" +
fmt.Sprintf("\n\n %s Sending payment\n\n", m.spinner.View())
}

func (m PayInvoiceModel) getPaymentSettledView() string {
s := m.styles
return s.HeaderText.Render("Invoice settled") + "\n\n" +
Expand Down

0 comments on commit c55705a

Please sign in to comment.