aboutsummaryrefslogtreecommitdiff
path: root/.config/khal/config_gen
blob: 683cd1718d9b447bba01b1abf54350e36dc04877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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