diff options
| author | Teddy Wing | 2020-12-19 23:57:50 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2020-12-19 23:59:49 +0100 | 
| commit | cbff612a593c44cc452070e7c50e7b8f858c763b (patch) | |
| tree | b282df6239381c36c7b36ecbdb93d3e118aeb579 | |
| parent | b2ba828379df713c29bb4fa3d4cd5ab305fe86b9 (diff) | |
| download | browserenv-cbff612a593c44cc452070e7c50e7b8f858c763b.tar.bz2 | |
Add `BROWSER` support to `OpenReader()`
If the `BROWSER` environment variable is set, open the reader in the
given browser.
Works by copying the `Reader`'s contents to a temporary file and opening
that with `OpenFile()`.
| -rw-r--r-- | browserenv.go | 16 | 
1 files changed, 16 insertions, 0 deletions
| diff --git a/browserenv.go b/browserenv.go index 98f2e24..f0a6dd8 100644 --- a/browserenv.go +++ b/browserenv.go @@ -2,6 +2,7 @@ package browserenv  import (  	"io" +	"io/ioutil"  	"os"  	"os/exec"  	"path/filepath" @@ -29,6 +30,21 @@ func OpenFile(path string) error {  }  func OpenReader(r io.Reader) error { +	envCommand := envBrowserCommand() +	if envCommand != "" { +		tempFile, err := ioutil.TempFile("", "browserenv") +		if err != nil { +			return err +		} + +		_, err = io.Copy(tempFile, r) +		if err != nil { +			return err +		} + +		return OpenFile(tempFile.Name()) +	} +  	return browser.OpenReader(r)  } | 
