From 85df0594be312307ea33086583f590283f828c09 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 20 Dec 2020 17:15:11 +0100 Subject: Support multiple colon-separated commands in `BROWSER` After reading the `urlview` man page, I learned that it supports multiple commands in the `BROWSER` environment variable separated by colons. Read over the source to see what `urlview` does: https://github.com/sigpipe/urlview/blob/08767aa863cd27d1755ba0aff65b8cc1a0c1446a/urlview.c#L617-L630 It tries each command from left to right until one of them exits with a `0` exit code. Use the same logic here. --- browserenv.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'browserenv.go') diff --git a/browserenv.go b/browserenv.go index f5a51ca..4edb032 100644 --- a/browserenv.go +++ b/browserenv.go @@ -17,6 +17,8 @@ var Stdout io.Writer = os.Stdout var percentS = regexp.MustCompile("%s[[:^alpha:]]?") +const commandSeparator = ":" + func OpenFile(path string) error { envCommand := envBrowserCommand() if envCommand != "" { @@ -67,8 +69,22 @@ func envBrowserCommand() string { } // TODO -func runBrowserCommand(command, url string) error { - return browserCommand(command, url).Run() +func runBrowserCommand(commands, url string) error { + commandList := strings.Split(commands, commandSeparator) + + var err error + for _, command := range commandList { + cmd := browserCommand(command, url) + + // Keep running commands from left to right until one of them exits + // successfully. + err = cmd.Run() + if err == nil || cmd.ProcessState.ExitCode() == 0 { + return err + } + } + + return err } // TODO -- cgit v1.2.3