dot

packages and services management
Log | Files | Refs | README

commit 5c0b5bc3d2937635752932288c37a2b3e0b18cab
parent 20376f2385e11940ae332b3f61c61f80d2e7e36d
Author: Josuah Demangeon <mail@josuah.net>
Date:   Fri, 22 Dec 2017 21:20:04 +0100

fixed calendar by comparing with date(1) output

Diffstat:
Mbin/calendar | 74+++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mbin/twt | 32++++++++++++--------------------
Mbin/xdg-open | 2+-
3 files changed, 60 insertions(+), 48 deletions(-)

diff --git a/bin/calendar b/bin/calendar @@ -4,19 +4,17 @@ CALENDAR="$HOME/var/cal" awk_date=' -function bissextile(yrs) +function leap(yrs) { - return yrs % 4 == 0 && (yrs % 100 != 0 || yrs % 400 == 0); + return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0); } function days_per_month(mth, yrs) { if (mth == 2) - return 28 + bissextile(yrs); - else if (mth < 7) - return 30 + mth % 2; + return 28 + leap(yrs); else - return 30 + mth % 2; + return 30 + (mth - (mth > 7)) % 2; } function to_sec(yrs, mth, day, hrs, min, sec) @@ -24,30 +22,24 @@ function to_sec(yrs, mth, day, hrs, min, sec) for (mth--; mth > 0; mth--) day += days_per_month(mth, yrs); for (yrs--; yrs >= 1970; yrs--) - day += 365 + bissextile(yrs); + day += 365 + leap(yrs); return sec + 60 * (min + 60 * (hrs + (24 * day))); } function to_date(fmt, sec) { - b = bissextile(1970); - for (yrs = 1970; sec >= 3600 * 24 * (365 + b); yrs++) { - b = bissextile(yrs); - sec -= 3600 * 24 * (365 + bissextile(yrs)); - } - d = days_per_month(1); - for (mth = 1; sec >= 3600 * 24 * d; mth++) { - d = days_per_month(mth); - sec -= 3600 * 24 * d; - } - for (day = 1; sec >= 3600 * 24; day++) - sec -= 3600 * 24; + 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) { @@ -79,7 +71,7 @@ BEGIN { } /END:VEVENT/ { - gsub(" *<[a-zA-Z0-9/]*> *", "", event["DESCRIPTION"]); + 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), @@ -115,20 +107,22 @@ BEGIN { awk_show=$awk_date'#OFF=[+-]HH function fold(str) { + str = str " "; while ((l = substr(str, 1, 66)) != "") { sub(" +[^ ]*$", "", l); - str = substr(str, length(l) + 1); printf(" | %s\n", l); - sub(" *", "", str); + str = substr(str, length(l) + 1); + sub("^ *", "", str); } } BEGIN { - FS = ":" + FS = ":"; } /^B:/ { - line["B"] = line["E"] = line["S"] = line["C"] = line["L"] = line["D"] = ""; + line["B"] = line["E"] = line["S"] = ""; + line["C"] = line["L"] = line["D"] = ""; } /^[BESCLD]:/ { @@ -154,6 +148,16 @@ BEGIN { 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 @@ -162,13 +166,29 @@ case $1 in awk -v OFF="$4" "$awk_import" "$2" > "$CALENDAR/$3" ;; (show) - [ $# -ne 2 ] && [ $# -ne 3 ] && exec "$0" + [ $# -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\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 diff --git a/bin/twt b/bin/twt @@ -6,20 +6,18 @@ FEED=$HOME/srv/twtxt.txt CONF=$HOME/.config/twt CACHE=$HOME/.cache/twt -awk_line=' -function bissextile(yrs) +awk_date=' +function leap(yrs) { - return yrs % 4 == 0 && (yrs % 100 != 0 || yrs % 400 == 0); + return (yrs % 4 == 0) && (yrs % 100 != 0) || (yrs % 400 == 0); } function days_per_month(mth, yrs) { if (mth == 2) - return 28 + bissextile(yrs); - else if (mth < 7) - return 30 + mth % 2; + return 28 + leap(yrs); else - return 30 + mth % 2; + return 30 + (mth - (mth > 7)) % 2; } function to_sec(yrs, mth, day, hrs, min, sec) @@ -27,24 +25,18 @@ function to_sec(yrs, mth, day, hrs, min, sec) for (mth--; mth > 0; mth--) day += days_per_month(mth, yrs); for (yrs--; yrs >= 1970; yrs--) - day += 365 + bissextile(yrs); + day += 365 + leap(yrs); return sec + 60 * (min + 60 * (hrs + (24 * day))); } function to_date(fmt, sec) { - b = bissextile(1970); - for (yrs = 1970; sec >= 3600 * 24 * (365 + b); yrs++) { - b = bissextile(yrs); - sec -= 3600 * 24 * (365 + bissextile(yrs)); - } - d = days_per_month(1); - for (mth = 1; sec >= 3600 * 24 * d; mth++) { - d = days_per_month(mth); - sec -= 3600 * 24 * d; - } - for (day = 1; sec >= 3600 * 24; day++) - sec -= 3600 * 24; + 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++) diff --git a/bin/xdg-open b/bin/xdg-open @@ -129,7 +129,7 @@ case $scheme in address=${1#*://} address=${address%*:} [ "$port" = . ] && port=22 - exec sftp -p "$port" "$address" + exec sftp -P "$port" "$address" ;; (ssh) address=${1#*://}