dates 2.77 KB
#!/bin/sh

##
# various functions and aliases that make gcal more convinient
#
# Author: Georg Hopp <ghopp@bigpoint.net>
# Date: Tue Apr 17 17:19:56 CEST 2012
# Version: 1.0.0
#

alias gcal='LC_MESSAGES=de_DE.UTF-8 /usr/bin/gcal -K -s Monday'
alias hollidays='gcal -qde_hh -n'
alias dates='gcal -c'
alias mdates='gcal -cm'
alias tcomingf='tcoming | sed '\''1!d'\'
alias ttomorrowf='ttomorrow | sed '\''1!d'\'

function terms() {
	_terms "all" 1 "$@"
}

function tcoming() {
	_terms "coming" 1
}

function tpast() {
	_terms "past" 1
}

function ttomorrow() {
	_terms "all" 0 -ct
}

function _terms() {
	local modifier=${1:-all}
	local color=${2:-0}
	local current_time=`date +%H:%M`
	shift 2

	#dates "$@" | sed '1,/^Fixed date/d; /^$/d; s/[^:]*: //'
	dates "$@" | awk -v ctime="${current_time}" -v mod="${modifier}" \
		-v col=${color} -v format=${DATES_FORMAT} '
		BEGIN {
			chour = ctime; gsub(/:.*/, "", chour);
			cminute = ctime; gsub(/.*:/, "", cminute);
			ctime = chour * 60 + cminute
		}

		NR == 1, /^Fixed date/ {next}
		/^$/ {next}

		{
			gsub(/^[^:]*: /, "");
			FS=" ";
			hour=$1; gsub(/:.*/, "", hour);
			minute=$1; gsub(/.*:/, "", minute);
			time = hour * 60 + minute

			color=""
			if (format == "i3bar") {
				if (col != 0 && time != 0 && time-ctime <= 15)
					color="\"color\":\"#00FF00\",";
				if (col != 0 && time != 0 && time-ctime <= 5)
					color="\"color\":\"#FFFF00\",";
				if (col != 0 && time != 0 && time-ctime <= 0)
					color="\"color\":\"#FF0000\",";
			}
			else {
				if (col != 0 && time != 0 && time-ctime <= 15)
					color="\033[38;5;82m";
				if (col != 0 && time != 0 && time-ctime <= 5)
					color="\033[38;5;190m";
				if (col != 0 && time != 0 && time-ctime <= 0)
					color="\033[38;5;124m";
			}

			if ((mod == "all") ||
				(mod == "coming" && (time+5 >= ctime || time == 0)) ||
				(mod == "past" && (time < ctime || time == 0))) {

			    gsub(/\\/, "", $0);
				if (format == "i3bar") {
					gsub(/"/, "\\\"", $0);
					out = color "\"full_text\":\"" $0 "\""
				}
				else {
					out = color $0 "\033[00m"
				}
				print out
			}
		}
		
		END {
			if ("" ==  out) {
				if (format == "i3bar") {
					print "\"full_text\":\"none\""
				}
				else {
					print "none"
				}
			}
		}'
}

test "X$(basename -- "$0")" = "Xgcal" && gcal "$@"
test "X$(basename -- "$0")" = "Xhollidays" && hollidays "$@"
test "X$(basename -- "$0")" = "Xdates" && dates "$@"
test "X$(basename -- "$0")" = "Xmdates" && mdates "$@"
test "X$(basename -- "$0")" = "Xtcomingf" && tcomingf "$@"
test "X$(basename -- "$0")" = "Xttomorrowf" && ttomorrowf "$@"
test "X$(basename -- "$0")" = "Xtcoming" && tcoming "$@"
test "X$(basename -- "$0")" = "Xtpast" && tpast "$@"
test "X$(basename -- "$0")" = "Xttomorrow" && ttomorrow "$@"
test "X$(basename -- "$0")" = "Xterms" && terms "$@"

# vim: set ft=sh ts=4 sw=4: