dot

packages and services management
Log | Files | Refs | README

commit c1f1ccbc8068e333380a2e7e2cc205b7773228da
parent b1bfe7f1c0b096d69461a2f38ae47d4885d743a6
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sat, 23 Dec 2017 00:13:43 +0100

calendar already exist

Diffstat:
Abin/agenda | 193+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dbin/calendar | 193-------------------------------------------------------------------------------
2 files changed, 193 insertions(+), 193 deletions(-)

diff --git a/bin/agenda b/bin/agenda @@ -0,0 +1,193 @@ +#!/bin/sh +# plain text agenda reading ical with its own simpler format + +AGENDA="$HOME/var/cal" + +awk_date=' +function leap(yrs) +{ + return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0); +} + +function days_per_month(mth, yrs) +{ + if (mth == 2) + return 28 + leap(yrs); + else + return 30 + (mth - (mth > 7)) % 2; +} + +function to_sec(yrs, mth, day, hrs, min, sec) +{ + while (--mth >= 1) + day += days_per_month(mth, yrs); + while (--yrs >= 1970) + day += 365 + leap(yrs); + return (((((day - 1) * 24) + hrs) * 60) + min) * 60 + sec; +} + +function to_date(fmt, sec) +{ + for (yrs = 1970; sec >= (s = 3600 * 24 * (365 + leap(yrs))); yrs++) + sec -= s; + for (mth = 1; sec >= (s = 3600 * 24 * days_per_month(mth, yrs)); mth++) + sec -= s; + for (day = 1; sec >= (s = 3600 * 24); day++) + sec -= s; + for (hrs = 0; sec >= 3600; hrs++) + sec -= 3600; + for (min = 0; sec >= 60; min++) + sec -= 60; + return sprintf(fmt, yrs, mth, day, hrs, min, sec); +}' + +awk_import=$awk_date'#OFF=[+-]HH +function ical_date(str, off) { + yrs = substr(str, 1, 4); + mth = substr(str, 5, 2); + day = substr(str, 7, 2); + hrs = substr(str, 10, 2); + min = substr(str, 12, 2); + return to_sec(yrs, mth, day, hrs, min, 0) - off * 3600; +} + +BEGIN { + FS = "[:;]"; +} + +{ + gsub("\r", ""); + gsub("\t", " "); +} + +/BEGIN:VEVENT/,/END:VEVENT/ { if (substr($0, 1, 1) == " ") { + event[type] = event[type] substr($0, 2, length($0) - 1); + } else { + type = $1; + i = index($0, ":"); + event[type] = substr($0, i + 1, length($0) - i); + } +} + +/END:VEVENT/ { + gsub("[ \t]*<[a-zA-Z0-9/]*>*[ \t]*", "", event["DESCRIPTION"]); + gsub("\\\\n", "\nD:", event["DESCRIPTION"]); + printf("B:%d\nE:%d\nS:%s\nC:%s\nL:%s\nD:%s\n:\n", + ical_date(event["DTSTART"], OFF), ical_date(event["DTEND"], OFF), + event["SUMMARY"], event["CATEGORIES"], event["LOCATION"], + event["DESCRIPTION"]); +}' + +awk_sort='#SORT=[BESCL] +BEGIN { + FS=":" + sort = "sort | cut -f " length(SORT) + 2 "-"; +} + +/^B:/ { + nb = 1; + line["B"] = line["E"] = line["S"] = line["C"] = line["L"] = line["D"] = ""; +} + +/^[BESCLD]:/ { + line[nb++] = sprintf("%08d\t%s", NR, $0); + line[$1] = substr($0, 3); +} + +/^:$/ { + line[nb++] = sprintf("%08d\t%s", NR, $0); + prefix = ""; + for (i = length(SORT); i > 0; i--) + prefix = line[substr(SORT, i, 1)] "\t" prefix; + for (i = 1; i < nb; i++) + printf("%s%s\n", prefix, line[i]) | sort; +}' + +awk_show=$awk_date'#OFF=[+-]HH +function fold(str) +{ + str = str " "; + while ((l = substr(str, 1, 66)) != "") { + sub(" +[^ ]*$", "", l); + printf(" | %s\n", l); + str = substr(str, length(l) + 1); + sub("^ *", "", str); + } +} + +BEGIN { + FS = ":"; +} + +/^B:/ { + line["B"] = line["E"] = line["S"] = ""; + line["C"] = line["L"] = line["D"] = ""; +} + +/^[BESCLD]:/ { + if (line[$1] == "") + line[$1] = substr($0, 3); + else + line[$1] = line[$1] " " substr($0, 3); +} + +/^:$/ { + beg = to_date("%04d/%02d/%02d %02d:%02d", line["B"] + OFF * 3600); + end = to_date("%04d/%02d/%02d %02d:%02d", line["E"] + OFF * 3600); + beg_mth = substr(beg, 1, 7); + beg_day = substr(beg, 9, 2); + beg_h_m = substr(beg, 12); + end_h_m = substr(end, 12); + printf("%s\n%2s %s %s\n %2s [%s] %s\n", + (beg_mth != last_mth) ? ("\n\n[" beg_mth "]\n") : "", + (beg_day != last_day) ? (beg_day) : (""), + beg_h_m, line["S"], end_h_m, line["C"], line["L"]); + fold(line["D"]); + last_mth = beg_mth; + last_day = beg_day; +}' + +awk_to_sec=$awk_date'#DATE=YYYY-MM-DD-HH-MM #OFF=[+-]HH +BEGIN { + yrs = substr(DATE, 1, 4); + mth = substr(DATE, 6, 2); + day = substr(DATE, 9, 2); + hrs = substr(DATE, 12, 2); + min = substr(DATE, 15, 2); + print(to_sec(yrs, mth, day, hrs, min, 0) - OFF * 3600); +}' + +mkdir -p "$AGENDA" + +case $1 in +(import) + [ $# -ne 4 ] && exec "$0" + awk -v OFF="$4" "$awk_import" "$2" > "$AGENDA/$3" + ;; +(show) + [ $# -lt 2 ] || [ $# -gt 3 ] && exec "$0" + awk -v SORT="${3:-BECLS}" "$awk_sort" "$AGENDA/$2" | + awk -v OFF="$(date +%z | cut -c 1-3)" "$awk_show" + ;; +(add) + [ $# -ne 4 ] && exec "$0" + off=$(date +%z | cut -c -3) + printf 'B:%s\nE:%s\nS:\nC:\nL:\nD:\n:\n' >> "$AGENDA/$2" \ + "$(awk -v DATE="$3" -v OFF="$off" "$awk_to_sec")" \ + "$(awk -v DATE="$4" -v OFF="$off" "$awk_to_sec")" + exec "$0" edit "$2" + ;; +(edit) + [ $# -ne 2 ] && exec "$0" + exec $EDITOR "$AGENDA/$2" + ;; +(*) + printf 'usage: agenda import ics_file name [+-]HH\n' + printf ' agenda show name [BESCL]\n' + printf ' agenda add name YYYY-MM-DD-HH-MM YYYY-MM-DD-HH-MM\n' + printf ' agenda edit name\n' + printf 'B: Begin - E: End - S: Summary - C: Category - L: Location\n' + printf '\n' + ls -C "$AGENDA" + ;; +esac diff --git a/bin/calendar b/bin/calendar @@ -1,193 +0,0 @@ -#!/bin/sh -# awk functions to deal with calendar - -CALENDAR="$HOME/var/cal" - -awk_date=' -function leap(yrs) -{ - return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0); -} - -function days_per_month(mth, yrs) -{ - if (mth == 2) - return 28 + leap(yrs); - else - return 30 + (mth - (mth > 7)) % 2; -} - -function to_sec(yrs, mth, day, hrs, min, sec) -{ - while (--mth >= 1) - day += days_per_month(mth, yrs); - while (--yrs >= 1970) - day += 365 + leap(yrs); - return (((((day - 1) * 24) + hrs) * 60) + min) * 60 + sec; -} - -function to_date(fmt, sec) -{ - for (yrs = 1970; sec >= (s = 3600 * 24 * (365 + leap(yrs))); yrs++) - sec -= s; - for (mth = 1; sec >= (s = 3600 * 24 * days_per_month(mth, yrs)); mth++) - sec -= s; - for (day = 1; sec >= (s = 3600 * 24); day++) - sec -= s; - for (hrs = 0; sec >= 3600; hrs++) - sec -= 3600; - for (min = 0; sec >= 60; min++) - sec -= 60; - return sprintf(fmt, yrs, mth, day, hrs, min, sec); -}' - -awk_import=$awk_date'#OFF=[+-]HH -function ical_date(str, off) { - yrs = substr(str, 1, 4); - mth = substr(str, 5, 2); - day = substr(str, 7, 2); - hrs = substr(str, 10, 2); - min = substr(str, 12, 2); - return to_sec(yrs, mth, day, hrs, min, 0) - off * 3600; -} - -BEGIN { - FS = "[:;]"; -} - -{ - gsub("\r", ""); - gsub("\t", " "); -} - -/BEGIN:VEVENT/,/END:VEVENT/ { if (substr($0, 1, 1) == " ") { - event[type] = event[type] substr($0, 2, length($0) - 1); - } else { - type = $1; - i = index($0, ":"); - event[type] = substr($0, i + 1, length($0) - i); - } -} - -/END:VEVENT/ { - gsub("[ \t]*<[a-zA-Z0-9/]*>*[ \t]*", "", event["DESCRIPTION"]); - gsub("\\\\n", "\nD:", event["DESCRIPTION"]); - printf("B:%d\nE:%d\nS:%s\nC:%s\nL:%s\nD:%s\n:\n", - ical_date(event["DTSTART"], OFF), ical_date(event["DTEND"], OFF), - event["SUMMARY"], event["CATEGORIES"], event["LOCATION"], - event["DESCRIPTION"]); -}' - -awk_sort='#SORT=[BESCL] -BEGIN { - FS=":" - sort = "sort | cut -f " length(SORT) + 2 "-"; -} - -/^B:/ { - nb = 1; - line["B"] = line["E"] = line["S"] = line["C"] = line["L"] = line["D"] = ""; -} - -/^[BESCLD]:/ { - line[nb++] = sprintf("%08d\t%s", NR, $0); - line[$1] = substr($0, 3); -} - -/^:$/ { - line[nb++] = sprintf("%08d\t%s", NR, $0); - prefix = ""; - for (i = length(SORT); i > 0; i--) - prefix = line[substr(SORT, i, 1)] "\t" prefix; - for (i = 1; i < nb; i++) - printf("%s%s\n", prefix, line[i]) | sort; -}' - -awk_show=$awk_date'#OFF=[+-]HH -function fold(str) -{ - str = str " "; - while ((l = substr(str, 1, 66)) != "") { - sub(" +[^ ]*$", "", l); - printf(" | %s\n", l); - str = substr(str, length(l) + 1); - sub("^ *", "", str); - } -} - -BEGIN { - FS = ":"; -} - -/^B:/ { - line["B"] = line["E"] = line["S"] = ""; - line["C"] = line["L"] = line["D"] = ""; -} - -/^[BESCLD]:/ { - if (line[$1] == "") - line[$1] = substr($0, 3); - else - line[$1] = line[$1] " " substr($0, 3); -} - -/^:$/ { - beg = to_date("%04d/%02d/%02d %02d:%02d", line["B"] + OFF * 3600); - end = to_date("%04d/%02d/%02d %02d:%02d", line["E"] + OFF * 3600); - beg_mth = substr(beg, 1, 7); - beg_day = substr(beg, 9, 2); - beg_h_m = substr(beg, 12); - end_h_m = substr(end, 12); - printf("%s\n%2s %s %s\n %2s [%s] %s\n", - (beg_mth != last_mth) ? ("\n\n[" beg_mth "]\n") : "", - (beg_day != last_day) ? (beg_day) : (""), - beg_h_m, line["S"], end_h_m, line["C"], line["L"]); - fold(line["D"]); - last_mth = beg_mth; - last_day = beg_day; -}' - -awk_to_sec=$awk_date'#DATE=YYYY-MM-DD-HH-MM #OFF=[+-]HH -BEGIN { - yrs = substr(DATE, 1, 4); - mth = substr(DATE, 6, 2); - day = substr(DATE, 9, 2); - hrs = substr(DATE, 12, 2); - min = substr(DATE, 15, 2); - print(to_sec(yrs, mth, day, hrs, min, 0) - OFF * 3600); -}' - -mkdir -p "$CALENDAR" - -case $1 in -(import) - [ $# -ne 4 ] && exec "$0" - awk -v OFF="$4" "$awk_import" "$2" > "$CALENDAR/$3" - ;; -(show) - [ $# -lt 2 ] || [ $# -gt 3 ] && exec "$0" - awk -v SORT="${3:-BECLS}" "$awk_sort" "$CALENDAR/$2" | - awk -v OFF="$(date +%z | cut -c 1-3)" "$awk_show" - ;; -(add) - [ $# -ne 4 ] && exec "$0" - off=$(date +%z | cut -c -3) - printf 'B:%s\nE:%s\nS:\nC:\nL:\nD:\n:\n' >> "$CALENDAR/$2" \ - "$(awk -v DATE="$3" -v OFF="$off" "$awk_to_sec")" \ - "$(awk -v DATE="$4" -v OFF="$off" "$awk_to_sec")" - exec "$0" edit "$2" - ;; -(edit) - [ $# -ne 2 ] && exec "$0" - exec $EDITOR "$CALENDAR/$2" - ;; -(*) - printf 'usage: calendar import ics_file name [+-]HH\n' - printf ' calendar show name [BESCL]\n' - printf ' calendar add name YYYY-MM-DD-HH-MM YYYY-MM-DD-HH-MM\n' - printf ' calendar edit name\n' - printf 'B: Begin - E: End - S: Summary - C: Category - L: Location\n' - printf '\n' - ls -C "$CALENDAR" - ;; -esac