Skip to content
README.md 2.57 KiB
Newer Older
Dimitri Enns's avatar
Dimitri Enns committed
# termin_cal
Dimitri Enns's avatar
Dimitri Enns committed
A simple bash function to add an appointment to a default calendar using khal.


```bash
function newcal(){
	function usage(){
		echo "USAGE: newcal --date [dd.mm|dd] --time [00:15|00] --title \"HEADLINE TITLE\" --notes \"Your Notes\""
		return 1
	}
	
	if [[ -z $1 ]]; then
		usage
		return 1
	fi
	
	## khal printcalendars
	local PCAL="personal" # Personal calendar
	local ALARMS="20minutes,10minutes,5minutes" # Reminders
	local TITLE_PREFIX="WhatEver Prefix // "
	local DEFAULT_DUR="60" # Default appointment duration

	local KHAL=$(which khal)
	local VDIR=$(which vdirsyncer)
	if [[ -z ${KHAL} || ! -e ${KHAL} ]] || [[ -z ${VDIR} || ! -e ${VDIR} ]]; then
		echo "Please install and configure KHAL or VDIRSYNCER first!"
		usage
	fi

	while [[ $# -ge 1 ]]
	do
		INPUT="$1"
		case $INPUT in
			--date)
				DATE="$2"
				shift
				shift;;
			--time)
				TIME="$2"
				shift
				shift;;
			--title)
				TITLE="$2"
				shift
				shift;;
			--notes)
				NOTES="$2"
				shift
				shift;;
			*)
				echo "Wrong input: $INPUT"
				return 1
			;;
		esac
	done

	## if date is only two digits, we assume the given day of the current month
	if [[ ! -z ${DATE} ]] && [[ ${DATE} =~ ^[0-9]{1,2}$ ]]; then
		local DATE="${DATE}.$(date +%m)"
	elif [[ -z ${DATE} ]]; then
		usage
		return 1
	fi

	## if time is only two digits, we assume full hour
	if [[ ! -z ${TIME} ]] && [[ ${TIME} =~ ^[0-9]{1,2}$ ]]; then
		local TIME="${TIME}:00"
	elif [[ -z ${TIME} ]]; then
		usage
		return 1
	fi

	## No title, no appointment
	if [[ -z ${TITLE} ]]; then
		usage
		return 1
	else
		local TITLE="${TITLE_PREFIX}${TITLE}"
	fi

	## If alarms are empty, dont add an alarm
	if [[ ! -z ${ALARMS} ]]; then
		ALARM_SWITCH="--alarms ${ALARMS}"
	else
		ALARM_SWITCH=""
	fi
	
	if [[ ! -z ${NOTES} ]]; then
		local TITLE="${TITLE} :: ${NOTES}"
	fi
	local END_TIME=$(date -d "${TIME} ${DEFAULT_DUR}min" +%R)

	if [[ -z ${PCAL} || -z ${DATE} || -z ${TIME} || -z ${END_TIME} || -z ${TITLE}  ]]; then
		usage
		return 1
	else
		## Add to personal calender
		khal new --calendar ${PCAL} ${DATE}. ${TIME} ${END_TIME} ${ALARM_SWITCH} "${TITLE}" 1> /dev/null
		local ERR=$?
		if [[ ${ERR} != "0" ]]; then
			echo "There was an error. KHAL exited with code ${ERR}"
			return ${ERR}
		else
			echo -e $'\u2714'" Appointment added
- Title: ${TITLE}
- Date: ${DATE} From: ${TIME} to: ${END_TIME}
- Calendar: ${PCAL}
"
		fi
		sleep 1
		vdirsyncer sync 1> /dev/null
		local ERR=$?
		if [[ ${ERR} != "0" ]]; then
			echo "There was an error. vdirsyncer exited with code ${ERR}"
			return ${ERR}
		else
			echo $'\u2714'" Updated Calendars... "
		fi
		return 0
	fi
}
```