From cae35fec418b5d7c80acd699ae9c3a15bca368c6 Mon Sep 17 00:00:00 2001
From: tv <tv@krebsco.de>
Date: Sun, 29 Jan 2023 13:11:55 +0100
Subject: tv q: simplify calendar

---
 tv/5pkgs/simple/q/default.nix | 65 +++++++++++++------------------------------
 1 file changed, 20 insertions(+), 45 deletions(-)

diff --git a/tv/5pkgs/simple/q/default.nix b/tv/5pkgs/simple/q/default.nix
index 2ae71db..eb8462d 100644
--- a/tv/5pkgs/simple/q/default.nix
+++ b/tv/5pkgs/simple/q/default.nix
@@ -10,53 +10,28 @@ let
     hspace = 2;
 
     # Return number of columns required to print n calenders side by side.
-    need_width = n:
-      assert n >= 1;
-      n * calwidth + (n - 1) * hspace;
-
-    pad = /* sh */ ''{
-      ${pkgs.gnused}/bin/sed '
-            # rtrim
-            s/ *$//
-
-            # delete last empty line
-            ''${/^$/d}
-          ' \
-        | ${pkgs.gawk}/bin/awk '{printf "%-${toString calwidth}s\n", $0}' \
-        | ${pkgs.gnused}/bin/sed '
-              # colorize header
-              1,2s/.*/&/
-
-              # colorize week number
-              s/^[ 1-9][0-9]/&/
-            '
-    }'';
+    need_width = n: assert n >= 1; n * calwidth + (n - 1) * hspace;
+
   in /* sh */ ''
     cols=$(${pkgs.ncurses}/bin/tput cols)
-    ${pkgs.coreutils}/bin/paste \
-        <(if test $cols -ge ${toString (need_width 3)}; then
-          ${pkgs.utillinux}/bin/cal -mw \
-              $(${pkgs.coreutils}/bin/date +'%m %Y' -d 'last month') \
-            | ${pad}
-        fi) \
-        <(if test $cols -ge ${toString (need_width 1)}; then
-          ${pkgs.utillinux}/bin/cal -mw \
-            | ${pkgs.gnused}/bin/sed '
-                # colorize day of month
-                s/\(^\| \)'"$(${pkgs.coreutils}/bin/date +%e)"'\>/&/
-              ' \
-            | ${pad}
-        fi) \
-        <(if test $cols -ge ${toString (need_width 2)}; then
-          ${pkgs.utillinux}/bin/cal -mw \
-              $(${pkgs.coreutils}/bin/date +'%m %Y' -d 'next month') \
-            | ${pad}
-        fi) \
-      | ${pkgs.gnused}/bin/sed '
-          s/^\t//
-          s/\t$//
-          s/\t/${lpad hspace " " ""}/g
-        '
+    if test $cols -ge ${toString (need_width 3)}; then
+      ${pkgs.utillinux}/bin/cal --color=always -mw3
+    elif test $cols -ge ${toString (need_width 2)}; then
+      ${pkgs.utillinux}/bin/cal --color=always -mw -n 2
+    elif test $cols -ge ${toString (need_width 1)}; then
+      ${pkgs.utillinux}/bin/cal --color=always -mw1
+    else
+      :
+    fi |
+    ${pkgs.gnused}/bin/sed -r '
+      # dim week numbers
+      s/((^ *|  )[ 1-5][0-9](   *)?)(([ 1-3][0-9])*)/\1\4/g
+      # dim month and day names
+      s/^ *[A-Z].*/&/
+      # highlight current date
+      s/\[7m//
+      s/\[27m//
+    '
   '';
 
   q-isodate = /* sh */ ''
-- 
cgit v1.2.3