commit 49995f3cc58e3045f462862a2b30de75ffbbf9a9
parent f1dcb8abc192d5a6d095139c516f7cf048542607
Author: Josuah Demangeon <mail@josuah.net>
Date: Sun, 11 Mar 2018 19:50:46 +0100
WIP conversion of agent to full awk
Diffstat:
2 files changed, 118 insertions(+), 125 deletions(-)
diff --git a/bin/.agenda.swp b/bin/.agenda.swp
Binary files differ.
diff --git a/bin/agenda b/bin/agenda
@@ -1,11 +1,7 @@
-#!/bin/sh
+#!/usr/bin/awk -f
# plain text agenda reading ical with its own simpler format
-AGENDA="${AGENDA:-$HOME/.config/agenda}"
-
-awk='
-
function leap(yrs)
{
return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0);
@@ -89,21 +85,117 @@ function sort(array, beg, end)
sort(array, beg, a - 1); # sort lower half
sort(array, a, end); # sort higher half
-}'
+}
-import="$awk"'
-#OFF [+-]HH
+function view(line, nb)
+{
+ sort(line, 1, nb);
+ for (i = 1; i < nb; i++) {
+ sub("([^\t]*\t){" length(SORT) + 1 "}", "", line[i]);
-BEGIN {
- FS = "[:;]";
+ if (line[i] ~ /^B:/) {
+ line["B"] = line["E"] = line["S"] = "";
+ line["C"] = line["L"] = line["D"] = "";
+
+ } else if (line[i] ~ /^[BESCLD]:/) {
+ if (line[$1] == "")
+ line[$1] = substr($0, 3);
+ else
+ line[$1] = line[$1] " " substr($0, 3);
+ } else if (line[i] ~ /:$/) {
+ display(line, i);
+ }
+ }
}
+function display(line, i, off) {
+ beg = to_date("%04d/%02d/%02d %02d:%02d", line["B"] + off * 3600);
+ end = to_date("%04d/%02d/%02d %02d:%02d", line["E"] + off * 3600);
+ b_mth = substr(beg, 1, 7);
+ b_day = substr(beg, 9, 2);
+ e_day = substr(end, 9, 2);
+ b_h_m = substr(beg, 12);
+ e_h_m = substr(end, 12);
+ printf("%s\n%2s %2s %s\n%2s %2s [%s] %s\n",
+ (b_mth != last_mth) ? ("\n[" b_mth "]\n") : "",
+ (b_day != last_day) ? (b_day) : (""), b_h_m, line["S"],
+ (b_day != e_day) ? (e_day) : (""), e_h_m, line["C"], line["L"]);
+ str = line["D"] " ";
+ while ((l = substr(str, 1, 66)) != "") {
+ sub(" +[^ ]*$", "", l);
+ printf(" | %s\n", l);
+ str = substr(str, length(l) + 1);
+ sub("^ *", "", str);
+ }
+ last_mth = b_mth;
+ last_day = b_day;
+}
+
+function usage()
{
+ print("usage: agenda -a <name> YYYY/MM/DD HH:MM [YYYY/MM/DD] HH:MM");
+ print(" agenda -i <file.ics> <name> [+-]HH");
+ print(" agenda [<name> [BESCL]]");
+ exit(1);
+}
+
+function add(argc, argv)
+{
+ file = AGENDA "/" argv[2];
+ date = argv[3] "T" argv[4];
+ if (argc == 4)
+ date2 = argv[3] "T" argv[4];
+ else if (argc == 5)
+ date2 = argv[3] "T" argv[5];
+ else if (argc == 6)
+ date2 = argv[5] "T" argv[6];
+ else
+ usage();
+
+ printf("B:%d\nE:%d\nS:\nC:\nL:\nD:\n:\n",
+ date_iso8601(date1), date_iso8601(date2)) >> file;
+
+ system(ENVIRON["EDITOR"] " '" file "'");
+ exit(0);
+}
+
+BEGIN {
+ AGENDA = ENVIRON["AGENDA"] ?
+ ENVIRON["AGENDA"] :
+ ENVIRON["HOME"] "/.config/agenda";
+
+ system("mkdir -p '" AGENDA "'");
+
+ "date +%z" | getline OFF;
+ close("date +%z");
+ OFF = substr(OFF, 1, 3);
+
+ if (ARGV[1] == "-i") {
+ IMPORT = 1;
+ FS = "[:;]";
+ file = ARGV[2];
+ } else if (ARGV[1] == "-i") {
+ add(ARGC, ARGV);
+ } else if (system("test -f '" AGENDA "/" ARGV[1] "'") == 0) {
+ VIEW = 1;
+ FS = ":";
+ SORT = ARGV[2] ? ARGV[2] : "BECLS";
+ file = AGENDA "/" ARGV[1]
+ } else {
+ usage();
+ }
+
+ ARGV[1] = file;
+ ARGC = 1;
+}
+
+IMPORT {
gsub("\r", "");
gsub("\t", " ");
}
-/BEGIN:VEVENT/,/END:VEVENT/ { if (substr($0, 1, 1) == " ") {
+IMPORT && /BEGIN:VEVENT/,/END:VEVENT/ {
+ if (substr($0, 1, 1) == " ") {
event[type] = event[type] substr($0, 2, length($0) - 1);
} else {
type = $1;
@@ -112,7 +204,7 @@ BEGIN {
}
}
-/END:VEVENT/ {
+IMPORT && /END:VEVENT/ {
gsub("[ \t]*<[a-zA-Z0-9/]*>*[ \t]*", "", event["DESCRIPTION"]);
gsub("\\\\n", "\nD:", event["DESCRIPTION"]);
gsub("\\\\", "", event["DESCRIPTION"]);
@@ -121,28 +213,20 @@ BEGIN {
date_ical(event["DTSTART"], OFF), date_ical(event["DTEND"], OFF),
event["SUMMARY"], event["CATEGORIES"], event["LOCATION"],
event["DESCRIPTION"]);
-}'
-
-sort="$awk"'
-#SORT [BESCL]
-
-BEGIN {
- FS = ":";
- sortcmd = "cut -f " length(SORT) + 2 "-";
}
-fn == "" && /^B:/ {
+VIEW && /^B:/ {
prev = nb + 1;
line["B"] = line["E"] = line["S"] = "";
line["C"] = line["L"] = line["D"] = "";
}
-fn == "" && /^[BESCLD]:/ {
+VIEW && /^[BESCLD]:/ {
line[++nb] = sprintf("%08d\t%s", NR, $0);
line[$1] = substr($0, 3);
}
-fn == "" && /^:$/ {
+VIEW && /^:$/ {
prefix = "";
line[++nb] = sprintf("%08d\t:", NR);
for (i = length(SORT); i > 0; i--)
@@ -151,109 +235,18 @@ fn == "" && /^:$/ {
line[i] = sprintf("%s%s", prefix, line[i]);
}
-END {
- sort(line, 1, nb);
- for (i = 1; i < nb; i++) {
- sub("([^\t]*\t){" length(SORT) + 1 "}", "", line[i]);
- printf("%s\n", line[i])
- }
-}'
-
-view="$awk"'
-#OFF [+-]HH
-
-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);
- b_mth = substr(beg, 1, 7);
- b_day = substr(beg, 9, 2);
- e_day = substr(end, 9, 2);
- b_h_m = substr(beg, 12);
- e_h_m = substr(end, 12);
- printf("%s\n%2s %2s %s\n%2s %2s [%s] %s\n",
- (b_mth != last_mth) ? ("\n[" b_mth "]\n") : "",
- (b_day != last_day) ? (b_day) : (""), b_h_m, line["S"],
- (b_day != e_day) ? (e_day) : (""), e_h_m, line["C"], line["L"]);
- str = line["D"] " ";
- while ((l = substr(str, 1, 66)) != "") {
- sub(" +[^ ]*$", "", l);
- printf(" | %s\n", l);
- str = substr(str, length(l) + 1);
- sub("^ *", "", str);
- }
- last_mth = b_mth;
- last_day = b_day;
-}'
-
-add="$awk"'
-#DT1 YYYY/MM/DD-HH:MM
-#DT2 YYYY/MM/DD-HH:MM
-#OFF [+-]HH
-
-BEGIN {
- printf("B:%d\nE:%d\nS:\nC:\nL:\nD:\n:\n",
- date_iso8601(DT1), date_iso8601(DT2));
-}'
-
-list="$awk"'
-
-sub("^B:", "") {
+LIST && sub("^B:", "") {
minimum = ($0 < minimum || minimum == 0) ? $0 : minimum;
}
END {
- name = FILENAME;
- sub(".*/", "", name);
- printf("%s - %-10s %s\n",
- to_date("%d/%02d/%02d", minimum + 3600), name, FILENAME);
-}'
-
-mkdir -p "$AGENDA"
-
-case "$1" in
-(-i)
- [ "$#" = 4 ] || exec "$0" -h
- awk -v OFF="$4" "$import" "$2" > "$AGENDA/$3"
- ;;
-(-a)
- [ "$#" -le 3 ] || [ "$#" -ge 6 ] && exec "$0" -h
- [ "$#" = 4 ] && set -- "$1" "$2" "$3" "$4" "$3" "$4"
- [ "$#" = 5 ] && set -- "$1" "$2" "$3" "$4" "$3" "$5"
- off="$(date +%z | cut -c -3)"
- awk -v OFF="$off" -v DT1="$3-$4" -v DT2="$5-$6" "$add" >> "$AGENDA/$2"
- exec $EDITOR "$AGENDA/$2"
- ;;
-(all)
- cd "$AGENDA"
- awk -v SORT="${2:-BECLS}" "$sort" * |
- awk -v OFF="$(date +%z | cut -c 1-3)" "$view"
- ;;
-('')
- find "$AGENDA" ! -type d ! -name '*.txt' -exec awk "$list" {} \; | sort
- ;;
-(*)
- [ "$#" = 1 ] || [ "$#" = 2 ] && [ -f "$AGENDA/$1" ] || exec cat << EOF
-usage: agenda -a <name> YYYY/MM/DD HH:MM [YYYY/MM/DD] HH:MM
- agenda -i <file.ics> <name> [+-]HH
- agenda [<name> [BESCL]]
-EOF
- awk -v SORT="${2:-BECLS}" "$sort" "$AGENDA/$1" |
- awk -v OFF="$(date +%z | cut -c 1-3)" "$view"
- ;;
-esac
+ if (VIEW) {
+ view(line, nb);
+ } else if (IMPORT) {
+ } else if (LIST) {
+ name = FILENAME;
+ sub(".*/", "", name);
+ printf("%s - %-10s %s\n",
+ to_date("%d/%02d/%02d", minimum + 3600), name, FILENAME);
+ }
+}