Skip to content

Instantly share code, notes, and snippets.

@prurigro
Last active August 29, 2015 14:14
Show Gist options
  • Save prurigro/71ff1c794dea971250cb to your computer and use it in GitHub Desktop.
Save prurigro/71ff1c794dea971250cb to your computer and use it in GitHub Desktop.
A pyCardDAV toolset wrapper for improved user experience on the command-line
#!/usr/bin/env bash
#
# Contact: A pyCardDAV toolset wrapper for improved user experience on the command-line
#
# Version 1.3.1
#
# Written by Kevin MacMartin (prurigro@gmail.com)
# Released under the MIT license
#
script_name="${0//*\/}"
[[ -t 1 ]] && {
c_wul=$'\e[4;37m' # WHITE (UNDERLINE)
c_d=$'\e[1;30m' # DARK GREY
c_r=$'\e[1;31m' # RED
c_g=$'\e[1;32m' # GREEN
c_y=$'\e[1;33m' # YELLOW
c_b=$'\e[1;34m' # BLUE
c_m=$'\e[1;35m' # MAGENTA
c_t=$'\e[1;36m' # CYAN
c_w=$'\e[1;37m' # WHITE
c_c=$'\e[0m' # DISABLES COLOUR
}
function pc_query_function() {
# Display the query being searched for
printf '%s\n' "Searching for: $c_w$pc_query_string${c_c}..."
# Obtains the raw query results
pc_query_result=$(pc_query -A "$pc_query_string")
# Cycle through and display each contact that was returned
while read -r; do
# Draw the top contact separator
printf '\n%s\n' "$c_d-----$c_c"
# Store the current contact's information in $pc_query_contact
pc_query_contact=$(sed '/^Name: '"$REPLY"'/,$!d;/^$/q' <<< "$pc_query_result")
# If the contact is contained in a list/category, move it beside their name in brackets
contact_category=$(egrep '^CATEGORIES: ' <<< "$pc_query_contact" | sed 's|^CATEGORIES: ||')
[[ -n "$contact_category" ]] && {
pc_query_contact=$(sed -re 's|^(Name: .*)$|\1 \${c_w}\(\${c_m}'"$contact_category"'\${c_w}\)\$c_c|' <<< "$pc_query_contact")
pc_query_contact=$(sed '/^CATEGORIES: /d' <<< "$pc_query_contact")
}
# Add a star to the contact's name if their contact is starred
[[ "$pc_query_contact" =~ ^[A-Z\-]*STARRED:\s*1 ]] \
&& pc_query_contact="$(sed -re 's|^(Name: .*)$|\1 ✪|' <<< "$pc_query_contact")"
# Format the results for terminal-friendly output
pc_query_contact=$(egrep -v '^(PHOTO|PRODID|REV|UID)' <<< "$pc_query_contact" \
| sed 's|^Name|NAME|;s|^EMAIL|MAIL|;s|^IMPP|ACCT|;s|^TEL|TELE|;/^[A-Z\-]*STARRED:.*$/d' \
| sed -re 's|MAIL (\([^)]*\)): |MAIL: \1 |;s|^ACCT: ([^:]*):(.*)|ACCT: \1 ≫ \2|;s|TELE (\([^)]*\)): |TELE: \1 |;s|\(([^)]*), pref\)(.*)|\(\1\)\2 ★|;s|^ADR \(([^)]*)\):\s*\;*|ADDR: \[\1\] |;s|^LABEL \(([^)]*)\):\s*\;*|ADDR: \[\1\] |' \
| sed -re 's|^(....[^:].*)| \1|' \
| sed -re 's|^([A-Z][A-Z][A-Z][A-Z]: )\(([^)]*)\)|\1\[\2\]|')
# Pure colour transformations (skip if we're in a pipe to save the cycles)
[[ -t 1 ]] \
&& pc_query_contact=$(sed -re 's|^NAME: (.*)('"$pc_query_string"')(.*)$|NAME: \1\${c_w}\2\${c_c}\3|;s|^MAIL: (.*)$|MAIL: \${c_wul}\1\$c_c|;s|^ACCT: ([^ ]*)|ACCT: \${c_r}\1\$c_c|' <<< "$pc_query_contact" \
| sed 's|\[|\$c_w\[\${c_b}|g;s|\]|\$c_w\]\$c_c|g' \
| sed 's|✪|\${c_y}✪$c_c|' \
| sed -re 's|^([A-Z][A-Z][A-Z][A-Z]:)|\${c_d}\1\$c_c|')
# Output the formatted contact data
eval printf '%s' '"'"$pc_query_contact"'"'
# Draw the bottom contact separator
printf '\n%s\n' "$c_d-----$c_c"
done < <(egrep '^Name: ' <<< "$pc_query_result" | sed 's|^Name: ||')
}
function help() {
printf '\n%s\n\n' "${c_w}Contact$c_c: A pyCardDAV wrapper for the terminal"
printf '%s\n' "${c_b}USAGE$c_c"
printf ' %s %s\n' "$c_m$script_name ${c_r}QUERY$c_c" "search for contacts matching the query text"
printf ' %s %s\n\n' "$c_m$script_name$c_c [${c_y}COMMAND$c_c] [${c_y}ARGS$c_c]" "run a command (with arguments when applicable)"
printf '%s\n' "${c_b}COMMANDS$c_c"
printf ' %s %s\n' "$c_w-i$c_d|$c_w--import$c_c [${c_y}ARGS$c_c]" "takes arguments for and runs pycard-import"
printf ' %s %s\n' "$c_w-u$c_d|$c_w--update$c_c" "download updates to the contact list from the server"
printf ' %s %s\n\n' "$c_w-h$c_d|$c_w--help$c_c" "display this help message"
}
[[ -z "$1" ]] && {
printf '\n%s\n' "${c_r}ERROR:$c_c No arguments given"
help
exit 1
}
case "$1" in
-u|--update)
pycardsyncer
;;
-i|--import)
[[ -z "$2" ]] && {
printf '%s\n\n' "${c_r}ERROR:$c_c run with arguments for ${c_w}pycard-import$c_c"
pycard-import -h
exit 1
}
shift
pycard-import "$@"
;;
-h|--help)
help
;;
*)
# Create the query string from the provided arguments
for word in "$@"; do
[[ -n "$pc_query_string" ]] \
&& pc_query_string+=" "
pc_query_string+="$word"
done
pc_query_function
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment