diff --git a/src/awk/update.awk b/src/awk/update.awk new file mode 100644 index 0000000..3ac88bc --- /dev/null +++ b/src/awk/update.awk @@ -0,0 +1,102 @@ +function getcontent(content_line, prop) +{ + return substr(content_line[prop], index(content_line[prop], ":") + 1); +} + +function escape(str) +{ + gsub("\\\\", "\\\\", str); + gsub(";", "\\\\;", str); + gsub(",", "\\\\,", str); +} + +function print_fold(nameparam, content, i, s) +{ + i = 74 - length(nameparam); + s = substr(content, 1, i); + print nameparam s; + s = substr(content, i+1, 73); + i = i + 73; + while (s) + { + print " " s; + s = substr(content, i+1, 73); + i = i + 73; + } +} + +BEGIN { + FS=":"; + zulu = strftime("%Y%m%dT%H%M%SZ", systime(), 1); +} + +ENDFILE { + if (NR == FNR) + { + # Sanitize input + cmd = "date +\"%R\"" + cmd | getline now + close(cmd) + # if time is set to 00:00 or right now, assume it's a date + # and not a datetime entry. + from = from ? from : "now" + cmd = "date -d \"" from "\" +\"%R\""; + cmd | getline t + close(cmd) + if (t == "00:00" || t == now) { + from_type = "DATE" + cmd = "date -d \"" from "\" +\"%Y%m%d\""; + } else { + from_type = "DATE-TIME" + cmd = "date -d \"" from "\" +\"@%s\" | xargs date -u +\"%Y%m%dT%H%M00Z\" -d" + } + cmd | getline from + close(cmd) + # + to = to ? to : "now" + cmd = "date -d \"" to "\" +\"%R\""; + cmd | getline t + close(cmd) + if (t == "00:00" || t == now) { + to_type = "DATE" + cmd = "date -d \"" to "\" +\"%Y%m%d\""; + } else { + to_type = "DATE-TIME" + cmd = "date -d \"" to "\" +\"@%s\" | xargs date -u +\"%Y%m%dT%H%M00Z\" -d" + } + cmd | getline to + close(cmd) + } + escape(summary); + escape(desc); +} + +NR == FNR && desc { desc = desc "\\n" $0; next; } +NR == FNR { + from = substr($0, 1, 6) == "::: |>" ? substr($0, 8) : ""; + getline + to = substr($0, 1, 6) == "::: <|" ? substr($0, 8) : ""; + getline + summary = substr($0, 1, 2) == "# " ? substr($0, 3) : "" + getline # This line should be empty + getline # First line of description + desc = $0; + next; +} + +/^BEGIN:VEVENT$/ { inside = 1; print; next } +/^X-ALT-DESC/ && inside { next } # drop this alternative description +/^ / && inside { next } # drop this folded line (the only content with folded lines will be updated) +/^(DTSTART|DTEND|SUMMARY|CATEGORIES|DESCRIPTION|LAST-MODIFIED)/ && inside { next } # skip for now, we will write updated fields at the end +/^SEQUENCE/ && inside { seq = $2; next } # store sequence number and skip +/^END:VEVENT$/ { + seq = seq ? seq + 1 : 1 + print "SEQUENCE:" seq + print "LAST-MODIFIED:" zulu + print "DTSTART;VALUE=" from_type ":" from + print "DTEND;VALUE=" to_type ":" to + print_fold("SUMMARY:", summary, i, s) + print_fold("DESCRIPTION:", desc, i, s) + inside = "" +} +{ print } diff --git a/src/main.sh b/src/main.sh index b49ace8..6124f1b 100755 --- a/src/main.sh +++ b/src/main.sh @@ -93,6 +93,13 @@ EOF EOF ) export AWK_GET + + AWK_UPDATE=$( + cat <<'EOF' +@@include src/awk/update.awk +EOF + ) + export AWK_UPDATE ### END OF AWK SCRIPTS ## Colors @@ -236,22 +243,31 @@ __list() { # LC_ALL=c xargs -I {} date -d "{}" +"%a %e %b %Y" } +__canonical_datetime_hm() { + s="$1" + t=$(date -d "@$s" +"%R") + dfmt="%F" + if [ "$t" != "00:00" ]; then + dfmt="$dfmt %R" + fi + date -d "@$s" +"$dfmt" +} + __canonical_datetime() { s="$1" shift t=$(date -d "@$s" +"%R") dfmt="$*%e %b %Y" if [ "$t" != "00:00" ]; then - dfmt="$dfmt %Y %R %Z" + dfmt="$dfmt %R %Z" fi date -d "@$s" +"$dfmt" } __edit() { - start=$(__canonical_datetime "$1") - end=$(__canonical_datetime "$2") + start=$(__canonical_datetime_hm "$1") + end=$(__canonical_datetime_hm "$2") fpath="$3" - # Use $start $end and $fpath summary=$(awk -v field="SUMMARY" "$AWK_GET" "$fpath") description=$(awk -v field="DESCRIPTION" "$AWK_GET" "$fpath") filetmp=$(mktemp --suffix='.md') @@ -267,12 +283,9 @@ __edit() { # Update only if changes are detected if [ "$checksum" != "$(cksum "$filetmp")" ]; then - # TODO: finish implementation - echo "going to update..." - read -r tmp - #file_new="$filetmp.ics" - #awk "$AWK_UPDATE" "$filetmp" "$file" >"$file_new" - #mv "$file_new" "$file" + filenew="$filetmp.ics" + awk "$AWK_UPDATE" "$filetmp" "$fpath" >"$filenew" + mv "$filenew" "$fpath" fi rm "$filetmp" } @@ -332,7 +345,6 @@ if [ "${1:-}" = "--day" ]; then --accept-nth='1,2,3,4' \ --preview="$0 --preview {}" \ --bind="backspace:first+accept" \ - --bind="ctrl-n:become($0 --new ${FZF_NTH:-})" \ --bind="ctrl-s:execute($SYNC_CMD ; printf 'Press to continue.'; read -r tmp)" ) hour=$(echo "$selection" | cut -d '|' -f 1) @@ -341,7 +353,6 @@ if [ "${1:-}" = "--day" ]; then fpath=$(echo "$selection" | cut -d '|' -f 4 | sed "s/ /|/g") if [ -n "$fpath" ]; then fpath="$ROOT/$fpath" - # TODO: Go on edit file __edit "$start" "$end" "$fpath" elif [ -n "$hour" ]; then # TODO: Go on add entry for hour