From 6e57b6d76c25968bb66f11a655c3bd1d20c33041 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 11 Oct 2024 19:52:05 +0200 Subject: add more time.txt scripts --- .local/share/bin/time2tsv | 107 ++++++++++++++++++++++++++++++++++++++++++++++ .local/share/bin/timerep | 75 ++++++++++++++++++++++++++++++++ .local/share/bin/timesum | 58 +------------------------ 3 files changed, 184 insertions(+), 56 deletions(-) create mode 100755 .local/share/bin/time2tsv create mode 100755 .local/share/bin/timerep diff --git a/.local/share/bin/time2tsv b/.local/share/bin/time2tsv new file mode 100755 index 0000000..4cc369d --- /dev/null +++ b/.local/share/bin/time2tsv @@ -0,0 +1,107 @@ +#!/bin/awk -f + +# ignore empty or comment lines +/^\s*#/ { next } +/^\s*$/ { next } + +{ + owner = "" + date = "" + duration = 0 + description = "" +} + +function max(a, b) { + return a > b ? a : b +} + +function duration2time(duration) { + temp = 0 + num = 0 + multiplier = 60 + + split(duration, char, "") + for (i=1; i <= length(duration); i++) { + if (char[i] == "h") { + temp += num * 60 * 60 + multiplier = 60 + num = 0 + continue + } + if (char[i] == "m") { + temp += num * 60 + multiplier = 1 + num = 0 + continue + } + if (char[i] == "s") { + temp += num + multiplier = 0 + num = 0 + continue + } + + num *= 10 + num += char[i] + } + if (num > 0) { + temp += num * multiplier + } + + return temp +} + +function stripnotes(desc) { + out = "" + nestlevel = 0 + + split(desc, char, "") + for (i=1; i <= length(desc); i++) { + if (char[i] == "(") { + nestlevel += 1 + continue + } + + if (char[i] == ")") { + nestlevel = max(0, nestlevel - 1) + continue + } + + if (nestlevel > 0) continue + out = out char[i] + } + + return out +} + +# strip leading whitespace +/^\s+/ { gsub(/^\s+/, "", $0) } + +# match owner +match($0, /^[[:alpha:]]+/) { + owner = substr($0, RSTART, RLENGTH) + gsub(/^[[:alpha:]]+:?\s*/, "", $0) +} + +# match date +match($0, /^[0-9-]{10}/) { + date = substr($0, RSTART, RLENGTH) + gsub(/^[0-9-]{10}:?\s*/, "", $0) +} + +# match time +match($0, /^\<([0-9]+h)?([0-9]{1,2}m)?([0-9]{1,2}s)?\>/) { + duration = duration2time(substr($0, RSTART, RLENGTH)) + gsub(/^\<([0-9]+h)?([0-9]{1,2}m)?([0-9]{1,2}s)?\>:?\s*/, "", $0) +} + +# description +{ + description = stripnotes($0) +} + +# output +{ + printf("%s\t%s\t%s\t%s\n", owner, date, duration, description) +} + diff --git a/.local/share/bin/timerep b/.local/share/bin/timerep new file mode 100755 index 0000000..c6b44ee --- /dev/null +++ b/.local/share/bin/timerep @@ -0,0 +1,75 @@ +#!/bin/sh +TAB="$(printf '\t')" +time2tsv "$@" | sort -t"$TAB" -k4 | awk -F"$TAB" ' +{ + gsub(/^\s+/, "", $4) + gsub(/\s+$/, "", $4) + desc = $4 + if (desc != last_desc && last_desc != "") { + printf("%s\t%d\n", last_desc, sum) + sum = 0 + } + sum += $3 + last_desc = desc +} +END { + printf("%s\t%d\n", last_desc, sum) +} +' | awk -F"$TAB" ' +BEGIN { + split("", last_path) # define empty array +} + +function strip(string) { + gsub(/^\s+/, "", string) + gsub(/\s+$/, "", string) + return string +} + +function repeat(str, count, i) { + out = "" + for (i = 0; i < count; i++) { + out = str out + } + return out +} +function indent(depth, i) { + return repeat(" ", depth) +} + +function printpath(new_path, new, i) { + new = 0 + for (i = 1; i <= length(new_path); i++) { + if (last_path[i] != new_path[i]) new = 1 + if (new == 0) continue + printf("%s%s\n", indent(i-1), new_path[i]) + last_path[i] = new_path[i] + } +} + +{ + len = split($1, path, "::") + for (i = 1; i <= len; i++) + path[i] = strip(path[i]) + + task = path[len] + delete path[len] + depth = len - 1 + sum += $2 + "timefmt "$2 | getline duration + + printpath(path) + printf("%-67s %10s\n", indent(depth) task, duration) +} + +BEGIN { + printf("\033[4m%-67s\033[0m \033[4m%10s\033[0m\n", "Description", "Total") +} + +END { + "timefmt "sum | getline duration + printf("%67s \033[4m%10s\033[0m\n", "", "") + printf("%67s %10s\n", "", duration) +} +' + diff --git a/.local/share/bin/timesum b/.local/share/bin/timesum index 448515d..6284ca4 100755 --- a/.local/share/bin/timesum +++ b/.local/share/bin/timesum @@ -1,58 +1,4 @@ #!/bin/sh -grep -o '\<\([0-9]\+h\)\?\([0-9]\{1,2\}m\)\?\([0-9]\{1,2\}s\)\?\>' "$@" |\ - awk ' -BEGIN { - sum = 0 -} - -{ - temp = 0 - num = 0 - multiplier = 60 - - split($0, char, "") - for (i=1; i <= length($0); i++) { - if (char[i] == "h") { - temp += num * 60 * 60 - multiplier = 60 - num = 0 - continue - } - if (char[i] == "m") { - temp += num * 60 - multiplier = 1 - num = 0 - continue - } - if (char[i] == "s") { - temp += num - multiplier = 0 - num = 0 - continue - } - - num *= 10 - num += char[i] - } - if (num > 0) { - temp += num * multiplier - } - - # printf("%10s -> %d\n", $0, temp) - sum += temp -} - -END { - seconds = sum % 60 - sum = (sum - seconds) / 60 - minutes = sum % 60 - sum = (sum - minutes) / 60 - hours = sum - - if (hours > 0) printf("%dh", hours) - printf("%02dm", minutes) - if (seconds > 0) printf("%02ds", seconds) - printf("\n") -} -' +TAB="$(printf '\t')" +time2tsv "$@" | cut -d"$TAB" -f3 | paste -sd+ - | bc | timefmt -- cgit v1.2.3