Created
December 12, 2021 11:26
-
-
Save matthewblott/a582dda0ffc3b9f6f3a87960c65161d0 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
#!/bin/bash | |
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