Skip to content

Commit

Permalink
feat: add loading spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
yanskun committed May 10, 2024
1 parent 71556e9 commit 778d807
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/briandowns/spinner v1.23.0 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
github.com/go-openapi/errors v0.22.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3d
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
github.com/cli/go-gh/v2 v2.9.0 h1:D3lTjEneMYl54M+WjZ+kRPrR5CEJ5BHS05isBPOV3LI=
github.com/cli/go-gh/v2 v2.9.0/go.mod h1:MeRoKzXff3ygHu7zP+NVTT+imcHW6p3tpuxHAzRM2xE=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
Expand Down
44 changes: 30 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"time"

"github.com/briandowns/spinner"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/fatih/color"
"github.com/google/go-github/v61/github"
Expand All @@ -30,16 +31,13 @@ func main() {
pflag.Parse()

if helpFlag {
b := color.New(color.Bold)
b.Println("\nUSAGE")
fmt.Println(" gh langs <command> [options]\n")
b.Println("COMMANDS")
fmt.Println(" account: Get languages used by a GitHub user or organization\n")
b.Println("OPTIONS")
pflag.Usage()
printHelp()
return
}

s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Start()

account := pflag.Arg(0)
if account == "" {
account, _ = getGitHubUsername()
Expand All @@ -57,13 +55,7 @@ func main() {

var filter time.Time
if filterVal != 0.0 {
totalDays := int(filterVal * 365)
years := -totalDays / 365
remainingDays := totalDays % 365
months := -remainingDays / 30
days := -remainingDays % 30
filter = time.Now().AddDate(years, months, days)

filter = computeFilter(filterVal)
repos = filterRepositories(repos, filter)
}

Expand All @@ -74,13 +66,37 @@ func main() {

languages := sumLanguages(results)

s.Stop()

printTable(languages)
fmt.Printf("https:github.com/%s has %d repositories\n", account, len(repos))
if filterVal != 0.0 {
fmt.Printf("Last updated after %s\n", filter.Format("2006-01-02"))
}
}

func printHelp() {
b := color.New(color.Bold)
b.Println("\nUSAGE")
fmt.Println(" gh langs <command> [options]\n")
b.Println("COMMANDS")
fmt.Println(" account: Get languages used by a GitHub user or organization\n")
b.Println("OPTIONS")
pflag.Usage()
return
}

func computeFilter(filterVal float64) time.Time {
var filter time.Time
totalDays := int(filterVal * 365)
years := -totalDays / 365
remainingDays := totalDays % 365
months := -remainingDays / 30
days := -remainingDays % 30
filter = time.Now().AddDate(years, months, days)
return filter
}

func getGitHubUsername() (string, error) {
cmd := exec.Command("gh", "api", "user", "--jq", ".login")
var out bytes.Buffer
Expand Down

0 comments on commit 778d807

Please sign in to comment.