1
1
#compdef gist
2
2
# ------------------------------------------------------------------------------
3
- # Copyright (c) 2015 Github zsh-users - http://github.com/zsh-users
3
+ # Copyright (c) 2017 Github zsh-users - http://github.com/zsh-users
4
4
# All rights reserved.
5
5
#
6
6
# Redistribution and use in source and binary forms, with or without
35
35
# -------
36
36
#
37
37
# * Akira Maeda <https://github.com/glidenote>
38
+ # * Patrick Ziegler <https://github.com/patrick96>
38
39
#
39
40
# ------------------------------------------------------------------------------
40
41
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
@@ -61,6 +62,58 @@ _arguments -C \
61
62
'(-P --paste)'{-P,--paste}'[Paste from the clipboard to gist]' \
62
63
'(-h --help)'{-h,--help}'[print options help]' \
63
64
'(-v --version)'{-v,--version}'[print version]' \
65
+ '(-r --read)'{-r,--read}'[Read a gist and print out the contents]:user gists:user_gists' \
64
66
'*: :_files' && ret=0
65
67
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
+
66
119
return ret
0 commit comments