Display unicode and markdown tables with a single function
This commit is contained in:
parent
d6d18ba43f
commit
efb0c4d1fe
1 changed files with 15 additions and 30 deletions
45
sncf.ss
45
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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue