#!/bin/sh . "$XDG_CONFIG_HOME/vdirsyncer/config_gen" lib 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 } _hexfmt() { sed 's/#\?\(..\)\(..\)\(..\)/0x\1 0x\2 0x\3/g' ; } TERMINAL_COLORS="$(for n in $(seq 0 15) ; do xrdb -get "color$n" ; done | _hexfmt)" _color_quantize() { INPUT="$(echo $1 | grep -o '#[0-9a-fA-F]\{6\}' | _hexfmt)" 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 # max diff can at most be 3 * 255 (see diff calculation) max_diff = 0xff * 3; # 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; } { # 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) { 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_caldav() { 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