Browse code

Add util

Jaidyn Levesque authored on 2020-01-26 06:58:57
Showing 2 changed files
... ...
@@ -42,6 +42,16 @@ $ farend -l 100 -q /tmp/todo
42 42
 # … usage goes on along those lines.
43 43
 
44 44
 
45
+————————————————————————————————————————
46
+UTILS
47
+————————————————————————————————————————
48
+The "utils/" folder contains some stuff I personally find useful that's related
49
+to the project.
50
+
51
+Right now, it just contains `ccal`, a frontend to `cal` that portably
52
+highlights the current day (since it's not builtin in many versions).
53
+
54
+
45 55
 ————————————————————————————————————————
46 56
 BORING STUFF
47 57
 ————————————————————————————————————————
48 58
new file mode 100755
... ...
@@ -0,0 +1,62 @@
1
+#!/bin/sh
2
+# --------------------------------------
3
+# name: ccal
4
+# desc: portable colourized `cal`
5
+# main: jaidyn ann <jadedctrl@teknik.io>
6
+# --------------------------------------
7
+
8
+# On some systems, the `cal` command can't highlight the current day.
9
+# This version does it portably, only assuming you have POSIX `cal`.
10
+
11
+# To change the highlight colours, set the $CCAL_HEAD and $CCAL_TAIL
12
+# variables. $CCAL_HEAD should set the colours (ANSI codes etc), _TAIL should
13
+# reset colours, ofc.
14
+
15
+# Return today's date, YYYY-MM-DD
16
+function today {
17
+	date +"%Y-%m-%d"
18
+}
19
+
20
+# Return the day of a given date
21
+function date_day {
22
+	local date="$1"
23
+	echo "$date" \
24
+	| awk -F "-" '{print $3}'
25
+}
26
+
27
+# Concatenate three arguments into one non-delimited string
28
+function join_three {
29
+	a="$1"
30
+	b="$2"
31
+	c="$3"
32
+	echo "${a}${b}${c}"
33
+}
34
+
35
+# Color the given text from piped input, using giving head and tail as codes
36
+function colour_text {
37
+	local text="$1"
38
+	local head="$2"
39
+	local tail="$3"
40
+
41
+	sed 's%'"$text"'%'"$(join_three "$head" "$text" "$tail")"'%'
42
+}
43
+
44
+# Colourized form of `cal`
45
+function colourized_cal {
46
+	cal $@ \
47
+	| colour_text "$(date_day "$(today)")" "$CCAL_HEAD" "$CCAL_TAIL"
48
+}
49
+
50
+
51
+
52
+# Set colours if you haven't
53
+if test -z "$CCAL_HEAD"; then
54
+	white_bg="$(tput setab 7 2>/dev/null)"
55
+	black_fg="$(tput setaf 0 2>/dev/null)"
56
+	bold="$(tput bold 2>/dev/null)"
57
+	reset="$(tput sgr0)"
58
+	CCAL_HEAD="${white_bg}${black_fg}${bold}"
59
+	CCAL_TAIL="${reset}"
60
+fi
61
+
62
+colourized_cal $@