Skip to content

Commit

Permalink
separate GetLastModtime into two functions
Browse files Browse the repository at this point in the history
  • Loading branch information
theredditbandit committed May 22, 2024
1 parent e080fda commit 36d4e2f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pkg/ui/interactiveTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +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, timestamp = utils.GetLastModifiedTime(db.DBName, proj)
t := utils.GetLastModifiedTime(db.DBName, proj)
lastEdited, timestamp = utils.ParseTime(t)
rec := map[string]string{proj: fmt.Sprintf("%s-%d", lastEdited, timestamp)}
err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ui/statusTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +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, timestamp = utils.GetLastModifiedTime(db.DBName, p)
t := utils.GetLastModifiedTime(db.DBName, p)
lastEdited, timestamp = utils.ParseTime(t)
rec := map[string]string{p: fmt.Sprintf("%s-%d", lastEdited, timestamp)}
err := db.WriteToDB(db.DBName, rec, pkg.LastUpdatedBucket)
if err != nil {
Expand Down
33 changes: 21 additions & 12 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ func FilterByStatuses(data map[string]string, status []string) map[string]string
return filteredData
}

// 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) {
// GetLastModifiedTime returns the last modified time
func GetLastModifiedTime(dbname, pname string) string {
var lastModTime time.Time
today := time.Now()
pPath, err := db.GetRecord(dbname, pname, pkg.ProjectPaths)
if err != nil {
return "Something went wrong", 0
return "Something went wrong"
}
_ = filepath.Walk(pPath, func(_ string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -58,14 +57,7 @@ func GetLastModifiedTime(dbname, pname string) (string, int64) {
}
return nil
})

switch fmt.Sprint(lastModTime.Date()) {
case fmt.Sprint(today.Date()):
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")), int64(lastModTime.Unix())
}
return fmt.Sprint(lastModTime.Format("02 Jan 06 15:04")), int64(lastModTime.Unix())
return fmt.Sprint(lastModTime.Format("02 Jan 06 15:04"))
}

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

func ParseTime(tstr string) (string, int64) {
layout := "02 Jan 06 15:04"
p, err := time.Parse(layout, tstr)
timeStamp := p.Unix()
if err != nil {
return "unnkown", 0
}
today := time.Now()
switch fmt.Sprint(p.Date()) {
case fmt.Sprint(today.Date()):
return fmt.Sprintf("Today %s", p.Format("15:04")), timeStamp
case fmt.Sprint(today.AddDate(0, 0, -1).Date()):
return fmt.Sprintf("Yesterday %s", p.Format("17:00")), timeStamp
}
return tstr, timeStamp
}

0 comments on commit 36d4e2f

Please sign in to comment.