Skip to content

Commit

Permalink
Merge pull request #22 from theredditbandit/dev
Browse files Browse the repository at this point in the history
add interactive table to pman ls
  • Loading branch information
theredditbandit authored May 7, 2024
2 parents 7340b47 + e6455c9 commit 2d76c03
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 5 deletions.
9 changes: 7 additions & 2 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var lsCmd = &cobra.Command{
`,
RunE: func(cmd *cobra.Command, _ []string) error {
filterFlag, _ := cmd.Flags().GetString("f")
oldUi, _ := cmd.Flags().GetBool("o")
data, err := db.GetAllRecords(StatusBucket)
if err != nil {
return err
Expand All @@ -26,11 +27,15 @@ var lsCmd = &cobra.Command{
fmt.Println("Filtering by status : ", filterFlag)
data = utils.FilterByStatus(data, filterFlag)
}
return ui.RenderTable(data)
if oldUi {
return ui.RenderTable(data)
}
return ui.RenderInteractiveTable(data)
},
}

func init() {
rootCmd.AddCommand(lsCmd)
lsCmd.Flags().String("f", "", "Filter projects by status. Usage : pman ls -f <status>")
lsCmd.Flags().String("f", "", "Filter projects by status. Usage : pman ls --f <status>")
lsCmd.Flags().Bool("o", false, "list projects using the old ui. Usage : pman ls --o")
}
110 changes: 110 additions & 0 deletions pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package ui

import (
"fmt"
"sort"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/theredditbandit/pman/pkg"
"github.com/theredditbandit/pman/pkg/db"
p "github.com/theredditbandit/pman/pkg/ui/pager"
"github.com/theredditbandit/pman/pkg/utils"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))

type tableModel struct {
table table.Model
}

func (m tableModel) Init() tea.Cmd { return nil }

func (m tableModel) 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":
project := m.table.SelectedRow()[1]
p.LaunchRenderer(project)
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m tableModel) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}

func RenderInteractiveTable(data map[string]string) error {
col := []table.Column{
{Title: "Status", Width: 20},
{Title: "Project", Width: 40},
{Title: "Last Edited", Width: 20},
}
var rows []table.Row
for p, status := range data {
alias, err := db.GetRecord(p, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(p)
if err == nil {
pname := fmt.Sprintf("%s (%s)", p, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
rows = append(rows, row)
} else {
row := []string{utils.TitleCase(status), p, lastEdited} // Status | projectName | lastEdited
rows = append(rows, row)
}
}

if len(rows) == 0 {
fmt.Printf("No projects found in the database\n\n")
fmt.Printf("Add projects to the database using \n\n")
fmt.Println("pman init .")
fmt.Println("or")
fmt.Println("pman add <projectDir>")
return nil
}
sort.Slice(rows, func(i, j int) bool {
valI := rows[i][1]
valJ := rows[j][1]
return valI < valJ
})
t := table.New(
table.WithColumns(col),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(10),
table.WithWidth(90),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)

m := tableModel{t}
if _, err := tea.NewProgram(m).Run(); err != nil {
return fmt.Errorf("Error running program: %s", err)
}
return nil
}
4 changes: 1 addition & 3 deletions pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
// RenderTable: renders the given data as a table
func RenderTable(data map[string]string) error {
var TableData [][]string

for p, status := range data {
alias, err := db.GetRecord(p, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(p)
Expand All @@ -35,7 +34,7 @@ func RenderTable(data map[string]string) error {
fmt.Println("pman init .")
fmt.Println("or")
fmt.Println("pman add <projectDir>")
return nil
return fmt.Errorf("no database initialised")
}
sort.Slice(TableData, func(i, j int) bool {
valI := TableData[i][1]
Expand Down Expand Up @@ -72,7 +71,6 @@ func RenderTable(data map[string]string) error {
if ok {
return baseStyle.Copy().Foreground(color)
}

color = statusColors["Default"]
return baseStyle.Copy().Foreground(color)
})
Expand Down

0 comments on commit 2d76c03

Please sign in to comment.