From efb0c4d1fe10c913f0906a64aeb73cb8ffd2d1b8 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Fri, 9 Sep 2022 22:30:03 +0200 Subject: [PATCH] Display unicode and markdown tables with a single function --- sncf.ss | 45 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/sncf.ss b/sncf.ss index 045a38c..226217d 100755 --- a/sncf.ss +++ b/sncf.ss @@ -5,6 +5,7 @@ (import :std/format :std/getopt :std/iter + :std/misc/string :std/net/request :std/srfi/19 :std/sugar @@ -27,8 +28,8 @@ (display-disruptions disruptions) (when mattermost-url (let ((tab-str-md (with-output-to-string (lambda () - (display-departures-table-md departures) - (display-disruptions disruptions))))) + (display-departures-table departures style: 'markdown) + (display-disruptions disruptions style: 'markdown))))) (post-to-mattermost mattermost-url tab-str-md channel: mattermost-channel))))) (catch (getopt-error? exn) (getopt-display-help exn "sncf" (current-error-port)) @@ -47,8 +48,10 @@ (request-status req) (request-status-text req))) (exit 1)))) -(def (display-departures-table departures) - (def tab (table '("Réseau" "Direction" "Heure") [10 30 13])) +(def (display-departures-table departures style: (style 'unicode)) + (def good-emoji (if (eq? style 'markdown) ":white_check_mark: " "")) + (def bad-emoji (if (eq? style 'markdown) ":warning: " "")) + (def tab (table '("Réseau" "Direction" "Heure") [10 30 (if (eq? style 'markdown) 32 13)] style)) (display (table-header tab)) (for ((dep departures)) (let* ((info (hash-ref dep (string->symbol "display_informations"))) @@ -58,40 +61,22 @@ (dep-dt-str (hash-ref stop-dt (string->symbol "departure_date_time"))) (dep-dt (string->date dep-dt-str "~Y~m~dT~H~M~S")) (hour-str (if (equal? dep-dt base-dep-dt) - (date->string dep-dt "~H:~M") - (format "~a → ~a" - (date->string base-dep-dt "~H:~M") - (date->string dep-dt "~H:~M"))))) + (str good-emoji (date->string dep-dt "~H:~M")) + (str bad-emoji (format "~a → ~a" + (date->string base-dep-dt "~H:~M") + (date->string dep-dt "~H:~M")))))) (display (table-row tab (hash-ref info 'network) (hash-ref info 'direction) hour-str)))) (display (table-footer tab))) -(def (display-departures-table-md departures) - (displayln "| Réseau | Direction | Heure |") - (displayln "|-") - (for ((dep departures)) - (let* ((info (hash-ref dep (string->symbol "display_informations"))) - (stop-dt (hash-ref dep (string->symbol "stop_date_time"))) - (base-dep-dt-str (hash-ref stop-dt (string->symbol "base_departure_date_time"))) - (base-dep-dt (string->date base-dep-dt-str "~Y~m~dT~H~M~S")) - (dep-dt-str (hash-ref stop-dt (string->symbol "departure_date_time"))) - (dep-dt (string->date dep-dt-str "~Y~m~dT~H~M~S")) - (hour-str (if (equal? dep-dt base-dep-dt) - (date->string dep-dt ":white_check_mark: ~H:~M") - (format ":warning: ~a → ~a" - (date->string base-dep-dt "~H:~M") - (date->string dep-dt "~H:~M"))))) - (displayln (format "| ~a | ~a | ~a |" - (hash-ref info 'network) - (hash-ref info 'direction) - hour-str))))) - -(def (display-disruptions disruptions) +(def (display-disruptions disruptions style: (style 'unicode)) (displayln "Perturbations :") (for ((dis disruptions)) - (display "* ") + (if (eq? style 'markdown) + (display "* ") + (display "• ")) (displayln (hash-ref (car (hash-ref dis 'messages)) 'text)))) (def (post-to-mattermost url text channel: (channel #f))