fzf-contact/src/main.sh

186 lines
5.0 KiB
Bash

#!/bin/sh
set -eu
# Always load functions
# Helper functions
. "sh/helper.sh"
# iCalendar routines
. "sh/icalendar.sh"
# Attachment handling
. "sh/attachment.sh"
# Categories handling
. "sh/categories.sh"
# Load environment variables only when not yet loaded
if [ ! "${SCRIPT_LOADED:-}" ]; then
# Read configuration
. "sh/config.sh"
# Read theme
. "sh/theme.sh"
# Load awk scripts
. "sh/awkscripts.sh"
# Mark as loaded
export SCRIPT_LOADED=1
fi
__lines() {
find "$ROOT" -mindepth 2 -maxdepth 2 -type f -name '*.vcf' -print0 | xargs -0 -P 0 \
awk \
-v collection_labels="$COLLECTION_LABELS" \
-v nickname_format="$FORMAT_NICKNAME" \
-v fn_format="$FORMAT_FN" \
-v group_label="$FLAG_GROUP" \
-v org_label="$FLAG_ORGANIZATION" \
-v location_label="$FLAG_LOCATION" \
-v individual_label="$FLAG_INDIVIDUAL" \
"$AWK_LIST" |
sort -g
}
# Program starts here
if [ "${1:-}" = "--help" ]; then
bn="$(basename "$0")"
shift
echo "Usage: $bn [OPTION] [FILTER]...
[OPTION]
--help Show this help and exit
Git Integration:
--git-init Activate git usage and exit
--git <cmd> Run git command and exit
Interactive Mode:
--new [FILTER..] Create new entry interactively and start
[FILTER..] Start with the specified filter
Non-Interactive Mode:
--list [FILTER..] List entries and exit
--import <file> Import vCard file and exit
--collection <nr> Select collection to which to import vCard
file. The argument <nr> is the ordinal
describing the collection. It defaults to the
starting value of 1.
[FILTER]
You may specify any of these filters. Filters can be negated using the
--no-... versions, e.g., --no-individuals. Multiple filters are applied in
conjuction.
--individuals Show individuals only
--groups Show groups only
--organizations Show organizations only
--locations Show locations only
--filter <query> Specify custom query
Examples:
$bn --git log
$bn --new
$bn --individuals
$bn --no-locations --filter \"Athens\"
$bn --list --locations
$bn --import ./contact.vcf
$bn --import ./boss.vcf --collection 2
"
exit
fi
# Command line arguments: Interal use
. "sh/cliinternal.sh"
# Command line arguments
. "sh/cli.sh"
# Parse command-line filter (if any)
. "sh/filter.sh"
if [ -n "${list_option:-}" ]; then
__lines |
$FZF \
--filter="$query" \
--delimiter="\t" \
--no-sort \
--with-nth='{2} {3} {1}'
exit 0
fi
# Set terminal title
if [ "$SET_TERMINAL_TITLE" = "yes" ]; then
printf '\033]0;%s\007' "$TERMINAL_TITLE"
fi
while true; do
query=$(stripws "$query")
query="${query:+"$query "}"
selection=$(
__lines | $FZF \
--ansi \
--query="$query" \
--delimiter="\t" \
--no-sort \
--no-hscroll \
--reverse \
--with-nth='{2} {3} {1}' \
--print-query \
--accept-nth=4 \
--preview="$0 --preview {4}" \
--expect="ctrl-n,ctrl-alt-d,alt-v,ctrl-a,ctrl-t" \
--bind="ctrl-r:reload($0 --reload)" \
--bind="alt-0:change-query()" \
--bind="alt-1:change-query(${COLLECTION1:-} )" \
--bind="alt-2:change-query(${COLLECTION2:-} )" \
--bind="alt-3:change-query(${COLLECTION3:-} )" \
--bind="alt-4:change-query(${COLLECTION4:-} )" \
--bind="alt-5:change-query(${COLLECTION5:-} )" \
--bind="alt-6:change-query(${COLLECTION6:-} )" \
--bind="alt-7:change-query(${COLLECTION7:-} )" \
--bind="alt-8:change-query(${COLLECTION8:-} )" \
--bind="alt-9:change-query(${COLLECTION9:-} )" \
--bind="alt-i:change-query($FLAG_INDIVIDUAL )" \
--bind="alt-g:change-query($FLAG_GROUP )" \
--bind="alt-o:change-query($FLAG_ORGANIZATION )" \
--bind="alt-l:change-query($FLAG_LOCATION )" \
--bind="alt-w:toggle-preview-wrap" \
--bind="/:toggle-preview" \
--bind="ctrl-d:preview-half-page-down" \
--bind="ctrl-u:preview-half-page-up" \
--bind="ctrl-s:execute($SYNC_CMD; [ -n \"${GIT:-}\" ] && ${GIT:-echo} add -A && ${GIT:-echo} commit -am 'Synchronized'; printf 'Press <enter> to continue.'; read -r tmp)" || #--color='preview-bg:#ecc297,preview-fg:black' \
true
)
# Line 1: query
# Line 2: key ("" for enter)
# Line 3: relative file path
lines=$(echo "$selection" | wc -l)
if [ "$lines" -eq 1 ]; then
exit 0
fi
query=$(echo "$selection" | head -n 1)
key=$(echo "$selection" | head -n 2 | tail -n 1)
fname=$(echo "$selection" | head -n 3 | tail -n 1)
file="$ROOT/$fname"
case "$key" in
"ctrl-n")
__new
;;
"ctrl-alt-d")
__delete "$file"
;;
"alt-v")
$EDITOR "$file"
;;
"ctrl-a")
__attachment_view "$file"
;;
"ctrl-t")
cat="$(__select_category)"
[ -n "$cat" ] && query="'$cat'"
;;
"")
__edit "$file"
;;
esac
done