Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes to portable-screencap #5

Closed
wants to merge 9 commits into from
47 changes: 24 additions & 23 deletions portable-screencap
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
#!/bin/sh

set -e
set -o pipefail
USAGE="usage: $0 - take a screenshot and output to stdout, portably"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i recall needing set -o pipefail for something. may have been in the ipfs-screencap script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes something like false | true fail though it succeeds by default:

$ false | true  
$ echo $?
0
$ set -o pipefail
$ false | true
$ echo $?
1

But as I said in chriscool@fcd55a5 it is not portable as dash, which is the default /bin/sh on Ubuntu, doesn't know about it:

$ /bin/sh
$ set -o pipefail
/bin/sh: 1: set: Illegal option -o pipefail

usage() {
echo "$0 - take a screenshot and output to stdout, portably"
echo "$USAGE"
exit 0
}

die() {
echo >&2 "error: $@"
exit 1
}

# get user options
while [ $# -gt 0 ]; do
# get options
arg="$1"
shift

case "$arg" in
-h) usage ;;
--help) usage ;;
-h|--help) usage ;;
--*)
die "unrecognised option: '$arg'\n$usage" ;;
die "unrecognised option: '$arg'\n$USAGE" ;;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops copy-paste fail on my part

*)
die "unrecognised argument" ;;
die "$0 accepts no arguments\n$USAGE" ;;
esac
done

die() {
echo >&2 "error: $@"
exit 1
}

darwin_screencap() {
screencapture -i "$1"
}

linux_screencap() {
if [ `which import` ]; then
if type import >/dev/null
then
import "$1"
elif [ `which shutter` ]; then
elif type shutter >/dev/null
then
shutter -e -o "$1"
elif [ `which scrot` ]; then
elif type scrot >/dev/null
then
scrot "$1"
else
die "cannot find a supported screen capture tool
Expand All @@ -50,15 +51,15 @@ please install a supported one:
}

all_screencap() {
case `uname` in
Darwin) darwin_screencap $1 ;;
Linux) linux_screencap $1 ;;
*) die "screencap not implemented for "`uname` ;;
case $(uname) in
Darwin) darwin_screencap "$1" ;;
Linux) linux_screencap "$1" ;;
*) die "screencap not implemented for $(uname)" ;;
esac
}

date=$(date +"%Y-%m-%dT%H:%M:%SZ")
fpath=$(mktemp /tmp/screencap.$date.XXXXXX.png)
all_screencap $fpath
cat "$fpath"
rm "$fpath" || true
fpath=$(mktemp "/tmp/screencap.$date.XXXXXX.png")
all_screencap "$fpath" || die "capture failed"
echo "Capture is in '$fpath'"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want this script to output the file to stdout. storing in files makes other things (like chaining operations) harder. portable-screencap >filename is easy enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sorry about that.