cleaned up the source file and got both tables to show

refactor
Anish Lakhwara 2024-03-02 01:44:48 -08:00
parent 9f7105f6b8
commit b43be7554f
1 changed files with 87 additions and 52 deletions

139
main.go
View File

@ -40,73 +40,59 @@ func Tasks() []Task {
if err != nil {
log.Fatal(err)
}
return tasks
}
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
type model struct {
table table.Model
}
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":
return m, tea.Batch(
tea.Printf("Let's go to %s!", m.table.SelectedRow()[1]),
)
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}
func main() {
tasks := Tasks()
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].Urgency > tasks[j].Urgency
})
return tasks
}
columns := []table.Column{
var (
tasks = Tasks()
columns = []table.Column{
{Title: "ID", Width: 4},
{Title: "Project", Width: 10},
{Title: "Tags", Width: 10},
{Title: "Description", Width: 50},
{Title: "Description", Width: 20},
// {Title: "Urgency", Width: 10},
}
baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
)
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)
}
return rows
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(20),
)
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),
)
}
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
@ -118,8 +104,57 @@ func main() {
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)
return t
}
m := model{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)
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
}
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.all.Focused() {
m.all.Blur()
} else {
m.all.Focus()
}
case "q", "ctrl+c":
return m, tea.Quit
case "enter", "space":
m.SwitchRow()
}
}
m.all, cmd = m.all.Update(msg)
return m, cmd
}
func (m model) View() string {
return lipgloss.JoinVertical(lipgloss.Center, baseStyle.Render(m.all.View()), baseStyle.Render(m.active.View()))
}
func main() {
m := model{CreateTable(true), CreateTable(false)}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)