Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@corporate-gadfly
Last active October 28, 2020 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corporate-gadfly/10586247 to your computer and use it in GitHub Desktop.
Save corporate-gadfly/10586247 to your computer and use it in GitHub Desktop.
awk recipe which takes the output of `cal` command and colorizes it. Useful for geektool.
##
## only warks with GNU awk (gawk)
## highlighting for today's date can be disabled with skiptodayhighlight=1
## command-line parameter
##
#
#
# For color codes see:
# http://www.termsys.demon.co.uk/vtansi.htm
# print input string in yellow
function yellow(string) {
# \033[0m is used to reset escape sequence
printf("%s%s%s", "\033[1;33m", string, "\033[0m");
}
# print input string in cyan
function cyan(string) {
# \033[0m is used to reset escape sequence
printf("%s%s%s", "\033[1;36m", string, "\033[0m");
}
# print input string in red
function red(string) {
# \033[0m is used to reset escape sequence
printf("%s%s%s", "\033[1;31m", string, "\033[0m");
}
# print input string in green
function green(string) {
# \033[0m is used to reset escape sequence
printf("%s%s%s", "\033[1;32m", string, "\033[0m");
}
# since this will be used with output from cal, assume fixed width fields
# of 3 characters
BEGIN {
FIELDWIDTHS = "3 3 3 3 3 3 3"
today = strftime("%e")
}
# print month in yellow
NR==1 {print yellow($0)}
# print the weekdays in green
NR==2 {print green($0)}
# for rows greater than 2
NR>2 {
# iterate over all the columns
for (i=1; i<=NF; i++) {
# use first 2 characters only
date = substr($i, 1, 2)
if (i == 1 || i==NF) { # first column or last column
# print in red if today's date and not skipping, otherwise cyan
date = today == date && !skiptodayhighlight ? red(date) : cyan(date)
# print newline if last column
printf("%s%s", date, i == NF ? ORS : OFS)
} else { # middle columns
# print in red if today's date and not skipping, otherwise normal
date = today == date && !skiptodayhighlight ? red(date) : date
printf("%s%s", date, OFS)
}
}
}
@corporate-gadfly
Copy link
Author

Sample output where today's date is highlighted in red, weekends are in cyan.
sample output
Use as follows:

cal | gawk -f ~/etc/calendar.awk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment