site-josuah

/usr/josuah
Log | Files | Refs

commit 5f22f6ce9c4f95b913c7b60ac753e215ea878566
parent eda125852e36b04a31391542b2316098bb88adc6
Author: Josuah Demangeon <me@josuah.net>
Date:   Wed, 24 Jun 2020 20:26:24 +0200

wiki/awk: add fold_line implementation

Diffstat:
Mwiki/awk/index.md | 37++++++++++++++++++++++++++++++++++++-
Mwiki/jj/jj-join | 12+++++++++---
Mwiki/jj/jj-log | 121++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mwiki/jj/jj-tail | 12++++++++++++
4 files changed, 115 insertions(+), 67 deletions(-)

diff --git a/wiki/awk/index.md b/wiki/awk/index.md @@ -241,6 +241,41 @@ user 0m 3.00s sys 0m 0.10s ``` +A fold_line() function +----------------------- +Convenient to work with text documents or emails. This version does not +truncate very long words, such as some https:// links. + +``` +function fold_line(str, len, + head, tail, i) +{ + head = substr(str, 1, len + 1) + sub(" *$", "", head) + if (length(head) == len + 1) + sub(" *[^ ]*$", "", head) + if (length(head) == 0) { + tail = substr(str, len + 1) + head = substr(str, 1, len) + if ((i = index(tail, " ")) == 0) + return str + return head substr(tail, 1, i) + } + return head +} +``` + +To use it, call it in a while loop like this: + +``` +{ + while (line = fold_line($0, 72)) { + print line + $0 = substr($0, length(line) + 2) + } +} +``` + A gmtime() function ------------------- POSIX awk as well as many implementations lack the [time functions][tf] present in @@ -316,7 +351,7 @@ function localtime(sec, tm, close("date +%z") h = substr(tz, 2, 2) m = substr(tz, 4, 2) - TZOFFSET = substr(date, 1, 1) (h * 3600 + m * 60) + TZOFFSET = substr(tz, 1, 1) (h * 3600 + m * 60) } return gmtime(sec + TZOFFSET, tm) } diff --git a/wiki/jj/jj-join b/wiki/jj/jj-join @@ -1,9 +1,15 @@ #!/bin/sh -e # re-join all channels for which there is a log -IRC_HOST=${IRC_HOST:-$1} -IRC_HOST=${IRC_HOST:?} -IRC_DIR=${IRC_DIR:-/var/irc} +: ${IRC_HOST:=$1} +: ${IRC_DIR:=/var/irc} + +if [ -z "$IRC_HOST" ]; then + cd "$IRC_DIR" + ext=0 + for host in *; do IRC_HOST=$host "$0" & done + exit +fi cd "$IRC_DIR/$IRC_HOST" [ -p "in" ] || exit 1 diff --git a/wiki/jj/jj-log b/wiki/jj/jj-log @@ -10,10 +10,6 @@ function mdays(mon, year) return (mon == 2) ? (28 + isleap(year)) : (30 + (mon + (mon > 7)) % 2) } -# Split the time in seconds since epoch into a table, with fields named -# as with gmtime(3): tm["year"], tm["mon"], tm["mday"], tm["hour"], -# tm["min"], tm["sec"]. - function gmtime(sec, tm) { tm["year"] = 1970 @@ -62,29 +58,21 @@ function localtime(sec, tm, return gmtime(sec + TZOFFSET, tm) } -function parse_msg(s, msg, - nickend, text) -{ - nickend = index($2, ">") - localtime($1, TM) - msg["type"] = substr($2, nickend + 1) - msg["text"] = substr($0, index($0, ">") + 2 + length(msg["type"])) - msg["nick"] = substr($2, 2, nickend - 2) - msg["date"] = sprintf("%04d/%02d/%02d", TM["year"], TM["mon"], TM["mday"]) - msg["time"] = sprintf("%02d:%02d", TM["hour"], TM["min"]) -} - -function parse_bold(s, - tail) +function fold_line(s, len, + head, tail, i) { - tail = s - s = "" - while (match(tail, "\002[^\002]*\002")) { - s = s substr(tail, 1, RSTART - 1) "\033[1m" - s = s substr(tail, RSTART + 1, RLENGTH - 2) "\033[m" - tail = substr(tail, RSTART + RLENGTH) + head = substr(s, 1, len + 1) + sub(" *$", "", head) + if (length(head) == len + 1) + sub(" *[^ ]*$", "", head) + if (length(head) == 0) { + tail = substr(s, len + 1) + head = substr(s, 1, len) + if ((i = index(tail, " ")) == 0) + return s + return head substr(tail, 1, i) } - return s tail + return head } BEGIN { @@ -95,62 +83,69 @@ BEGIN { next } -# Handle tail's "==> filename <==" headers. - $1 == "==>" { sub(".log <==", "") sub("/channels/", " ") sub("/server$", " server") sub(".*/", "") sub(" ", "/") - printf("\n== %s ==", $0) + printf("\n\n%s", $0) fflush() + prev["nick"] = "" next } +NF < 2 { + printf("\n%s", $0) + fflush() + next +} + { - if (NF < 2) { - printf("\n%s", $0) - fflush() - next - } + nickend = index($2, ">") + localtime($1, tm) + msg["type"] = substr($2, nickend + 1) + msg["text"] = substr($0, index($0, ">") + 2 + length(msg["type"])) + msg["nick"] = substr($2, 2, nickend - 2) + msg["date"] = sprintf("%04d/%02d/%02d", tm["year"], tm["mon"], tm["mday"]) + msg["time"] = sprintf("%02d:%02d", tm["hour"], tm["min"]) +} - parse_msg(s, msg) - msg["text"] = parse_bold(msg["text"]) +index(msg["type"], "!") { + printf("\a") + color = "1;33" +} - # Display date only when it actually changed. - if (LASTDATE != msg["date"]) { - LASTDATE = msg["date"] - printf("\n\n-- " msg["date"] " --") - } +index(msg["type"], "a") { + msg["text"] = msg["nick"] " " msg["text"] + msg["nick"] = "*" +} - # Set the date color for highlights. - if (index(msg["type"], "!")) { - printf("\a") - dcolor = "33" - } else { - dcolor = "38;5;8" - } +msg["nick"] == prev["nick"] { + nick = (msg["nick"] == "-") ? ("-") : ("") +} - # Status message. - if (msg["nick"] == "-") { - printf("\n\033[%sm%s \033[%sm%s\033[0m", - dcolor, msg["time"], "38;5;8", msg["text"]) - fflush() - next - } +msg["nick"] != prev["nick"] { + nick = msg["nick"] + print "" +} - ncolor = (index(msg["type"], "*") ? "37" : "31") +prev["date"] != msg["date"] { + printf("\n%s", msg["date"]) +} - # Handle ACTION. - if (index(msg["type"], "a")) { - msg["text"] = msg["nick"] " " msg["text"] - msg["nick"] = "*" +{ + fold = fold_line(msg["text"], 66) + sp = (length(nick) > 10) ? ("") : (" ") + printf("\n\033[%sm%5s\033[m %10s %s%s", + color, msg["time"], nick, sp, fold) + color = "" + while (msg["text"] = substr(msg["text"], length(fold) + 2)) { + fold = fold_line(msg["text"], 66) + printf("\n%5s %10s %s", "", "", fold) } - - printf("\n\033[%sm%s\033[0m \033[%sm%10s\033[0m: %s", - dcolor, msg["time"], ncolor, msg["nick"], msg["text"]) fflush() - delete msg + for (i in msg) + prev[i] = msg[i] } diff --git a/wiki/jj/jj-tail b/wiki/jj/jj-tail @@ -1,6 +1,18 @@ #!/bin/sh -e # tail all jj channels in /var/irc +case "$1" in (-[0-9]*) + export MIN="$(awk -v now="$(date +%s)" -v time="${1#-}" 'BEGIN { + while (match(time, /^[0-9]+[dhms]/)) { + t[substr(time, RLENGTH, 1)] = substr(time, 1, RLENGTH - 1) + time = substr(time, RLENGTH + 1) + } + print(now - t["d"]*86400 - t["h"]*3600 - t["m"]*60 - t["s"]) + }')" + shift + ;; +esac + # build a list of (-path "") to give to find(1) [ "$#" = 0 ] && set -- "" for x in "$@"; do