|
| 1 | +package ui |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/charmbracelet/bubbles/table" |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | + |
| 11 | + "github.com/theredditbandit/pman/pkg" |
| 12 | + "github.com/theredditbandit/pman/pkg/db" |
| 13 | + p "github.com/theredditbandit/pman/pkg/ui/pager" |
| 14 | + "github.com/theredditbandit/pman/pkg/utils" |
| 15 | +) |
| 16 | + |
| 17 | +var baseStyle = lipgloss.NewStyle(). |
| 18 | + BorderStyle(lipgloss.NormalBorder()). |
| 19 | + BorderForeground(lipgloss.Color("240")) |
| 20 | + |
| 21 | +type tableModel struct { |
| 22 | + table table.Model |
| 23 | +} |
| 24 | + |
| 25 | +func (m tableModel) Init() tea.Cmd { return nil } |
| 26 | + |
| 27 | +func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 28 | + var cmd tea.Cmd |
| 29 | + switch msg := msg.(type) { |
| 30 | + case tea.KeyMsg: |
| 31 | + switch msg.String() { |
| 32 | + case "esc": |
| 33 | + if m.table.Focused() { |
| 34 | + m.table.Blur() |
| 35 | + } else { |
| 36 | + m.table.Focus() |
| 37 | + } |
| 38 | + case "q", "ctrl+c": |
| 39 | + return m, tea.Quit |
| 40 | + case "enter": |
| 41 | + project := m.table.SelectedRow()[1] |
| 42 | + p.LaunchRenderer(project) |
| 43 | + } |
| 44 | + } |
| 45 | + m.table, cmd = m.table.Update(msg) |
| 46 | + return m, cmd |
| 47 | +} |
| 48 | + |
| 49 | +func (m tableModel) View() string { |
| 50 | + return baseStyle.Render(m.table.View()) + "\n" |
| 51 | +} |
| 52 | + |
| 53 | +func RenderInteractiveTable(data map[string]string) error { |
| 54 | + col := []table.Column{ |
| 55 | + {Title: "Status", Width: 20}, |
| 56 | + {Title: "Project", Width: 40}, |
| 57 | + {Title: "Last Edited", Width: 20}, |
| 58 | + } |
| 59 | + var rows []table.Row |
| 60 | + for p, status := range data { |
| 61 | + alias, err := db.GetRecord(p, pkg.ProjectAliasBucket) |
| 62 | + lastEdited := utils.GetLastModifiedTime(p) |
| 63 | + if err == nil { |
| 64 | + pname := fmt.Sprintf("%s (%s)", p, alias) |
| 65 | + row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited |
| 66 | + rows = append(rows, row) |
| 67 | + } else { |
| 68 | + row := []string{utils.TitleCase(status), p, lastEdited} // Status | projectName | lastEdited |
| 69 | + rows = append(rows, row) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if len(rows) == 0 { |
| 74 | + fmt.Printf("No projects found in the database\n\n") |
| 75 | + fmt.Printf("Add projects to the database using \n\n") |
| 76 | + fmt.Println("pman init .") |
| 77 | + fmt.Println("or") |
| 78 | + fmt.Println("pman add <projectDir>") |
| 79 | + return nil |
| 80 | + } |
| 81 | + sort.Slice(rows, func(i, j int) bool { |
| 82 | + valI := rows[i][1] |
| 83 | + valJ := rows[j][1] |
| 84 | + return valI < valJ |
| 85 | + }) |
| 86 | + t := table.New( |
| 87 | + table.WithColumns(col), |
| 88 | + table.WithRows(rows), |
| 89 | + table.WithFocused(true), |
| 90 | + table.WithHeight(10), |
| 91 | + table.WithWidth(90), |
| 92 | + ) |
| 93 | + s := table.DefaultStyles() |
| 94 | + s.Header = s.Header. |
| 95 | + BorderStyle(lipgloss.NormalBorder()). |
| 96 | + BorderForeground(lipgloss.Color("240")). |
| 97 | + BorderBottom(true). |
| 98 | + Bold(false) |
| 99 | + s.Selected = s.Selected. |
| 100 | + Foreground(lipgloss.Color("229")). |
| 101 | + Background(lipgloss.Color("57")). |
| 102 | + Bold(false) |
| 103 | + t.SetStyles(s) |
| 104 | + |
| 105 | + m := tableModel{t} |
| 106 | + if _, err := tea.NewProgram(m).Run(); err != nil { |
| 107 | + return fmt.Errorf("Error running program: %s", err) |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
0 commit comments