diff --git a/cmd/ls.go b/cmd/ls.go index b2e5007..1ad9b3b 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -20,6 +20,7 @@ var lsCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, _ []string) error { filterFlag, _ := cmd.Flags().GetString("f") oldUI, _ := cmd.Flags().GetBool("c") + refreshLastEditTime, _ := cmd.Flags().GetBool("r") data, err := db.GetAllRecords(db.DBName, StatusBucket) if err != nil { return err @@ -29,9 +30,9 @@ var lsCmd = &cobra.Command{ data = utils.FilterByStatuses(data, strings.Split(filterFlag, ",")) } if oldUI { - return ui.RenderTable(data) + return ui.RenderTable(data, refreshLastEditTime) } - return ui.RenderInteractiveTable(data) + return ui.RenderInteractiveTable(data, refreshLastEditTime) }, } @@ -39,4 +40,5 @@ func init() { rootCmd.AddCommand(lsCmd) lsCmd.Flags().String("f", "", "Filter projects by status. Usage : pman ls --f ") lsCmd.Flags().Bool("c", false, "list projects using the colorful table. Usage : pman ls --c") + lsCmd.Flags().Bool("r", false, "Refresh Last Edited time: pman ls --r") } diff --git a/pkg/ui/interactiveTable.go b/pkg/ui/interactiveTable.go index feb2eaa..ae79e3e 100644 --- a/pkg/ui/interactiveTable.go +++ b/pkg/ui/interactiveTable.go @@ -57,16 +57,55 @@ func (m tableModel) View() string { return baseStyle.Render(m.table.View()) + "\n" } -func RenderInteractiveTable(data map[string]string) error { +func RenderInteractiveTable(data map[string]string, refreshLastEditedTime bool) error { + var rows []table.Row + var lastEdited string + col := []table.Column{ {Title: "Status", Width: 20}, {Title: "Project", Width: 40}, {Title: "Last Edited", Width: 20}, } - var rows []table.Row + + if refreshLastEditedTime { + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } else { + rec, err := db.GetRecord(db.DBName, "lastRefreshTime", pkg.ConfigBucket) + if err != nil { // lastRefreshTime key does not exist in db + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + if utils.DayPassed(rec) { // lastEdited values are more than a day old. need to refresh them + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + } + for proj, status := range data { alias, err := db.GetRecord(db.DBName, proj, pkg.ProjectAliasBucket) - lastEdited := utils.GetLastModifiedTime(db.DBName, proj) + if refreshLastEditedTime { + lastEdited = utils.GetLastModifiedTime(db.DBName, proj) + rec := map[string]string{proj: lastEdited} + err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket) + if err != nil { + return err + } + } else { + lE, err := db.GetRecord(db.DBName, proj, pkg.LastUpdatedBucket) + if err != nil { + return err + } + lastEdited = lE + } if err == nil { pname := fmt.Sprintf("%s (%s)", proj, alias) row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited diff --git a/pkg/ui/statusTable.go b/pkg/ui/statusTable.go index 73bcf9c..2ae266a 100644 --- a/pkg/ui/statusTable.go +++ b/pkg/ui/statusTable.go @@ -14,13 +14,51 @@ import ( ) // RenderTable: renders the given data as a table -func RenderTable(data map[string]string) error { +func RenderTable(data map[string]string, refreshLastEditedTime bool) error { var tableData [][]string + var lastEdited string + + if refreshLastEditedTime { + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } else { + rec, err := db.GetRecord(db.DBName, "lastRefreshTime", pkg.ConfigBucket) + if err != nil { // lastRefreshTime key does not exist in db + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + if utils.DayPassed(rec) { // lastEdited values are more than a day old. need to refresh them + refreshLastEditedTime = true + err := utils.UpdateLastEditedTime() + if err != nil { + return err + } + } + } + for p, status := range data { alias, err := db.GetRecord(db.DBName, p, pkg.ProjectAliasBucket) - lastEdited := utils.GetLastModifiedTime(db.DBName, p) + if refreshLastEditedTime { + lastEdited = utils.GetLastModifiedTime(db.DBName, p) + rec := map[string]string{p: lastEdited} + err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket) + if err != nil { + return err + } + } else { + lE, err := db.GetRecord(db.DBName, p, pkg.LastUpdatedBucket) + if err != nil { + return err + } + lastEdited = lE + } if err == nil { - pname := fmt.Sprintf("%s (%s)", p, alias) + pname := fmt.Sprintf("%s (%s) nil error", p, alias) row := []string{utils.TitleCase(status), pname, lastEdited} // Status | projectName (alias) | lastEdited tableData = append(tableData, row) } else { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 4056523..94c86d2 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "strconv" "time" "github.com/charmbracelet/glamour" @@ -109,3 +110,23 @@ func ReadREADME(dbname, projectName string) ([]byte, error) { } return data, nil } + +func UpdateLastEditedTime() error { + r := fmt.Sprint(time.Now().Unix()) + rec := map[string]string{"lastRefreshTime": r} + err := db.WriteToDB(db.DBName, rec, pkg.ConfigBucket) + if err != nil { + return err + } + return nil +} + +func DayPassed(t string) bool { + oneDay := 86400 + now := time.Now().Unix() + recTime, _ := strconv.ParseInt(t, 10, 64) + if now-recTime > int64(oneDay) { + return true + } + return false +}