cleaned awk scripts, str escape bugfix, proper use of local variables

This commit is contained in:
2025-06-16 11:04:32 +02:00
parent ee02a7647b
commit 83beaa3ad5
12 changed files with 401 additions and 262 deletions

View File

@@ -1,20 +1,69 @@
# $s|$e|$starttime|$endtime|$fpath|$collection|$description
## src/awk/dayview.awk
## Generate the view of a day from lines of the form
## ```
## <start_date>|<end_date>|<start_time>|<end_time>|<file_path>|<collection>|<description>
## ```.
##
## @assign today: Date of `today` in the format %D (%m/%d/%y)
## @assign daystart: Hour of start of the day
## @assign dayend: Hour of end of the day
# Functions
# allday
# Return line for all-day event.
#
# @input collection: Collection symbol
# @input desc: Event description
# @return: Single-line string
function allday(collection, desc) {
return collection " " ITALIC FAINT " (allday) " OFF desc
}
# endstoday
# Return line for multi-day event, or event that starts at midnight, which ends today.
#
# @input stop: Time at which the event ends
# @input collection: Collection symbol
# @input desc: Event description
# @return: Single-line string
function endstoday(stop, collection, desc) {
return collection " " CYAN " -- " stop OFF ": " desc
}
# slice
# Return line for event that starts sometime today.
#
# @input start: Time at which the event starts
# @input stop: Time at which the event ends
# @input collection: Collection symbol
# @input desc: Event description
# @return: Single-line string
function slice(start, stop, collection, desc) {
if (stop == "00:00")
return collection " " CYAN start " -- " OFF ": " desc
else
return collection " " CYAN start OFF " -- " CYAN stop OFF ": " desc
}
# hrline
# Print line for a single hour entry.
#
# @input hour: Hour of the entry
function hrline(hour) {
hour = hour < 10 ? "0"hour : hour
print today, hour, "", "", "", " " FAINT hour ":00 ----------------------" OFF
}
# hrlines
# Print lines for hour entries before an event that starts at `start` and stops
# at `stop`.
#
# @local variables: starth, stoph, tmp, i
# @input start: Time at which the event starts
# @input stop: Time at which the event ends
# @input h: Last event-free hour
# @return: Hour of now last event-free hour
function hrlines(start, stop, h, starth, stoph, tmp, i) {
starth = substr(start, 1, 2)
stoph = substr(stop, 1, 2)
@@ -27,11 +76,10 @@ function hrlines(start, stop, h, starth, stoph, tmp, i) {
else
return stoph + tmp
}
# AWK program
BEGIN {
FS = "|"
GREEN = "\033[1;32m"
RED = "\033[1;31m"
WHITE = "\033[1;97m"
CYAN = "\033[1;36m"
ITALIC = "\033[3m"
FAINT = "\033[2m"
@@ -41,9 +89,7 @@ BEGIN {
$1 == "00:00" && $2 == "00:00" { print today, $1, $3, $4, $5, allday($6, $7); next }
$1 == "00:00" { print today, $1, $3, $4, $5, endstoday($2, $6, $7); next }
$1 ~ /^[0-9]{2}:[0-9]{2}$/ {
daystart = hrlines($1, $2, daystart, starth, stoph, tmp, i)
daystart = hrlines($1, $2, daystart)
print today, $1, $3, $4, $5, slice($1, $2, $6, $7)
}
END {
hrlines(dayend":00", 0, daystart, starth, stoph, tmp, i)
}
END { hrlines(dayend":00", 0, daystart) }