aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-02-09 12:57:39 +0800
committertypebrook <typebrook@gmail.com>2020-02-09 12:57:39 +0800
commit6bd0f583ea178757eae6874c3f4cc07f23f5d552 (patch)
treeb9bac884f3e7d256f615dbb2ea408124d866edd3
parente7fb40ca3a6d1dabd5d15551546ae52654f052db (diff)
update
-rwxr-xr-xscripts/gist40
1 files changed, 24 insertions, 16 deletions
diff --git a/scripts/gist b/scripts/gist
index 01f1cd0..60a7be7 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -42,6 +42,10 @@ configuredClient=""
42GITHUB_API=https://api.github.com 42GITHUB_API=https://api.github.com
43CONFIG=~/.config/gist.conf; mkdir -p ~/.config 43CONFIG=~/.config/gist.conf; mkdir -p ~/.config
44 44
45action="${EDITOR:-vi} ."
46folder=~/gist && mkdir -p $folder
47auto_sync=true
48
45# Shell configuration 49# Shell configuration
46set -o pipefail 50set -o pipefail
47[ "$TRACE" ] && set -x 51[ "$TRACE" ] && set -x
@@ -163,11 +167,24 @@ _apply_config() {
163 _validate_config "$@" || return 1 167 _validate_config "$@" || return 1
164 168
165 AUTH_HEADER="Authorization: token $token" 169 AUTH_HEADER="Authorization: token $token"
166 [[ -z "$action" ]] && action="${EDITOR:-vi} ."
167 [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder
168 INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX 170 INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX
169} 171}
170 172
173_check_repo_status() {
174 # if repo is not yet cloned, show green message "Not cloned yet"
175 if [[ ! -d $1 ]]; then
176 if [[ $auto_sync ]]; then echo "\e[32m[cloning]\e[0m"; fi
177 else
178 cd $1
179 if [[ -n $(git status --short) ]] &>/dev/null; then
180 echo "\e[36m[working]\e[0m"
181 else
182 [[ $(_blob_code $1) != $2 ]] 2>/dev/null && echo "\e[31m[outdated]\e[0m"
183 [[ -n $(git cherry) ]] 2>/dev/null && echo "\e[31m[ahead]\e[0m"
184 fi
185 fi
186}
187
171# TODO Split into 2 funcs for filter and display 188# TODO Split into 2 funcs for filter and display
172# Show the list of gist, but not updated time 189# Show the list of gist, but not updated time
173_show_list() { 190_show_list() {
@@ -183,19 +200,11 @@ _show_list() {
183 | while read index link blob_code file_num comment_num author description; do 200 | while read index link blob_code file_num comment_num author description; do
184 [[ $1 == "s" ]] && local name=$author 201 [[ $1 == "s" ]] && local name=$author
185 local repo=$folder/$(echo $link | sed 's#.*/##') 202 local repo=$folder/$(echo $link | sed 's#.*/##')
186 local occupy=0 203 local extra=$(_check_repo_status $repo $blob_code)
187 local extra="$file_num $comment_num" 204 [[ -z $extra ]] && extra="$file_num $comment_num"
188 205
189 # if repo is not yet cloned, show green message "Not cloned yet" 206 echo -e "$(printf "% 3s" $index)" $link $name $extra $description \
190 [[ ! -d $repo ]] && extra="\e[32m[Not cloned yet]\e[0m" && occupy=16 207 | cut -c -$(tput cols)
191 # if there are some changes in git index or working directory, show blue message "working"
192 [[ -n $(cd $repo && git status --short) ]] 2>/dev/null && extra="\e[36m[working]\e[0m" && occupy=9
193 # if there files are different, show red message "outdated"
194 [[ $(_blob_code $repo) != $blob_code ]] 2>/dev/null && extra="\e[31m[outdated]\e[0m" && occupy=10
195 # if there is a commit not yet push, show red message "ahead"
196 [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7
197
198 echo -e "$(printf "% 3s" $index)" $link $name $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) )
199 done 208 done
200 echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty 209 echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty
201} 210}
@@ -281,8 +290,7 @@ _query_user() {
281} 290}
282 291
283_blob_code() { 292_blob_code() {
284 cd $1 \ 293 cd $1 && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-'
285 && git ls-tree master | cut -d' ' -f3 | cut -c-7 | sort | paste -sd '-'
286} 294}
287 295
288# update local git repos 296# update local git repos