Last active
December 3, 2021 16:24
-
-
Save matthewblott/3ff404e40252e336e4fa5cf441cbe139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Search the gist list and filter on the supplied arguments. | |
# Display the results or if a single match is found then open the gist in the code editor. | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
IFS=$'\n\t' | |
args=("$@") | |
_search() { | |
local q="" | |
for arg in "${args[@]}" | |
do | |
q="${q} ${arg}" | |
done | |
local q0=$(echo "${q}" | cut -c 2-) | |
gh gist list --limit 1000 | \ | |
awk -v q=${q0} ' | |
BEGIN { | |
FS=" "; | |
count = split(q, array, " "); | |
} | |
{ | |
pass = 1; | |
for (i = 1; i <= count; ++i) { | |
if(tolower($0) !~ tolower(array[i])) { | |
pass = 0; | |
break; | |
} | |
} | |
$(NF-3)=""; | |
$(NF-2)=""; | |
$(NF-1)=""; | |
$(NF)=""; | |
if(pass == 1) { | |
print $(0); | |
} | |
} | |
END { | |
}' | xclip -selection clipboard | |
local data=($(xclip -selection clipboard -o)) | |
local count="${#data[@]}" | |
if [ $count -gt 1 ]; then | |
for var in "${data[@]}" | |
do | |
# the id field is required for editing but remove it from display | |
echo "${var}" | cut -c 34- | |
done | |
exit | |
fi | |
local id=$(echo $data | cut -c1-32) | |
gh gist edit $id | |
} | |
main() { | |
if [ "$#" -eq 0 ]; then | |
echo 'No arguments were provided.' | |
exit | |
fi | |
_search "$@" | |
} | |
if [[ "$0" == "$BASH_SOURCE" ]]; then | |
main "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment