-
Notifications
You must be signed in to change notification settings - Fork 0
/
external.go
62 lines (56 loc) · 1.46 KB
/
external.go
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
package skimmer
import (
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
)
const (
EnvHttpBrowser = "SKIM_HTTP_BROWSER"
EnvGopherBrowser = "SKIM_GOPHER_BROWSER"
EnvGemeniBrowser = "SKIM_GEMINI_BROWSER"
EnvFtpBrowser = "SKIM_FTP_BROWSER"
)
var clear map[string]func() //create a map for storing clear funcs
func SetupScreen(out io.Writer) {
clear = make(map[string]func())
for _, posix := range []string{"linux", "darwin"} {
clear[posix] = func() {
cmd := exec.Command("clear") //Linux example, its tested
cmd.Stdout = out
cmd.Run()
}
}
clear["windows"] = func() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = out
cmd.Run()
}
}
func ClearScreen() {
clrFn, ok := clear[runtime.GOOS] //runtime.GOOS -> linux, windows, darwin etc.
if ok { //if we defined a clear func for that platform:
clrFn() //we execute it
}
// else we do nothing because we don't know how ..
}
func OpenInBrowser(in io.Reader, out io.Writer, eout io.Writer, link string) error {
browser := ""
switch {
case strings.HasPrefix(link, "https:") || strings.HasPrefix(link, "http:"):
// Open in the strimmer recommended web browser
if browser = os.Getenv(EnvHttpBrowser); browser == "" {
return fmt.Errorf("%s is not set", EnvHttpBrowser)
}
}
if browser != "" {
cmd := exec.Command(browser, link)
cmd.Stdin = in
cmd.Stdout = out
cmd.Stderr = eout
return cmd.Run()
}
return fmt.Errorf("I do not know how to open %q", link)
}