pee/ui/table/main.go

120 lines
2.4 KiB
Go
Raw Normal View History

2023-10-22 01:12:58 +05:30
package table
import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
)
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
type keyMap struct {
Quit key.Binding
Edit key.Binding
Open key.Binding
}
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Quit, k.Edit, k.Open}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Quit, k.Edit, k.Open}, // first column
}
}
var keys = keyMap{
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q", "quit"),
),
Edit: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "edit selected project's configuration"),
),
Open: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "open selected project"),
),
}
type model struct {
table table.Model
action string
help help.Model
keys keyMap
}
func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
if m.table.Focused() {
m.table.Blur()
} else {
m.table.Focus()
}
case "q", "ctrl+c":
return m, tea.Quit
case "enter":
m.action = "open"
return m, tea.Quit
case "e":
m.action = "edit"
return m, tea.Quit
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
func (m model) View() string {
helpView := m.help.View(m.keys)
return baseStyle.Render(m.table.View()) + "\n" + helpView + "\n\n"
}
func Table(columns []table.Column, rows []table.Row) (table.Row, string) {
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(7),
)
s := table.DefaultStyles()
2023-10-26 19:50:15 +05:30
// rounded borders
2023-10-22 01:12:58 +05:30
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
2023-10-26 19:50:15 +05:30
2023-10-22 01:12:58 +05:30
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
2023-10-26 19:50:15 +05:30
Background(lipgloss.Color("240")).
2023-10-22 01:12:58 +05:30
Bold(false)
2023-10-26 19:50:15 +05:30
2023-10-22 01:12:58 +05:30
t.SetStyles(s)
m := model{t, "", help.New(), keys}
newModel, err := tea.NewProgram(m).Run()
if err != nil {
log.Fatal(err)
}
m = newModel.(model)
selected := rows[m.table.Cursor()]
return selected, m.action
}