single table, with selection being updated correctly

refactor
Anish Lakhwara 2024-03-03 00:02:23 -08:00
parent b43be7554f
commit 3967be6516
1 changed files with 64 additions and 38 deletions

102
main.go
View File

@ -9,6 +9,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/bubbles/key"
"os"
"strconv"
"strings"
@ -49,6 +50,7 @@ func Tasks() []Task {
var (
tasks = Tasks()
columns = []table.Column{
{Title: "✅?", Width: 3},
{Title: "ID", Width: 4},
{Title: "Project", Width: 10},
{Title: "Tags", Width: 10},
@ -63,36 +65,65 @@ var (
type model struct {
all table.Model
active table.Model
}
func TasksToRows(tasks []Task) []table.Row {
rows := make([]table.Row, len(tasks))
for i := 0; i < len(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
}
func CreateTable(WithTasks bool) table.Model {
var t table.Model
if WithTasks {
rows := TasksToRows(tasks)
t = table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(15),
)
} else {
t = table.New(
table.WithColumns(columns),
table.WithRows(make([]table.Row, len(tasks))),
table.WithFocused(true),
table.WithHeight(15),
)
func NewKeyMap() table.KeyMap {
return table.KeyMap{
LineUp: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "up"),
),
LineDown: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "down"),
),
PageUp: key.NewBinding(
key.WithKeys("b", "pgup"),
key.WithHelp("b/pgup", "page up"),
),
PageDown: key.NewBinding(
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.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
@ -107,23 +138,17 @@ func CreateTable(WithTasks bool) table.Model {
return t
}
func (m model) SwitchRow() {
selectedRowIndex := m.all.Cursor()
allRows := m.all.Rows()
allRows = append(allRows[:selectedRowIndex], allRows[selectedRowIndex+1:]...)
selectedRow := m.all.SelectedRow()
activeRows := m.active.Rows()
activeRows = append(activeRows, selectedRow)
m.all.SetRows(allRows)
m.active.SetRows(activeRows)
func (m model) SwitchRow() table.Model {
selected := m.all.SelectedRow()
if (selected[0] == "❌") {
selected[0] = "✅"
} else {
selected[0] = "❌"
}
// m.all.SetRows(allRows)
m.all.UpdateViewport()
m.active.UpdateViewport()
// TODO broken
// m.all doesn't update until keypress (up down left right)
// m.active doesn't ever get updated
return m.all
}
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":
return m, tea.Quit
case "enter", "space":
m.SwitchRow()
case " ":
m.all = m.SwitchRow()
return m, nil
}
}
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 {
return lipgloss.JoinVertical(lipgloss.Center, baseStyle.Render(m.all.View()), baseStyle.Render(m.active.View()))
return baseStyle.Render(m.all.View())
}
func main() {
m := model{CreateTable(true), CreateTable(false)}
m := model{CreateTable()}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)