Skip to content

Commit

Permalink
feat: added channel model help
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd committed Jan 8, 2024
1 parent d0450dc commit 32d6041
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
35 changes: 33 additions & 2 deletions internal/tui/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/ardevd/flash/internal/lnd"
"github.com/btcsuite/btcd/btcutil"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -20,11 +22,31 @@ type ChannelModel struct {
ctx context.Context
htlcTable table.Model
base *BaseModel
help help.Model
keys keyMap
}

// Help

// ShortHelp returns keybindings to be shown in the mini help view. It's part
// of the key.Map interface.
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Help, k.Back, k.Quit}
}

// FullHelp returns keybindings for the expanded help view. It's part of the
// key.Map interface.
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Update, k.Close, k.ForceClose}, // first column
{k.Back, k.Quit}, // second column
}
}

func NewChannelModel(service *lndclient.GrpcLndServices, channel lnd.Channel, backModel tea.Model, base *BaseModel) *ChannelModel {
m := ChannelModel{lndService: service, ctx: context.Background(), channel: channel, base: base}
m := ChannelModel{lndService: service, ctx: context.Background(), channel: channel, base: base, help: help.New(), keys: Keymap}
m.styles = GetDefaultStyles()

m.base.pushView(&m)
return &m
}
Expand All @@ -43,9 +65,15 @@ func (m *ChannelModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
windowSizeMsg = msg
m.help.Width = msg.Width
v, h := m.styles.BorderedStyle.GetFrameSize()
m.initData(windowSizeMsg.Width-h, windowSizeMsg.Height-v)

case tea.KeyMsg:
switch {
case key.Matches(msg, Keymap.Help):
m.help.ShowAll = !m.help.ShowAll
}
}
return m, cmd
}
Expand Down Expand Up @@ -192,9 +220,12 @@ func (m ChannelModel) View() string {

htlcTableView := lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(s.Keyword("Pending HTLCs\n\n")+m.htlcTable.View()))

helpView := s.Base.Render(m.help.View(m.keys))

return lipgloss.JoinVertical(lipgloss.Left,
topView,
statsView,
channelBalanceView,
htlcTableView)
htlcTableView,
helpView)
}
25 changes: 20 additions & 5 deletions internal/tui/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"github.com/charmbracelet/bubbles/key"
)

type keymap struct {
Create key.Binding
type keyMap struct {
Close key.Binding
ForceClose key.Binding
Update key.Binding
Enter key.Binding
Rename key.Binding
Delete key.Binding
Expand All @@ -15,13 +17,26 @@ type keymap struct {
Right key.Binding
Tab key.Binding
ReverseTab key.Binding
Help key.Binding
}

// Keymap reusable key mappings shared across models
var Keymap = keymap{
Create: key.NewBinding(
var Keymap = keyMap{
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Close: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "create"),
key.WithHelp("c", "close"),
),
ForceClose: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "force close"),
),
Update: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("u", "update"),
),
Tab: key.NewBinding(
key.WithKeys("tab"),
Expand Down

0 comments on commit 32d6041

Please sign in to comment.