From 9d8609b1b41b333e81e3b95f292067548ba469e9 Mon Sep 17 00:00:00 2001 From: Jaidyn Levesque Date: Sun, 26 Jan 2020 00:58:57 -0600 Subject: [PATCH] Add util --- README.txt | 10 +++++++++ util/ccal.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100755 util/ccal.sh diff --git a/README.txt b/README.txt index 91af995..d63f88d 100644 --- a/README.txt +++ b/README.txt @@ -42,6 +42,16 @@ $ farend -l 100 -q /tmp/todo # … usage goes on along those lines. +———————————————————————————————————————— +UTILS +———————————————————————————————————————— +The "utils/" folder contains some stuff I personally find useful that's related +to the project. + +Right now, it just contains `ccal`, a frontend to `cal` that portably +highlights the current day (since it's not builtin in many versions). + + ———————————————————————————————————————— BORING STUFF ———————————————————————————————————————— diff --git a/util/ccal.sh b/util/ccal.sh new file mode 100755 index 0000000..f0b43d2 --- /dev/null +++ b/util/ccal.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# -------------------------------------- +# name: ccal +# desc: portable colourized `cal` +# main: jaidyn ann +# -------------------------------------- + +# On some systems, the `cal` command can't highlight the current day. +# This version does it portably, only assuming you have POSIX `cal`. + +# To change the highlight colours, set the $CCAL_HEAD and $CCAL_TAIL +# variables. $CCAL_HEAD should set the colours (ANSI codes etc), _TAIL should +# reset colours, ofc. + +# Return today's date, YYYY-MM-DD +function today { + date +"%Y-%m-%d" +} + +# Return the day of a given date +function date_day { + local date="$1" + echo "$date" \ + | awk -F "-" '{print $3}' +} + +# Concatenate three arguments into one non-delimited string +function join_three { + a="$1" + b="$2" + c="$3" + echo "${a}${b}${c}" +} + +# Color the given text from piped input, using giving head and tail as codes +function colour_text { + local text="$1" + local head="$2" + local tail="$3" + + sed 's%'"$text"'%'"$(join_three "$head" "$text" "$tail")"'%' +} + +# Colourized form of `cal` +function colourized_cal { + cal $@ \ + | colour_text "$(date_day "$(today)")" "$CCAL_HEAD" "$CCAL_TAIL" +} + + + +# Set colours if you haven't +if test -z "$CCAL_HEAD"; then + white_bg="$(tput setab 7 2>/dev/null)" + black_fg="$(tput setaf 0 2>/dev/null)" + bold="$(tput bold 2>/dev/null)" + reset="$(tput sgr0)" + CCAL_HEAD="${white_bg}${black_fg}${bold}" + CCAL_TAIL="${reset}" +fi + +colourized_cal $@