#!/bin/sh EXPORT_LIB=y . "$XDG_CONFIG_HOME/vdirsyncer/config_gen" cat << EOF [calendars] EOF _sync() { yes | vdirsyncer discover 1> /dev/null 2> /dev/null vdirsyncer sync 1> /dev/null 2> /dev/null vdirsyncer metasync 1> /dev/null 2> /dev/null } color_fmt() { pastel format hsl | tr -d 'hsl(,%)' | sed 's/\.[^ ]\+//g' ; } TERMINAL_COLORS="$(for n in $(seq 0 15) ; do xrdb -get "color$n" ; done | color_fmt)" _color_quantize() { INPUT="$(echo $1 | grep -o '#[0-9a-fA-F]\{6\}' | color_fmt)" COLOR="$(printf '%s\n%s' "$INPUT" "$TERMINAL_COLORS" | awk --non-decimal-data ' # abs() is not built-in??? function abs(v) { return v < 0 ? -v : v } BEGIN { closest = 0; # closest ANSI color index # offset line number once for input color, and another time to make color # numbers start from 0 (line numbers start from 1 by default) NR -= 2; # keep track of the first color first = 1; } { # first line of input contains input colors if (NR == -1) { input_r = $1; input_g = $2; input_b = $3; next; } if (NR == 0) next; # do not pick background color # sloppy check to nearest RGB color diff = abs(input_r - $1) + abs(input_g - $2) + abs(input_b - $3); if (diff < max_diff || first) { first = 0; max_diff = diff; closest = NR; } } END { print closest; } ')" case "$COLOR" in 0) echo "black" ; break ;; 1) echo "dark red" ; break ;; 2) echo "dark green" ; break ;; 3) echo "brown" ; break ;; 4) echo "dark blue" ; break ;; 5) echo "dark magenta" ; break ;; 6) echo "dark cyan" ; break ;; 7) echo "dark gray" ; break ;; 8) echo "light gray" ; break ;; 9) echo "light red" ; break ;; 10) echo "light green" ; break ;; 11) echo "yellow" ; break ;; 12) echo "light blue" ; break ;; 13) echo "light magenta" ; break ;; 14) echo "light cyan" ; break ;; 15) echo "white" ; break ;; esac } _cfg_dav() { [ "$DAV_TYPE" != "caldav" ] && return jq --raw-output '.collections[][0]' "$VDIRSYNCER_STATUS_PATH/$ID.collections" | while read -r collection ; do collection_path="$XDG_DATA_HOME/vdirsyncer/$ID/$collection" color_path="$collection_path/color" displayname_path="$collection_path/displayname" [ ! -d "$collection_path" ] && _sync [ ! -f "$color_path" ] && _sync [ ! -f "$displayname_path" ] && _sync COLOR="$(cat "$color_path")" NAME="$(cat "$displayname_path")" [ -n "$COLOR" ] && [ -n "$QUANTIZE_COLOR" ] && COLOR="$(_color_quantize "$COLOR")" cat << EOF [[$NAME]] path = "$collection_path" EOF [ -n "$COLOR" ] && echo "color = \"$COLOR\"" echo done } _cfg_ical() { collection_path="$XDG_DATA_HOME/vdirsyncer/$ID" [ ! -d "$collection_path" ] && _sync cat << EOF [[$NAME]] path = "$collection_path" readonly = True EOF [ -n "$COLOR" ] && echo "color = \"$COLOR\"" echo } . "$CONFIG_FILE" printf '# %s:%s=%s\n' vim ft dosini