-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzgist
executable file
·88 lines (74 loc) · 1.62 KB
/
fzgist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
declare -r esc=$'\033'
declare -r c_reset="${esc}[0m"
declare -r c_red="${esc}[31m"
# declare c_green="${esc}[32m"
# declare c_blue="${esc}[34m"
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&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
}
usage() {
LESS=-FEXR less <<'HELP'
ix [OPTIONS]
-l list all pastes, uses fzf for interactive use
-d [id] delete the paste at [id]
-i [id] replaces the paste with stdin
-h print this help
HELP
}
has -v fzf curl jq || die
declare json gist url id
declare -a preview
list_pastes() {
printf 'fetching user info...\r'
user=$(api github get user | jq -r '.gists_url')
user="${user%\{*}"
user="${user/#*.com}"
gists=$(api github get "$user" | jq -r '.[] | "\(.html_url) \(.description)"')
if has hxselect hxnormalize; then
preview+=( --preview='
g={};
curl -sL "${g%% *}" |
hxnormalize -xe |
hxselect ".file-box" |
w3m -T text/html -dump |
grep -v "^Raw$"'
)
fi
if has fzf; then
gist=$(fzf --cycle -e "${preview[@]}" <<< "$gists")
else
echo "$gists"
exit
fi
[[ -n "$gist" ]] || die
url="${gist%% *}"
id="${url##*/}"
printf '%s\n' "$id"
}
paste_gist() {
curl -d '@-' https://api.github.com/gists
}
while getopts ':hl' x; do
case "$x" in
h) usage; exit ;;
l) list_pastes; exit ;;
esac
done