Skip to content

Commit

Permalink
Clean up code and move helpers out of main
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Sep 6, 2021
1 parent d8f1b91 commit 46015c1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
64 changes: 17 additions & 47 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"strconv"
"strings"
"sync"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
"github.com/skratchdot/open-golang/open"
)

Expand Down Expand Up @@ -206,22 +204,26 @@ func parseAllProjects() {
}
}

func printVersion() {
if len(CommitSHA) > 7 {
CommitSHA = CommitSHA[:7]
}
if Version == "" {
Version = "(built from source)"
}

fmt.Printf("gitty %s", Version)
if len(CommitSHA) > 0 {
fmt.Printf(" (%s)", CommitSHA)
}

fmt.Println()
}

func main() {
flag.Parse()
if *version {
if len(CommitSHA) > 7 {
CommitSHA = CommitSHA[:7]
}
if Version == "" {
Version = "(built from source)"
}

fmt.Printf("gitty %s", Version)
if len(CommitSHA) > 0 {
fmt.Printf(" (%s)", CommitSHA)
}

fmt.Println()
printVersion()
os.Exit(0)
}

Expand All @@ -239,35 +241,3 @@ func main() {

parseRepository()
}

func ago(t time.Time) string {
s := humanize.Time(t)
if strings.Contains(s, "minute") || strings.Contains(s, "second") {
return "now"
}

s = strings.TrimSuffix(s, " ago")
s = strings.ReplaceAll(s, "years", "y")
s = strings.ReplaceAll(s, "year", "y")
s = strings.ReplaceAll(s, "months", "m")
s = strings.ReplaceAll(s, "month", "m")
s = strings.ReplaceAll(s, "weeks", "w")
s = strings.ReplaceAll(s, "week", "w")
s = strings.ReplaceAll(s, "days", "d")
s = strings.ReplaceAll(s, "day", "d")
s = strings.ReplaceAll(s, "hours", "h")
s = strings.ReplaceAll(s, "hour", "h")
s = strings.ReplaceAll(s, " ", "")

return s
}

func pluralize(count int, singular string, plural string) string {
if count == 0 {
return fmt.Sprintf("No %s", plural)
} else if count == 1 {
return fmt.Sprintf("1 %s", singular)
} else {
return fmt.Sprintf("%d %s", count, plural)
}
}
41 changes: 41 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"strings"
"time"

"github.com/dustin/go-humanize"
)

func ago(t time.Time) string {
s := humanize.Time(t)
if strings.Contains(s, "minute") || strings.Contains(s, "second") {
return "now"
}

s = strings.TrimSuffix(s, " ago")
s = strings.ReplaceAll(s, "years", "y")
s = strings.ReplaceAll(s, "year", "y")
s = strings.ReplaceAll(s, "months", "m")
s = strings.ReplaceAll(s, "month", "m")
s = strings.ReplaceAll(s, "weeks", "w")
s = strings.ReplaceAll(s, "week", "w")
s = strings.ReplaceAll(s, "days", "d")
s = strings.ReplaceAll(s, "day", "d")
s = strings.ReplaceAll(s, "hours", "h")
s = strings.ReplaceAll(s, "hour", "h")
s = strings.ReplaceAll(s, " ", "")

return s
}

func pluralize(count int, singular string, plural string) string {
if count == 0 {
return fmt.Sprintf("No %s", plural)
} else if count == 1 {
return fmt.Sprintf("1 %s", singular)
} else {
return fmt.Sprintf("%d %s", count, plural)
}
}

0 comments on commit 46015c1

Please sign in to comment.