Skip to content

Commit fa1c720

Browse files
authored
Merge pull request #496 from patrick96/gist-read
Add gist completion for read flag
2 parents a185e55 + 0ed2e86 commit fa1c720

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

Diff for: src/_gist

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#compdef gist
22
# ------------------------------------------------------------------------------
3-
# Copyright (c) 2015 Github zsh-users - http://github.com/zsh-users
3+
# Copyright (c) 2017 Github zsh-users - http://github.com/zsh-users
44
# All rights reserved.
55
#
66
# Redistribution and use in source and binary forms, with or without
@@ -35,6 +35,7 @@
3535
# -------
3636
#
3737
# * Akira Maeda <https://github.com/glidenote>
38+
# * Patrick Ziegler <https://github.com/patrick96>
3839
#
3940
# ------------------------------------------------------------------------------
4041
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
@@ -61,6 +62,58 @@ _arguments -C \
6162
'(-P --paste)'{-P,--paste}'[Paste from the clipboard to gist]' \
6263
'(-h --help)'{-h,--help}'[print options help]' \
6364
'(-v --version)'{-v,--version}'[print version]' \
65+
'(-r --read)'{-r,--read}'[Read a gist and print out the contents]:user gists:user_gists' \
6466
'*: :_files' && ret=0
6567

68+
_user_gists_cache_policy() {
69+
# rebuild if cache is more than a day old
70+
local -a oldp
71+
oldp=( "$1"(mh+1) )
72+
(( $#oldp ))
73+
}
74+
75+
user_gists() {
76+
local update_policy ret=1
77+
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
78+
if [[ -z "$update_policy" ]]; then
79+
zstyle ":completion:${curcontext}:" cache-policy _user_gists_cache_policy
80+
fi
81+
82+
# stores the gists of the logged in user in the format ID[Description]
83+
_list=()
84+
_cached_gists="user_gists"
85+
86+
# retrieve/Write gists from/to cache
87+
if _cache_invalid $_cached_gists || ! _retrieve_cache $_cached_gists; then
88+
_gists=$(gist -l)
89+
90+
if [ $? -eq 0 ]; then
91+
_store_cache $_cached_gists _gists
92+
else
93+
# some error occurred, the user is probably not logged in
94+
# set _gists to an empty string so that no completion is attempted
95+
_gists=""
96+
fi
97+
else
98+
_retrieve_cache $_cached_gists
99+
fi
100+
101+
if [ -n "$_gists" ]; then
102+
echo "$_gists" | while read -r line; do
103+
# Splitting the gist -l output
104+
url="$(echo "$line" | cut -d " " -f 1 | cut -d "/" -f 4)"
105+
# gists w/o descriptions can have only one column in the output, those
106+
# have their description set to an empty string
107+
description="$(echo "$line" | awk '{if(NF > 1){$1=""; print $0}}')"
108+
109+
_list+=( "${url}[${description}]" )
110+
done
111+
112+
_values "gists" $_list
113+
ret=0
114+
fi
115+
116+
return ret
117+
}
118+
66119
return ret

0 commit comments

Comments
 (0)