Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pman ls and pman i both sort output by last edited time. #42

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"errors"

"github.com/spf13/cobra"

"github.com/theredditbandit/pman/pkg/utils"
)

const (
StatusBucket = "projects"
ProjectPathBucket = "projectPaths"
ProjectAliasBucket = "projectAliases"
ConfigBucket = "config"
version = "1.0"
version = "1.0.1"
)

var (
Expand All @@ -23,7 +21,7 @@ var (
var rootCmd = &cobra.Command{
Use: "pman",
Short: "A cli project manager",
Version: utils.GetVersion(),
Version: version,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
Expand Down
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)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define a function that format a time.Time into this

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup , that makes much more sense. so much easier to do than what I did too.

I'll fix this

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(tbl [][]string) [][]string { // cleanUp func removes the unix timestamp col from the tabledata
result := make([][]string, len(tbl))
for i, inner := range tbl {
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
22 changes: 6 additions & 16 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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is strange. You should returned a date, and let the caller transform it

}

// BeautifyMD: returns styled markdown
Expand Down Expand Up @@ -129,12 +128,3 @@ func DayPassed(t string) bool {
recTime, _ := strconv.ParseInt(t, 10, 64)
return now-recTime > int64(oneDay)
}

func GetVersion() string {
cmd := exec.Command("git", "describe", "--tags", "--always")
out, err := cmd.Output()
if err != nil {
return "unknown"
}
return strings.TrimSpace(string(out))
}
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
Loading