-
Star
(116)
You must be signed in to star a gist -
Fork
(18)
You must be signed in to fork a gist
-
-
Save defunkt/318247 to your computer and use it in GitHub Desktop.
| #!/bin/sh -e | |
| # | |
| # Usage: browser | |
| # pipe html to a browser | |
| # e.g. | |
| # $ echo '<h1>hi mom!</h1>' | browser | |
| # $ ron -5 man/rip.5.ron | browser | |
| if [ -t 0 ]; then | |
| if [ -n "$1" ]; then | |
| open $1 | |
| else | |
| cat <<usage | |
| Usage: browser | |
| pipe html to a browser | |
| $ echo '<h1>hi mom!</h1>' | browser | |
| $ ron -5 man/rip.5.ron | browser | |
| usage | |
| fi | |
| else | |
| f="/tmp/browser.$RANDOM.html" | |
| cat /dev/stdin > $f | |
| open $f | |
| fi |
Try
:; I="$(basename $0)"; [ -z $BROWSER ] && open=open || open=$BROWSER
[ $# -eq 0 ] && set -- -; [ "$1" = "-" ] && [ -t 0 ] &&
echo "Usage: [ COMMAND | $I ] | [ $I < FILE ] | [ $I FILE ... ]" >&2 && exit
for what; do where="/tmp/$(od -N2 < /dev/urandom | openssl dgst -sha1).html"
cat "$what" > "$where" && $open "$where"; sleep 1 && rm -f "$where" & doneSome advantages:
- Respect
$BROWSER. Can beBROWSER=lynx,BROWSER='open -a Firefox', etc. - No bashisms (
$RANDOM) or zsh-isms (=(...)). - Correctly handles arguments that do not end in
.html. - Correctly handles multiple arguments and shell globs.
- Cleans up after itself.
- Doesn't even need a shebang!
Or, you might prefer what I use: avoid leaving your terminal at all, and:
cat "$what" > "$where" && qlmanage -p "$where" >/dev/null 2>&1; sleep 1 ...To show Markdown files in browsers, I found a way without temporary files: https://gist.github.com/Boldewyn/4311962
It uses data: URIs for fun and profit:
x-www-browser $(base64 -w0 | cat <(echo -n 'data:text/html;base64,') -)
# ^--- Debian link to default browser
# ^---- base64 slurps stdin as default
# ^--- concat the "data:" header, then the content
It's still working after ~10 years. π Thanks!
Installed with brew install browser π Tested on MacOS Mojave 10.14.6 π
Works perfectly! Thanks Chris. Oh.. and thanks for making Github! π
Small heads up for an improvement: open can be replaced by xdg-open to make this work on Linux. Maybe the script could detect if it is used on Mac/Linux and use the correct command based on that.
Small heads up for an improvement: open can be replaced by xdg-open to make this work on Linux. Maybe the script could detect if it is used on Mac/Linux and use the correct command based on that.
Here's @geoff-nixon's version as a standalone Bash script, with support already baked in for Quick Look or some other previewer. Requires bash to be somewhere in the search path if you're on Mac; should mostly work on Linux, too.
Note this was removed from Homebrew in January.
Zeokat usefull piece of code.