From 778d807e852bc5802eb2dde019f78a65f04137d6 Mon Sep 17 00:00:00 2001 From: Naoya Yasuda <43776161+yanskun@users.noreply.github.com> Date: Fri, 10 May 2024 13:25:05 +0900 Subject: [PATCH] feat: add loading spinner --- go.mod | 1 + go.sum | 2 ++ main.go | 44 ++++++++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 6d493d5..76dcbcb 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8608bd5..fcd5f70 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index ac903ac..aeda26e 100644 --- a/main.go +++ b/main.go @@ -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" @@ -30,16 +31,13 @@ func main() { pflag.Parse() if helpFlag { - b := color.New(color.Bold) - b.Println("\nUSAGE") - fmt.Println(" gh langs [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() @@ -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) } @@ -74,6 +66,8 @@ 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 { @@ -81,6 +75,28 @@ func main() { } } +func printHelp() { + b := color.New(color.Bold) + b.Println("\nUSAGE") + fmt.Println(" gh langs [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