Skip to content

Commit

Permalink
feat: pman ls and pman i both now show results sorted by lastedited time
Browse files Browse the repository at this point in the history
  • Loading branch information
theredditbandit committed May 22, 2024
1 parent 02a2210 commit 9f34e35
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
33 changes: 25 additions & 8 deletions pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/charmbracelet/bubbles/table"
Expand Down Expand Up @@ -60,6 +61,7 @@ func (m tableModel) View() string {
func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool) error {
var rows []table.Row
var lastEdited string
var timestamp int64

col := []table.Column{
{Title: "Status", Width: 20},
Expand Down Expand Up @@ -93,8 +95,8 @@ func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool)
for proj, status := range data {
alias, err := db.GetRecord(db.DBName, proj, pkg.ProjectAliasBucket)
if refreshLastEditedTime {
lastEdited = utils.GetLastModifiedTime(db.DBName, proj)
rec := map[string]string{proj: lastEdited}
lastEdited, timestamp = utils.GetLastModifiedTime(db.DBName, proj)
rec := map[string]string{proj: fmt.Sprintf("%s-%d", lastEdited, timestamp)}
err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket)
if err != nil {
return err
Expand All @@ -104,14 +106,20 @@ func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool)
if err != nil {
return err
}
lastEdited = lE
out := strings.Split(lE, "-")
lastEdited = out[0]
tstmp, err := strconv.ParseInt(out[1], 10, 64)
if err != nil {
return err
}
timestamp = tstmp
}
if err == nil {
pname := fmt.Sprintf("%s (%s)", proj, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
row := []string{utils.TitleCase(status), pname, lastEdited, fmt.Sprint(timestamp)} // Status | projectName (alias) | lastEdited | timestamp
rows = append(rows, row)
} else {
row := []string{utils.TitleCase(status), proj, lastEdited} // Status | projectName | lastEdited
row := []string{utils.TitleCase(status), proj, lastEdited, fmt.Sprint(timestamp)} // Status | projectName | lastEdited | timestamp
rows = append(rows, row)
}
}
Expand All @@ -125,10 +133,19 @@ func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool)
return nil
}
sort.Slice(rows, func(i, j int) bool {
valI := rows[i][1]
valJ := rows[j][1]
return valI < valJ
valI, _ := strconv.ParseInt(rows[i][3], 10, 64)
valJ, _ := strconv.ParseInt(rows[j][3], 10, 64)
return valI > valJ
})
cleanUp := func(r []table.Row) []table.Row {
result := make([]table.Row, len(r))
for i, inner := range r {
n := len(inner)
result[i] = inner[:n-1]
}
return result
}
rows = cleanUp(rows)
t := table.New(
table.WithColumns(col),
table.WithRows(rows),
Expand Down
37 changes: 29 additions & 8 deletions pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"sort"
"strconv"
"strings"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
Expand All @@ -17,6 +19,7 @@ import (
func RenderTable(data map[string]string, refreshLastEditedTime bool) error {
var tableData [][]string
var lastEdited string
var timestamp int64

if refreshLastEditedTime {
err := utils.UpdateLastEditedTime()
Expand Down Expand Up @@ -44,8 +47,8 @@ func RenderTable(data map[string]string, refreshLastEditedTime bool) error {
for p, status := range data {
alias, err := db.GetRecord(db.DBName, p, pkg.ProjectAliasBucket)
if refreshLastEditedTime {
lastEdited = utils.GetLastModifiedTime(db.DBName, p)
rec := map[string]string{p: lastEdited}
lastEdited, timestamp = utils.GetLastModifiedTime(db.DBName, p)
rec := map[string]string{p: fmt.Sprintf("%s-%d", lastEdited, timestamp)}
err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket)
if err != nil {
return err
Expand All @@ -55,14 +58,20 @@ func RenderTable(data map[string]string, refreshLastEditedTime bool) error {
if err != nil {
return err
}
lastEdited = lE
out := strings.Split(lE, "-")
lastEdited = out[0]
tstmp, err := strconv.ParseInt(out[1], 10, 64)
if err != nil {
return err
}
timestamp = tstmp
}
if err == nil {
pname := fmt.Sprintf("%s (%s) nil error", p, alias)
row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited
row := []string{utils.TitleCase(status), pname, lastEdited, fmt.Sprint(timestamp)} // Status | projectName (alias) | lastEdited | timestamp
tableData = append(tableData, row)
} else {
row := []string{utils.TitleCase(status), p, lastEdited} // Status | projectName | lastEdited
row := []string{utils.TitleCase(status), p, lastEdited, fmt.Sprint(timestamp)} // Status | projectName | lastEdited | timestamp
tableData = append(tableData, row)
}
}
Expand All @@ -75,10 +84,22 @@ func RenderTable(data map[string]string, refreshLastEditedTime bool) error {
return fmt.Errorf("no database initialized")
}
sort.Slice(tableData, func(i, j int) bool {
valI := tableData[i][1]
valJ := tableData[j][1]
return valI < valJ
valI, _ := strconv.ParseInt(tableData[i][3], 10, 64)
valJ, _ := strconv.ParseInt(tableData[j][3], 10, 64)
return valI > valJ
})

cleanUp := func(table [][]string) [][]string { // cleanUp func removes the unix timestamp col from the tabledata

Check failure on line 92 in pkg/ui/statusTable.go

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

import-shadowing: The name 'table' shadows an import name (revive)
result := make([][]string, len(table))
for i, inner := range table {
n := len(inner)
result[i] = inner[:n-1]
}
return result
}

tableData = cleanUp(tableData)

re := lipgloss.NewRenderer(os.Stdout)
baseStyle := re.NewStyle().Padding(0, 1)
headerStyle := baseStyle.Copy().Foreground(lipgloss.Color("252")).Bold(true)
Expand Down
13 changes: 6 additions & 7 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/charmbracelet/glamour"
Expand Down Expand Up @@ -43,12 +41,13 @@ func FilterByStatuses(data map[string]string, status []string) map[string]string
return filteredData
}

func GetLastModifiedTime(dbname, pname string) string {
// GetLastModifiedTime returns the last modified time and the unix timestamp as well that is used to sort the projects later
func GetLastModifiedTime(dbname, pname string) (string, int64) {
var lastModTime time.Time
today := time.Now()
pPath, err := db.GetRecord(dbname, pname, pkg.ProjectPaths)
if err != nil {
return "Something went wrong"
return "Something went wrong", 0
}
_ = filepath.Walk(pPath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -62,11 +61,11 @@ func GetLastModifiedTime(dbname, pname string) string {

switch fmt.Sprint(lastModTime.Date()) {
case fmt.Sprint(today.Date()):
return fmt.Sprintf("Today %s", lastModTime.Format("15:04"))
return fmt.Sprintf("Today %s", lastModTime.Format("15:04")), int64(lastModTime.Unix())
case fmt.Sprint(today.AddDate(0, 0, -1).Date()):
return fmt.Sprintf("Yesterday %s", lastModTime.Format("17:00"))
return fmt.Sprintf("Yesterday %s", lastModTime.Format("17:00")), int64(lastModTime.Unix())
}
return fmt.Sprint(lastModTime.Format("02 Jan 06 15:04"))
return fmt.Sprint(lastModTime.Format("02 Jan 06 15:04")), int64(lastModTime.Unix())
}

// BeautifyMD: returns styled markdown
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func Test_GetLastModifiedTime(t *testing.T) {
err = db.WriteToDB(dbname, map[string]string{projectName: projectPath}, pkg.ProjectPaths)
require.NoError(t, err)

actual := utils.GetLastModifiedTime(dbname, projectName)
actual, _ := utils.GetLastModifiedTime(dbname, projectName)

assert.NotEqual(t, expectedMsg, actual)
assert.Contains(t, actual, "Today")
Expand All @@ -248,7 +248,7 @@ func Test_GetLastModifiedTime(t *testing.T) {
err = db.WriteToDB(dbname, map[string]string{projectName: projectPath}, pkg.ProjectPaths)
require.NoError(t, err)

actual := utils.GetLastModifiedTime(dbname, projectName)
actual, _ := utils.GetLastModifiedTime(dbname, projectName)

assert.NotEqual(t, expectedMsg, actual)
assert.Contains(t, actual, "Yesterday")
Expand All @@ -272,15 +272,15 @@ func Test_GetLastModifiedTime(t *testing.T) {
err = db.WriteToDB(dbname, map[string]string{projectName: projectPath}, pkg.ProjectPaths)
require.NoError(t, err)

actual := utils.GetLastModifiedTime(dbname, projectName)
actual, _ := utils.GetLastModifiedTime(dbname, projectName)

assert.NotEqual(t, expectedMsg, actual)
assert.NotEmpty(t, actual)
})
t.Run("Test GetLastModifiedTime with invalid project", func(t *testing.T) {
projectPath := "./invalid_project"

actual := utils.GetLastModifiedTime(dbname, projectPath)
actual, _ := utils.GetLastModifiedTime(dbname, projectPath)

assert.Equal(t, expectedMsg, actual)
})
Expand Down

0 comments on commit 9f34e35

Please sign in to comment.