Created
February 23, 2017 17:00
-
-
Save thcipriani/3b67432310ac6bfba26f01c16e931f75 to your computer and use it in GitHub Desktop.
I keep this function sourced from my .bashrc—it is incredibly useful.
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
spell() { | |
# /me futilely avoids googling words to find their correct spelling | |
local candidates oldifs word array_pos | |
oldifs="$IFS" | |
IFS=':' | |
# Parse the apsell format and return a list of ":" separated words | |
read -a candidates <<< "$(printf "%s\n" "$1" \ | |
| aspell -a \ | |
| awk -F':' '/^&/ { | |
split($2, a, ",") | |
result="" | |
for (x in a) { | |
gsub(/^[ \t]/, "", a[x]) | |
result = a[x] ":" result | |
} | |
gsub(/:$/, "", result) | |
print result | |
}')" | |
# Reverse number and print the parsed bash array because the list comes | |
# out of gawk backwards | |
for item in "${candidates[@]}"; do | |
printf '%s\n' "$item" | |
done \ | |
| tac \ | |
| nl \ | |
| less -FirSX | |
printf "[ $(tput setaf 2)?$(tput sgr0) ]\t%s" \ | |
'Enter the choice (empty to cancel, 0 for input): ' | |
read index | |
[[ -z "$index" ]] && return | |
[[ "$index" == 0 ]] && word="$1" | |
[[ -z "$word" ]] && { | |
array_pos=$(( ${#candidates[@]} - index )) | |
word="${candidates[$array_pos]}" | |
} | |
[[ -n "$word" ]] && { | |
printf "$word" | xsel -p | |
printf "Copied '%s' to clipboard!\n" "$word" | |
} || printf "[ $(tput setaf 1):($(tput sgr0) ] %s\n" 'No match found' | |
IFS="$oldifs" | |
} |
Author
thcipriani
commented
Feb 23, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment