diff options
Diffstat (limited to 'internal/tui/components/core')
| -rw-r--r-- | internal/tui/components/core/dialog.go | 84 | ||||
| -rw-r--r-- | internal/tui/components/core/help.go | 22 |
2 files changed, 95 insertions, 11 deletions
diff --git a/internal/tui/components/core/dialog.go b/internal/tui/components/core/dialog.go new file mode 100644 index 000000000..954e26e87 --- /dev/null +++ b/internal/tui/components/core/dialog.go @@ -0,0 +1,84 @@ +package core + +import ( + "github.com/charmbracelet/bubbles/key" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/kujtimiihoxha/termai/internal/tui/layout" + "github.com/kujtimiihoxha/termai/internal/tui/util" +) + +type SizeableModel interface { + tea.Model + layout.Sizeable +} + +type DialogMsg struct { + Content SizeableModel +} + +type DialogCloseMsg struct{} + +type KeyBindings struct { + Return key.Binding +} + +var keys = KeyBindings{ + Return: key.NewBinding( + key.WithKeys("esc"), + key.WithHelp("esc", "close"), + ), +} + +type DialogCmp interface { + tea.Model + layout.Bindings +} + +type dialogCmp struct { + content SizeableModel +} + +func (d *dialogCmp) Init() tea.Cmd { + return nil +} + +func (d *dialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case DialogMsg: + d.content = msg.Content + case DialogCloseMsg: + d.content = nil + return d, nil + case tea.KeyMsg: + if key.Matches(msg, keys.Return) { + return d, util.CmdHandler(DialogCloseMsg{}) + } + } + if d.content != nil { + u, cmd := d.content.Update(msg) + d.content = u.(SizeableModel) + return d, cmd + } + return d, nil +} + +func (d *dialogCmp) BindingKeys() []key.Binding { + bindings := []key.Binding{keys.Return} + if d.content == nil { + return bindings + } + if c, ok := d.content.(layout.Bindings); ok { + return append(bindings, c.BindingKeys()...) + } + return bindings +} + +func (d *dialogCmp) View() string { + w, h := d.content.GetSize() + return lipgloss.NewStyle().Width(w).Height(h).Render(d.content.View()) +} + +func NewDialogCmp() DialogCmp { + return &dialogCmp{} +} diff --git a/internal/tui/components/core/help.go b/internal/tui/components/core/help.go index 9402d9362..4ef857c78 100644 --- a/internal/tui/components/core/help.go +++ b/internal/tui/components/core/help.go @@ -24,23 +24,23 @@ type helpCmp struct { bindings []key.Binding } -func (m *helpCmp) Init() tea.Cmd { +func (h *helpCmp) Init() tea.Cmd { return nil } -func (m *helpCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (h *helpCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: - m.width = msg.Width + h.width = msg.Width } - return m, nil + return h, nil } -func (m *helpCmp) View() string { +func (h *helpCmp) View() string { helpKeyStyle := styles.Bold.Foreground(styles.Rosewater).Margin(0, 1, 0, 0) helpDescStyle := styles.Regular.Foreground(styles.Flamingo) // Compile list of bindings to render - bindings := removeDuplicateBindings(m.bindings) + bindings := removeDuplicateBindings(h.bindings) // Enumerate through each group of bindings, populating a series of // pairs of columns, one for keys, one for descriptions var ( @@ -72,7 +72,7 @@ func (m *helpCmp) View() string { // check whether it exceeds the maximum width avail (the width of the // terminal, subtracting 2 for the borders). width += lipgloss.Width(pair) - if width > m.width-2 { + if width > h.width-2 { break } pairs = append(pairs, pair) @@ -80,7 +80,7 @@ func (m *helpCmp) View() string { // Join pairs of columns and enclose in a border content := lipgloss.JoinHorizontal(lipgloss.Top, pairs...) - return styles.DoubleBorder.Height(rows).PaddingLeft(1).Width(m.width - 2).Render(content) + return styles.DoubleBorder.Height(rows).PaddingLeft(1).Width(h.width - 2).Render(content) } func removeDuplicateBindings(bindings []key.Binding) []key.Binding { @@ -103,11 +103,11 @@ func removeDuplicateBindings(bindings []key.Binding) []key.Binding { return result } -func (m *helpCmp) SetBindings(bindings []key.Binding) { - m.bindings = bindings +func (h *helpCmp) SetBindings(bindings []key.Binding) { + h.bindings = bindings } -func (m helpCmp) Height() int { +func (h helpCmp) Height() int { return helpWidgetHeight } |
