Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Useful bash helper for invoking dot
#!/bin/bash
echo $0 | grep -q "dot-functions.sh" && {
echo "usage: source dot-functions.sh, then run 'dot foo.dot' and the PNG will show"
exit 1
}
produce_image_type() {
for arg in "$@"; do
[[ "$arg" =~ -T[-_a-z]+ ]] && echo ${arg#-T} && return 0
done
echo "png"
}
dot() {
local DOT_CMD=`which dot`
local IMAGE_TYPE=`produce_image_type "$@"`
for f in "$@"; do
[[ "$f" =~ -T[-_a-z]+ ]] && continue
# strip trailing ".dot"
# NOTE: also handle trailing ".", which is what happens when you
# tab-complete after the png already exists
EXT=`echo $f | grep -o '[^.]*$'` # usually just "dot", but some people call dot files .gv
FILE=`echo $f | sed -e 's/\.[^.]*$//' -e 's/\.$//'`
$DOT_CMD -T$IMAGE_TYPE "$FILE.$EXT" > "$FILE.$EXT.$IMAGE_TYPE" && local IMAGE_FILES="$IMAGE_FILES \"$FILE.$EXT.$IMAGE_TYPE\"" || return 1
done &&
if [[ `uname -a` =~ "Darwin" ]]
then
eval "open $IMAGE_FILES"
else
eval "gnome-open $IMAGE_FILES"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment