Skip to content

Commit

Permalink
implement ci suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
theredditbandit committed May 13, 2024
1 parent b8e023e commit 15348b4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
4 changes: 2 additions & 2 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var lsCmd = &cobra.Command{
`,
RunE: func(cmd *cobra.Command, _ []string) error {
filterFlag, _ := cmd.Flags().GetString("f")
oldUi, _ := cmd.Flags().GetBool("o")
oldUI, _ := cmd.Flags().GetBool("o")
data, err := db.GetAllRecords(StatusBucket)
if err != nil {
return err
Expand All @@ -27,7 +27,7 @@ var lsCmd = &cobra.Command{
fmt.Println("Filtering by status : ", filterFlag)
data = utils.FilterByStatus(data, filterFlag)
}
if oldUi {
if oldUI {
return ui.RenderTable(data)
}
return ui.RenderInteractiveTable(data)
Expand Down
4 changes: 2 additions & 2 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func GetRecord(key string, bucketName string) (string, error) {
}
v := bucket.Get([]byte(key))
if v == nil {
return fmt.Errorf("Key not found in db\n")
return fmt.Errorf("key not found in db")
}
rec = string(v)
return nil
Expand All @@ -123,7 +123,7 @@ func GetAllRecords(bucketName string) (map[string]string, error) {
err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(bucketName))
if bucket == nil {
return fmt.Errorf("Database not found. \nThis could be because no project dir has been initialized yet.")
return fmt.Errorf("database not found. \nThis could be because no project dir has been initialized yet")
}
err := bucket.ForEach(func(k, v []byte) error {
records[string(k)] = string(v)
Expand Down
21 changes: 8 additions & 13 deletions pkg/ui/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ import (

func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
d := list.NewDefaultDelegate()

d.UpdateFunc = func(msg tea.Msg, m *list.Model) tea.Cmd {
var title string

if i, ok := m.SelectedItem().(item); ok {
title = i.Title()
} else {
i, ok := m.SelectedItem().(item)
if !ok {
return nil
}

switch msg := msg.(type) {
case tea.KeyMsg:
title := i.Title()
if msg, ok := msg.(tea.KeyMsg); ok {
switch {
case key.Matches(msg, keys.choose):
p.LaunchRenderer(title)
return tea.Quit // TODO : change this to return to the list

err := p.LaunchRenderer(title)
if err == nil {
return tea.Quit // TODO : change this to return to the list
}
case key.Matches(msg, keys.remove):
index := m.Index()
m.RemoveItem(index)
Expand All @@ -36,7 +32,6 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
return m.NewStatusMessage(statusMessageStyle("Deleted " + title))
}
}

return nil
}

Expand Down
22 changes: 12 additions & 10 deletions pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ type tableModel struct {
table table.Model
}

func (m tableModel) Init() tea.Cmd { return nil }
func (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:
if msg, ok := msg.(tea.KeyMsg); ok {
switch msg.String() {
case "esc":
if m.table.Focused() {
Expand All @@ -39,7 +38,10 @@ func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
case "enter":
project := m.table.SelectedRow()[1]
p.LaunchRenderer(project)
err := p.LaunchRenderer(project)
if err != nil {
return m, tea.Quit
}
}
}
m.table, cmd = m.table.Update(msg)
Expand All @@ -57,15 +59,15 @@ func RenderInteractiveTable(data map[string]string) error {
{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)
for proj, status := range data {
alias, err := db.GetRecord(proj, pkg.ProjectAliasBucket)
lastEdited := utils.GetLastModifiedTime(proj)
if err == nil {
pname := fmt.Sprintf("%s (%s)", p, alias)
pname := fmt.Sprintf("%s (%s)", proj, 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
row := []string{utils.TitleCase(status), proj, lastEdited} // Status | projectName | lastEdited
rows = append(rows, row)
}
}
Expand Down Expand Up @@ -104,7 +106,7 @@ func RenderInteractiveTable(data map[string]string) error {

m := tableModel{t}
if _, err := tea.NewProgram(m).Run(); err != nil {
return fmt.Errorf("Error running program: %s", err)
return fmt.Errorf("error running program: %w", err)
}
return nil
}
20 changes: 10 additions & 10 deletions pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ import (

// RenderTable: renders the given data as a table
func RenderTable(data map[string]string) error {
var TableData [][]string
var tableData [][]string
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
TableData = append(TableData, row)
tableData = append(tableData, row)
} else {
row := []string{utils.TitleCase(status), p, lastEdited} // Status | projectName | lastEdited
TableData = append(TableData, row)
tableData = append(tableData, row)
}
}
if len(TableData) == 0 {
if len(tableData) == 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 fmt.Errorf("no database initialised")
return fmt.Errorf("no database initialized")
}
sort.Slice(TableData, func(i, j int) bool {
valI := TableData[i][1]
valJ := TableData[j][1]
sort.Slice(tableData, func(i, j int) bool {
valI := tableData[i][1]
valJ := tableData[j][1]
return valI < valJ
})
re := lipgloss.NewRenderer(os.Stdout)
Expand All @@ -62,12 +62,12 @@ func RenderTable(data map[string]string) error {
BorderStyle(re.NewStyle().Foreground(lipgloss.Color("238"))).
Headers(headers...).
Width(90).
Rows(TableData...).
Rows(tableData...).
StyleFunc(func(row, _ int) lipgloss.Style {
if row == 0 {
return headerStyle
}
color, ok := statusColors[fmt.Sprint(TableData[row-1][0])]
color, ok := statusColors[fmt.Sprint(tableData[row-1][0])]
if ok {
return baseStyle.Copy().Foreground(color)
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ var (
)

type item struct {
name string
status string
lastEdited string
name string
status string
}

func (i item) Title() string { return i.name }
Expand Down Expand Up @@ -92,7 +91,10 @@ func newModel() (model, error) {
}
})

formattedData, _ := d.([]item)
formattedData, ok := d.([]item)
if !ok {
return model{}, fmt.Errorf("something went wrong while creating a list of items")
}
items := make([]list.Item, len(data))
for i := 0; i < len(data); i++ {
items[i] = formattedData[i]
Expand Down

0 comments on commit 15348b4

Please sign in to comment.