Skip to content

Commit

Permalink
refactor(tui): cleaned up styling (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd authored Jan 5, 2024
1 parent 52f908a commit 952516a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 52 deletions.
7 changes: 3 additions & 4 deletions cmd/flash/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (
)

func main() {
logger := log.NewWithOptions(os.Stderr, log.Options{

})
logger := log.NewWithOptions(os.Stderr, log.Options{})
styles := tui.GetDefaultStyles()
// Arguments
tlsCertFile := flag.String("c", "", "TLS Certificate file")
adminMacaroon := flag.String("m", "", "Admin Macaroon")
Expand All @@ -28,7 +27,7 @@ func main() {
if *tlsCertFile != "" && *adminMacaroon != "" {
encryptionKey := credentials.EncryptCredentials(*tlsCertFile, *adminMacaroon)
log.Info("Encrypted credentials file 'auth.bin' saved.\nEncryption key:" +
tui.Keyword(encryptionKey) + "\n\nauth.bin with the encryption key can now be used to connect to the node")
styles.Keyword(encryptionKey) + "\n\nauth.bin with the encryption key can now be used to connect to the node")
return
}

Expand Down
43 changes: 23 additions & 20 deletions internal/tui/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var formSelection string

func InitDashboard(service *lndclient.GrpcLndServices, nodeData lnd.NodeData) *DashboardModel {
m := DashboardModel{lndService: service, ctx: context.Background(), nodeData: nodeData}

m.styles = GetDefaultStyles()
return &m
}

Expand All @@ -50,7 +50,7 @@ func (m DashboardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
windowSizeMsg = msg

v, h := borderedStyle.GetFrameSize()
v, h := m.styles.BorderedStyle.GetFrameSize()
m.initData(windowSizeMsg.Width-h, windowSizeMsg.Height-v)
m.loaded = true

Expand Down Expand Up @@ -95,6 +95,8 @@ func (m DashboardModel) Init() tea.Cmd {
}

func (m DashboardModel) View() string {
s := m.styles

if m.quitting {
return ""
}
Expand All @@ -109,33 +111,33 @@ func (m DashboardModel) View() string {

listsView = lipgloss.JoinHorizontal(
lipgloss.Center,
focusedStyle.Render(channelsView),
borderedStyle.Render(paymentsView),
s.FocusedStyle.Render(channelsView),
s.BorderedStyle.Render(paymentsView),
)

case payments:
listsView = lipgloss.JoinHorizontal(
lipgloss.Center,
borderedStyle.Render(channelsView),
focusedStyle.Render(paymentsView),
s.BorderedStyle.Render(channelsView),
s.FocusedStyle.Render(paymentsView),
)

default:
listsView = lipgloss.JoinHorizontal(
lipgloss.Center,
borderedStyle.Render(channelsView),
borderedStyle.Render(paymentsView),
s.BorderedStyle.Render(channelsView),
s.BorderedStyle.Render(paymentsView),
)
}

nodeInfoView := lipgloss.JoinVertical(lipgloss.Left, borderedStyle.Render(
keyword(m.nodeData.NodeInfo.Alias)+"\n"+m.nodeData.NodeInfo.PubKey+
nodeInfoView := lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(
s.Keyword(m.nodeData.NodeInfo.Alias)+"\n"+m.nodeData.NodeInfo.PubKey+
"\nLnd v"+m.nodeData.NodeInfo.Version))

balanceView := lipgloss.JoinVertical(lipgloss.Left, borderedStyle.Render(
subKeyword("Lightning Balance ")+m.nodeData.NodeInfo.ChannelBalance+
"\n"+subKeyword("Lightning Capacity ")+m.nodeData.NodeInfo.TotalCapacity+
"\n"+subKeyword("Onchain Balance ")+m.nodeData.NodeInfo.OnChainBalance))
balanceView := lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(
s.SubKeyword("Lightning Balance ")+m.nodeData.NodeInfo.ChannelBalance+
"\n"+s.SubKeyword("Lightning Capacity ")+m.nodeData.NodeInfo.TotalCapacity+
"\n"+s.SubKeyword("Onchain Balance ")+m.nodeData.NodeInfo.OnChainBalance))

topView := lipgloss.JoinHorizontal(lipgloss.Left,
nodeInfoView, balanceView)
Expand All @@ -155,27 +157,27 @@ func (m DashboardModel) View() string {
}

func (m *DashboardModel) getPaymentTools() string {
style := borderedStyle
style := m.styles.BorderedStyle
if m.focused == paymentTools {
style = focusedStyle
style = m.styles.FocusedStyle
}

return style.Render(m.forms[0].WithShowHelp(false).View())
}

func (m *DashboardModel) getMessageTools() string {
style := borderedStyle
style := m.styles.BorderedStyle
if m.focused == messageTools {
style = focusedStyle
style = m.styles.FocusedStyle
}

return style.Render(m.forms[2].WithShowHelp(false).View())
}

func (m *DashboardModel) getChannelTools() string {
style := borderedStyle
style := m.styles.BorderedStyle
if m.focused == channelTools {
style = focusedStyle
style = m.styles.FocusedStyle
}

return style.Render(m.forms[1].WithShowHelp(false).View())
Expand Down Expand Up @@ -264,6 +266,7 @@ func (m *DashboardModel) Next() {

// Model for the Dashboard view
type DashboardModel struct {
styles *Styles
focused dashboardComponent
lists []list.Model
forms []*huh.Form
Expand Down
16 changes: 8 additions & 8 deletions internal/tui/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func isFormReady(v bool) error {
func NewInvoiceModel(context context.Context, service *lndclient.GrpcLndServices, state InvoiceState, dashboard *DashboardModel) InvoiceModel {
m := InvoiceModel{width: maxWidth, lndService: service, ctx: context, invoiceState: state, dashboard: dashboard}
m.lg = lipgloss.DefaultRenderer()
m.styles = NewStyles(m.lg)
m.styles = NewDialogStyles(m.lg)
m.form = huh.NewForm(
huh.NewGroup(
huh.NewInput().
Expand Down Expand Up @@ -270,8 +270,8 @@ func (m InvoiceModel) printQrCode() string {
// View to show when invoice generation is cancelled
func (m InvoiceModel) getInvoiceCancelView() string {
s := m.styles
view := lipgloss.JoinVertical(lipgloss.Left, borderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invoice Generation Cancelled")) +
fmt.Sprintf("\n%s\n", "The invoice generation was cancelled. No invoice data committed.") +
view := lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invoice Generation Cancelled"))+
fmt.Sprintf("\n%s\n", "The invoice generation was cancelled. No invoice data committed.")+
fmt.Sprintf("\n\n%s\n", "Press Esc to return")))

return view
Expand All @@ -282,15 +282,15 @@ func (m InvoiceModel) View() string {

switch m.invoiceState {
case StateSettled:
view := lipgloss.JoinVertical(lipgloss.Left, borderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invoice Settled")) +
fmt.Sprintf("\n%s\n", "The invoice was settled. Payment received") +
view := lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invoice Settled"))+
fmt.Sprintf("\n%s\n", "The invoice was settled. Payment received")+
fmt.Sprintf("\n\n%s\n", "Press Enter to return")))

return view

case StateExpired:
view := lipgloss.JoinVertical(lipgloss.Left, borderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invoice Expired")) +
fmt.Sprintf("\n%s\n", "The invoice was expired. No payment settled.") +
view := lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invoice Expired"))+
fmt.Sprintf("\n%s\n", "The invoice was expired. No payment settled.")+
fmt.Sprintf("\n\n%s\n", "Press Enter to return")))
return view
}
Expand All @@ -303,7 +303,7 @@ func (m InvoiceModel) View() string {
fmt.Fprintf(&b, "\n%s\n\n", invoiceVal)
fmt.Fprintf(&b, "%s\n", m.printQrCode())

return lipgloss.JoinVertical(lipgloss.Left, borderedStyle.Render(b.String()))
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(b.String()))
default:
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := m.lg.NewStyle().Margin(1, 0).Render(v)
Expand Down
45 changes: 25 additions & 20 deletions internal/tui/styling.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@ import (
"github.com/muesli/termenv"
)

var term = termenv.EnvColorProfile()

// Styles contain style elements for views
type Styles struct {
Base,
HeaderText,
Status,
StatusHeader,
Highlight,
ErrorHeaderText,
BorderedStyle,
FocusedStyle,
Help lipgloss.Style
Keyword func(string) string
SubKeyword func(string) string
}

func GetDefaultStyles() *Styles {
return NewDialogStyles( lipgloss.DefaultRenderer())
}

func NewStyles(lg *lipgloss.Renderer) *Styles {
func NewDialogStyles(lg *lipgloss.Renderer) *Styles {
s := Styles{}
s.Base = lg.NewStyle().
Padding(1, 4, 0, 1)
Expand All @@ -37,29 +48,23 @@ func NewStyles(lg *lipgloss.Renderer) *Styles {
Foreground(red)
s.Help = lg.NewStyle().
Foreground(lipgloss.Color("240"))
return &s
}


/* Styling */
var (
term = termenv.EnvColorProfile()
keyword = makeFgStyle("211")
Keyword = makeFgStyle("211")
subKeyword = makeFgStyle("140")
s.Keyword = makeFgStyle("211")
s.SubKeyword = makeFgStyle("140")
s.BorderedStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62"))

borderedStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62"))
s.FocusedStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("169"))

focusedStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("169"))
)
return &s
}

// Return a function that will colorize the foreground of a given string.
func makeFgStyle(color string) func(string) string {
return termenv.Style{}.Foreground(term.Color(color)).Styled
}
}

0 comments on commit 952516a

Please sign in to comment.