73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/briandowns/spinner"
|
|
"github.com/charmbracelet/glamour"
|
|
"github.com/cli/cli/pkg/browser"
|
|
)
|
|
|
|
// OpenInBrowser opens the url in a web browser based on OS and $BROWSER environment variable
|
|
func OpenInBrowser(url string) error {
|
|
browseCmd, err := browser.Command(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return PrepareCmd(browseCmd).Run()
|
|
}
|
|
|
|
func RenderMarkdown(text string) (string, error) {
|
|
return glamour.Render(text, "dark")
|
|
}
|
|
|
|
func Pluralize(num int, thing string) string {
|
|
if num == 1 {
|
|
return fmt.Sprintf("%d %s", num, thing)
|
|
} else {
|
|
return fmt.Sprintf("%d %ss", num, thing)
|
|
}
|
|
}
|
|
|
|
func fmtDuration(amount int, unit string) string {
|
|
return fmt.Sprintf("about %s ago", Pluralize(amount, unit))
|
|
}
|
|
|
|
func FuzzyAgo(ago time.Duration) string {
|
|
if ago < time.Minute {
|
|
return "less than a minute ago"
|
|
}
|
|
if ago < time.Hour {
|
|
return fmtDuration(int(ago.Minutes()), "minute")
|
|
}
|
|
if ago < 24*time.Hour {
|
|
return fmtDuration(int(ago.Hours()), "hour")
|
|
}
|
|
if ago < 30*24*time.Hour {
|
|
return fmtDuration(int(ago.Hours())/24, "day")
|
|
}
|
|
if ago < 365*24*time.Hour {
|
|
return fmtDuration(int(ago.Hours())/24/30, "month")
|
|
}
|
|
|
|
return fmtDuration(int(ago.Hours()/24/365), "year")
|
|
}
|
|
|
|
func Spinner() *spinner.Spinner {
|
|
return spinner.New(spinner.CharSets[11], 400*time.Millisecond)
|
|
}
|
|
|
|
// ColorFuncForState returns a color function for a PR/Issue state
|
|
func ColorFuncForState(state string) func(string) string {
|
|
switch state {
|
|
case "OPEN":
|
|
return Green
|
|
case "CLOSED":
|
|
return Red
|
|
case "MERGED":
|
|
return Magenta
|
|
default:
|
|
return nil
|
|
}
|
|
}
|