1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
package utils
import (
"fmt"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/charmbracelet/glamour"
"gitlab.com/gitlab-org/cli/internal/run"
"gitlab.com/gitlab-org/cli/pkg/browser"
)
type MarkdownRenderOpts []glamour.TermRendererOption
// OpenInBrowser opens the url in a web browser based on OS and $BROWSER environment variable
func OpenInBrowser(url, browserType string) error {
browseCmd, err := browser.Command(url, browserType)
if err != nil {
return err
}
return run.PrepareCmd(browseCmd).Run()
}
func SanitizePathName(path string) string {
if !strings.HasPrefix(path, "/") {
// Prefix the path with "/" ensures that filepath.Clean removes all `/..`
// See rule 4 of filepath.Clean for more information: https://pkg.go.dev/path/filepath#Clean
path = "/" + path
}
return filepath.Clean(path)
}
func RenderMarkdown(text, glamourStyle string) (string, error) {
opts := MarkdownRenderOpts{
glamour.WithStylePath(getStyle(glamourStyle)),
}
return renderMarkdown(text, opts)
}
func RenderMarkdownWithoutIndentations(text, glamourStyle string) (string, error) {
opts := MarkdownRenderOpts{
glamour.WithStylePath(getStyle(glamourStyle)),
markdownWithoutIndentation(),
}
return renderMarkdown(text, opts)
}
func renderMarkdown(text string, opts MarkdownRenderOpts) (string, error) {
// Glamour rendering preserves carriage return characters in code blocks, but
// we need to ensure that no such characters are present in the output.
text = strings.ReplaceAll(text, "\r\n", "\n")
tr, err := glamour.NewTermRenderer(opts...)
if err != nil {
return "", err
}
return tr.Render(text)
}
func markdownWithoutIndentation() glamour.TermRendererOption {
overrides := []byte(`
{
"document": {
"margin": 0
},
"code_block": {
"margin": 0
}
}`)
return glamour.WithStylesFromJSONBytes(overrides)
}
func getStyle(glamourStyle string) string {
if glamourStyle == "" || glamourStyle == "none" {
return "notty"
}
return glamourStyle
}
func Pluralize(num int, thing string) string {
if num == 1 {
return fmt.Sprintf("%d %s", num, thing)
}
return fmt.Sprintf("%d %ss", num, thing)
}
func fmtDuration(amount int, unit string) string {
return fmt.Sprintf("about %s ago", Pluralize(amount, unit))
}
func PrettyTimeAgo(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 TimeToPrettyTimeAgo(d time.Time) string {
now := time.Now()
ago := now.Sub(d)
return PrettyTimeAgo(ago)
}
func FmtDuration(d time.Duration) string {
d = d.Round(time.Second)
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return fmt.Sprintf("%02dm %02ds", m, s)
}
func Humanize(s string) string {
// Replaces - and _ with spaces.
replace := "_-"
h := func(r rune) rune {
if strings.ContainsRune(replace, r) {
return ' '
}
return r
}
return strings.Map(h, s)
}
func DisplayURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
return urlStr
}
return u.Hostname() + u.Path
}
// PresentInStringSlice take a Hay (Slice of Strings) and a Needle (string)
// and returns true based on whether or not the Needle is present in the hay.
func PresentInStringSlice(hay []string, needle string) bool {
for x := range hay {
if hay[x] == needle {
return true
}
}
return false
}
// PresentInIntSlice take a Hay (Slice of Ints) and a Needle (int)
// and returns true based on whether or not the Needle is present in the hay.
func PresentInIntSlice(hay []int, needle int) bool {
for x := range hay {
if hay[x] == needle {
return true
}
}
return false
}
// CommonElementsInStringSlice takes 2 Slices of Strings and returns a Third Slice
// that is the common elements between the first 2 Slices.
func CommonElementsInStringSlice(s1 []string, s2 []string) (arr []string) {
hash := make(map[string]bool)
for x := range s1 {
hash[s1[x]] = true
}
for i := range s2 {
if hash[s2[i]] {
arr = append(arr, s2[i])
}
}
return arr
}
// isValidUrl tests a string to determine if it is a well-structured url or not.
func IsValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
func ByteToHumanReadableFormat(b int) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := unit, 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
// Map transfers the elements of its first argument using the result of the second fn(e)
func Map[T1, T2 any](elems []T1, fn func(T1) T2) []T2 {
r := make([]T2, len(elems))
for i, v := range elems {
r[i] = fn(v)
}
return r
}
// Ptr takes any value and returns a pointer to that value
func Ptr[T any](v T) *T {
return &v
}
|