aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--browserenv.go29
-rw-r--r--browserenv_unix.go6
-rw-r--r--browserenv_windows.go7
3 files changed, 35 insertions, 7 deletions
diff --git a/browserenv.go b/browserenv.go
index f799770..8401a32 100644
--- a/browserenv.go
+++ b/browserenv.go
@@ -12,13 +12,23 @@ import (
"github.com/pkg/browser"
)
+// Stderr is the browser command's standard error Writer. Defaults to
+// os.Stderr.
var Stderr io.Writer = os.Stderr
+
+// Stdout is the browser command's standard output Writer. Defaults to
+// os.Stdout.
var Stdout io.Writer = os.Stdout
+// percentS is a regular expression that matches "%s" not followed by an
+// alphabetic character.
var percentS = regexp.MustCompile("%s[[:^alpha:]]?")
+// commandSeparator is the delimiter used in between multiple commands
+// specified in the BROWSER environment variable.
const commandSeparator = ":"
+// OpenFile opens the file referenced by path in a browser.
func OpenFile(path string) error {
envCommand := envBrowserCommand()
if envCommand != "" {
@@ -35,6 +45,8 @@ func OpenFile(path string) error {
return browser.OpenFile(path)
}
+// OpenReader copies the contents of r to a temporary file and opens the
+// resulting file in a browser.
func OpenReader(r io.Reader) error {
envCommand := envBrowserCommand()
if envCommand != "" {
@@ -54,6 +66,7 @@ func OpenReader(r io.Reader) error {
return browser.OpenReader(r)
}
+// OpenURL opens url in a browser.
func OpenURL(url string) error {
envCommand := envBrowserCommand()
if envCommand != "" {
@@ -63,12 +76,14 @@ func OpenURL(url string) error {
return browser.OpenURL(url)
}
-// TODO
+// envBrowserCommand gets the value of the BROWSER environment variable.
func envBrowserCommand() string {
return os.Getenv("BROWSER")
}
-// TODO
+// runBrowserCommand opens url using commands, a colon-separated string of
+// shell commands. Each command is executed from left to right until one exits
+// with an exit code of 0.
func runBrowserCommand(commands, url string) error {
commandList := strings.Split(commands, commandSeparator)
@@ -87,7 +102,8 @@ func runBrowserCommand(commands, url string) error {
return err
}
-// TODO
+// browserCommand sets up an exec.Cmd to run command, attaching Stdout and
+// Stderr.
func browserCommand(command, url string) *exec.Cmd {
shellArgs := shell()
shell := shellArgs[0]
@@ -104,6 +120,8 @@ func browserCommand(command, url string) *exec.Cmd {
return cmd
}
+// fmtBrowserCommand formats command with url, producing a shell command that
+// can be executed with `/bin/sh -c COMMAND`.
func fmtBrowserCommand(command, url string) string {
url = escapeURL(url)
@@ -116,14 +134,19 @@ func fmtBrowserCommand(command, url string) string {
return command
}
+// browserCommandIncludesURL returns true if command includes a match for the
+// percentS pattern.
func browserCommandIncludesURL(command string) bool {
return percentS.MatchString(command)
}
+// fmtWithURL replaces all occurrences of "%s" in command with url.
func fmtWithURL(command, url string) string {
return strings.ReplaceAll(command, "%s", url)
}
+// escapeURL replaces single quotes ("'") in url with the corresponding URL
+// entity.
func escapeURL(url string) string {
return strings.ReplaceAll(url, "'", "%27")
}
diff --git a/browserenv_unix.go b/browserenv_unix.go
index 6d569e4..4565d59 100644
--- a/browserenv_unix.go
+++ b/browserenv_unix.go
@@ -7,7 +7,8 @@ import (
"os"
)
-// TODO
+// shell returns the current shell specified by the SHELL environment variable
+// along with a "-c" argument. If SHELL is undefined, `/bin/sh` is used.
func shell() (args []string) {
shell := os.Getenv("SHELL")
@@ -18,7 +19,8 @@ func shell() (args []string) {
return []string{shell, "-c"}
}
-// TODO
+// shellEscapeCommand formats a browser command with url, escaping url by
+// wrapping it in single quotes.
func shellEscapeCommand(browser, url string) string {
return fmt.Sprintf("%s '%s'", browser, url)
}
diff --git a/browserenv_windows.go b/browserenv_windows.go
index ac44d82..c257658 100644
--- a/browserenv_windows.go
+++ b/browserenv_windows.go
@@ -2,14 +2,17 @@ package browserenv
import "fmt"
+// shellArgs are the command and arguments needed to run a string containing
+// commands in a shell.
var shellArgs = []string{"cmd", "/c"}
-// TODO
+// shell returns a Windows `cmd` shell and "/c" argument.
func shell() (args []string) {
return shellArgs
}
-// TODO
+// shellEscapeCommand formats a browser command with url, ensuring url is
+// properly shell-escaped.
func shellEscapeCommand(browser, url string) string {
return fmt.Sprintf("%s %s", browser, url)
}