Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Last active March 3, 2016 23:32
Show Gist options
  • Save DanielFGray/d47925487840bb445296 to your computer and use it in GitHub Desktop.
Save DanielFGray/d47925487840bb445296 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# DanielFGray 2016
# https://gist.github.com/d47925487840bb445296
err() {
local c_red="${esc}[31m"
echo -e "${c_red}$1${c_reset}" >&2
}
die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
has() {
local verbose=false
if [[ $1 == '-v' ]]; then
verbose=true
shift
fi
for c in "$@"; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
[[ "$verbose" == true ]] && err "$c not found"
return 1
fi
done
}
# http://stackoverflow.com/a/10660730
rawurlencode() {
local string="$*"
local strlen=${#string}
local encoded=""
for (( pos=0; pos < strlen; pos++ )); do
c="${string:$pos:1}"
case "$c" in
[-_.~a-zA-Z0-9])
o="$c" ;;
*)
printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
has -v curl jq fzf || die
url=$(command curl -sfL "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&q=$(rawurlencode "$*")" |
jq -c '.["responseData"]["results"] | map("\(.unescapedUrl) \(.title) | \(.content)") | .[]' |
sed -r 's/<[^>]*>//g; s/\\n//g; s/^\s*"//; s/",?$//' |
fzf -e --ansi --cycle --inline-info | awk '{print $1}')
if [[ -n "$url" ]]; then
case "$url" in
*youtube.com*|*youtu.be*)
mpv "$url" ;;
*)
w3m "$url" ;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment