single table, with selection being updated correctly
This commit is contained in:
parent
b43be7554f
commit
3967be6516
102
main.go
102
main.go
|
@ -9,6 +9,7 @@ import (
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/charmbracelet/bubbles/table"
|
"github.com/charmbracelet/bubbles/table"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
"github.com/charmbracelet/bubbles/key"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -49,6 +50,7 @@ func Tasks() []Task {
|
||||||
var (
|
var (
|
||||||
tasks = Tasks()
|
tasks = Tasks()
|
||||||
columns = []table.Column{
|
columns = []table.Column{
|
||||||
|
{Title: "✅?", Width: 3},
|
||||||
{Title: "ID", Width: 4},
|
{Title: "ID", Width: 4},
|
||||||
{Title: "Project", Width: 10},
|
{Title: "Project", Width: 10},
|
||||||
{Title: "Tags", Width: 10},
|
{Title: "Tags", Width: 10},
|
||||||
|
@ -63,36 +65,65 @@ var (
|
||||||
|
|
||||||
type model struct {
|
type model struct {
|
||||||
all table.Model
|
all table.Model
|
||||||
active table.Model
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TasksToRows(tasks []Task) []table.Row {
|
func TasksToRows(tasks []Task) []table.Row {
|
||||||
rows := make([]table.Row, len(tasks))
|
rows := make([]table.Row, len(tasks))
|
||||||
for i := 0; i < len(tasks); i++ {
|
for i := 0; i < len(tasks); i++ {
|
||||||
t := tasks[i]
|
t := tasks[i]
|
||||||
rows[i] = []string{strconv.Itoa(t.Id), t.Project, strings.Join(t.Tags[:], " "), t.Description} // fmt.Sprintf("%f", t.Urgency)
|
rows[i] = []string{"❌", strconv.Itoa(t.Id), t.Project, strings.Join(t.Tags[:], " "), t.Description} // fmt.Sprintf("%f", t.Urgency)
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTable(WithTasks bool) table.Model {
|
func NewKeyMap() table.KeyMap {
|
||||||
var t table.Model
|
return table.KeyMap{
|
||||||
if WithTasks {
|
LineUp: key.NewBinding(
|
||||||
rows := TasksToRows(tasks)
|
key.WithKeys("up", "k"),
|
||||||
t = table.New(
|
key.WithHelp("↑/k", "up"),
|
||||||
table.WithColumns(columns),
|
),
|
||||||
table.WithRows(rows),
|
LineDown: key.NewBinding(
|
||||||
table.WithFocused(true),
|
key.WithKeys("down", "j"),
|
||||||
table.WithHeight(15),
|
key.WithHelp("↓/j", "down"),
|
||||||
)
|
),
|
||||||
} else {
|
PageUp: key.NewBinding(
|
||||||
t = table.New(
|
key.WithKeys("b", "pgup"),
|
||||||
table.WithColumns(columns),
|
key.WithHelp("b/pgup", "page up"),
|
||||||
table.WithRows(make([]table.Row, len(tasks))),
|
),
|
||||||
table.WithFocused(true),
|
PageDown: key.NewBinding(
|
||||||
table.WithHeight(15),
|
key.WithKeys("f", "pgdown"),
|
||||||
)
|
key.WithHelp("f/pgdn", "page down"),
|
||||||
|
),
|
||||||
|
HalfPageUp: key.NewBinding(
|
||||||
|
key.WithKeys("u", "ctrl+u"),
|
||||||
|
key.WithHelp("u", "½ page up"),
|
||||||
|
),
|
||||||
|
HalfPageDown: key.NewBinding(
|
||||||
|
key.WithKeys("d", "ctrl+d"),
|
||||||
|
key.WithHelp("d", "½ page down"),
|
||||||
|
),
|
||||||
|
GotoTop: key.NewBinding(
|
||||||
|
key.WithKeys("home", "g"),
|
||||||
|
key.WithHelp("g/home", "go to start"),
|
||||||
|
),
|
||||||
|
GotoBottom: key.NewBinding(
|
||||||
|
key.WithKeys("end", "G"),
|
||||||
|
key.WithHelp("G/end", "go to end"),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateTable() table.Model {
|
||||||
|
var t table.Model
|
||||||
|
rows := TasksToRows(tasks)
|
||||||
|
t = table.New(
|
||||||
|
table.WithColumns(columns),
|
||||||
|
table.WithRows(rows),
|
||||||
|
table.WithFocused(true),
|
||||||
|
table.WithHeight(15),
|
||||||
|
table.WithKeyMap(NewKeyMap()),
|
||||||
|
)
|
||||||
|
|
||||||
s := table.DefaultStyles()
|
s := table.DefaultStyles()
|
||||||
s.Header = s.Header.
|
s.Header = s.Header.
|
||||||
BorderStyle(lipgloss.NormalBorder()).
|
BorderStyle(lipgloss.NormalBorder()).
|
||||||
|
@ -107,23 +138,17 @@ func CreateTable(WithTasks bool) table.Model {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) SwitchRow() {
|
func (m model) SwitchRow() table.Model {
|
||||||
selectedRowIndex := m.all.Cursor()
|
selected := m.all.SelectedRow()
|
||||||
allRows := m.all.Rows()
|
if (selected[0] == "❌") {
|
||||||
allRows = append(allRows[:selectedRowIndex], allRows[selectedRowIndex+1:]...)
|
selected[0] = "✅"
|
||||||
|
} else {
|
||||||
selectedRow := m.all.SelectedRow()
|
selected[0] = "❌"
|
||||||
activeRows := m.active.Rows()
|
}
|
||||||
activeRows = append(activeRows, selectedRow)
|
// m.all.SetRows(allRows)
|
||||||
|
|
||||||
m.all.SetRows(allRows)
|
|
||||||
m.active.SetRows(activeRows)
|
|
||||||
|
|
||||||
m.all.UpdateViewport()
|
m.all.UpdateViewport()
|
||||||
m.active.UpdateViewport()
|
return m.all
|
||||||
// TODO broken
|
|
||||||
// m.all doesn't update until keypress (up down left right)
|
|
||||||
// m.active doesn't ever get updated
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd { return nil }
|
func (m model) Init() tea.Cmd { return nil }
|
||||||
|
@ -141,8 +166,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
}
|
}
|
||||||
case "q", "ctrl+c":
|
case "q", "ctrl+c":
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "enter", "space":
|
case " ":
|
||||||
m.SwitchRow()
|
m.all = m.SwitchRow()
|
||||||
|
return m, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.all, cmd = m.all.Update(msg)
|
m.all, cmd = m.all.Update(msg)
|
||||||
|
@ -150,11 +176,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) View() string {
|
func (m model) View() string {
|
||||||
return lipgloss.JoinVertical(lipgloss.Center, baseStyle.Render(m.all.View()), baseStyle.Render(m.active.View()))
|
return baseStyle.Render(m.all.View())
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
m := model{CreateTable(true), CreateTable(false)}
|
m := model{CreateTable()}
|
||||||
if _, err := tea.NewProgram(m).Run(); err != nil {
|
if _, err := tea.NewProgram(m).Run(); err != nil {
|
||||||
fmt.Println("Error running program:", err)
|
fmt.Println("Error running program:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
Loading…
Reference in a new issue