forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
107 lines (93 loc) · 2.17 KB
/
utils.go
File metadata and controls
107 lines (93 loc) · 2.17 KB
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
package utils
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"time"
"github.com/kballard/go-shellquote"
md "github.com/vilmibm/go-termd"
)
func OpenInBrowser(url string) error {
browser := os.Getenv("BROWSER")
if browser == "" {
browser = searchBrowserLauncher(runtime.GOOS)
} else {
browser = os.ExpandEnv(browser)
}
if browser == "" {
return errors.New("Please set $BROWSER to a web launcher")
}
browserArgs, err := shellquote.Split(browser)
if err != nil {
return err
}
endingArgs := append(browserArgs[1:], url)
browseCmd := exec.Command(browserArgs[0], endingArgs...)
return PrepareCmd(browseCmd).Run()
}
func searchBrowserLauncher(goos string) (browser string) {
switch goos {
case "darwin":
browser = "open"
case "windows":
browser = "cmd /c start"
default:
candidates := []string{"xdg-open", "cygstart", "x-www-browser", "firefox",
"opera", "mozilla", "netscape"}
for _, b := range candidates {
path, err := exec.LookPath(b)
if err == nil {
browser = path
break
}
}
}
return browser
}
func normalizeNewlines(d []byte) []byte {
d = bytes.Replace(d, []byte("\r\n"), []byte("\n"), -1)
d = bytes.Replace(d, []byte("\r"), []byte("\n"), -1)
return d
}
func RenderMarkdown(text string) string {
textB := []byte(text)
textB = normalizeNewlines(textB)
mdCompiler := md.Compiler{
Columns: 100,
SyntaxHighlighter: md.SyntaxTheme{
"keyword": md.Style{Color: "#9196ed"},
"comment": md.Style{
Color: "#c0c0c2",
},
"literal": md.Style{
Color: "#aaedf7",
},
"name": md.Style{
Color: "#fe8eb5",
},
},
}
return mdCompiler.Compile(string(textB))
}
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 FuzzyAgo(ago time.Duration) string {
if ago < time.Minute {
return "less than a minute ago"
}
if ago < time.Hour {
return fmt.Sprintf("about %s ago", Pluralize(int(ago.Minutes()), "minute"))
}
if ago < 24*time.Hour {
return fmt.Sprintf("about %s ago", Pluralize(int(ago.Hours()), "hour"))
}
return fmt.Sprintf("about %s ago", Pluralize(int(ago.Hours()/24), "day"))
}