36999cd825
Adds `nib tui` command and `make tui` target. Scrollable entity list with j/k navigation, enter for detail view, `a` to capture new entries using the existing parse grammar, and `d` to delete.
52 lines
891 B
Go
52 lines
891 B
Go
package tui
|
|
|
|
import (
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/lerko/nib/internal/db"
|
|
)
|
|
|
|
type entitiesLoadedMsg struct {
|
|
entities []*db.Entity
|
|
}
|
|
|
|
type entityCreatedMsg struct {
|
|
entity *db.Entity
|
|
}
|
|
|
|
type entityDeletedMsg struct {
|
|
id string
|
|
}
|
|
|
|
type errMsg struct {
|
|
err error
|
|
}
|
|
|
|
func loadEntities(store *db.Store, params db.ListParams) tea.Cmd {
|
|
return func() tea.Msg {
|
|
entities, err := store.List(params)
|
|
if err != nil {
|
|
return errMsg{err}
|
|
}
|
|
return entitiesLoadedMsg{entities}
|
|
}
|
|
}
|
|
|
|
func createEntity(store *db.Store, e *db.Entity) tea.Cmd {
|
|
return func() tea.Msg {
|
|
if err := store.Create(e); err != nil {
|
|
return errMsg{err}
|
|
}
|
|
return entityCreatedMsg{e}
|
|
}
|
|
}
|
|
|
|
func deleteEntity(store *db.Store, id string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
if _, err := store.SoftDelete(id); err != nil {
|
|
return errMsg{err}
|
|
}
|
|
return entityDeletedMsg{id}
|
|
}
|
|
}
|