diff options
Diffstat (limited to 'fun/π°οΈ')
-rw-r--r-- | fun/π°οΈ/OWNERS | 3 | ||||
-rw-r--r-- | fun/π°οΈ/bin.lisp | 91 | ||||
-rw-r--r-- | fun/π°οΈ/default.nix | 43 | ||||
-rw-r--r-- | fun/π°οΈ/lib.lisp | 32 |
4 files changed, 169 insertions, 0 deletions
diff --git a/fun/π°οΈ/OWNERS b/fun/π°οΈ/OWNERS new file mode 100644 index 000000000000..f16dd105d761 --- /dev/null +++ b/fun/π°οΈ/OWNERS @@ -0,0 +1,3 @@ +inherited: true +owners: + - sterni diff --git a/fun/π°οΈ/bin.lisp b/fun/π°οΈ/bin.lisp new file mode 100644 index 000000000000..1f5a2b2e0518 --- /dev/null +++ b/fun/π°οΈ/bin.lisp @@ -0,0 +1,91 @@ +(defpackage π°οΈ.bin + (:shadow :describe) + (:use :cl :opts) + (:import-from :uiop :quit) + (:import-from :local-time + :now :timestamp-subtimezone :+utc-zone+ + :*default-timezone* :define-timezone) + (:import-from :klatre :format-dottime-offset) + (:import-from :π°οΈ :β) + (:export :π)) + +(in-package :π°οΈ.bin) +(declaim (optimize (safety 3))) + +(opts:define-opts + (:name :help + :description "Print this help text" + :short #\h + :long "help") + (:name :dot-time + :description "Use pseudo dot-time format (implies -u)" + :short #\d + :long "dot-time") + (:name :utc + :description "Display time in UTC instead of local time" + :short #\u + :long "utc") + (:name :no-newline + :description "Don't print a trailing newline" + :short #\n + :long "no-newline")) + +(defun make-slash-terminated (str) + (if (eq (char str (1- (length str))) #\/) + str + (concatenate 'string str "/"))) + +; TODO(sterni): upstream this into local-time +(defun setup-default-timezone () + (let* ((tz (remove #\: (uiop:getenv "TZ") :count 1)) + (tz-dir (uiop:getenv "TZDIR")) + (tz-file (if (and tz tz-dir) + (merge-pathnames + (pathname tz) + (pathname (make-slash-terminated tz-dir))) + (pathname "/etc/localtime")))) + (handler-case + (define-timezone *default-timezone* tz-file :load t) + (t () (setf *default-timezone* +utc-zone+))))) + + +(defun π () + (let ((ts (now))) + (multiple-value-bind (options free-args) + (handler-case (opts:get-opts) + ; only handle subset of conditions that can happen here + (opts:unknown-option (c) + (format t "error: unknown option ~s~%" (opts:option c)) + (quit 100))) + + ; check if we have any free args we don't know what to do with + (when (> (length free-args) 0) + (write-string "error: unexpected command line argument(s): ") + (loop for arg in free-args + do (progn (write-string arg) (write-char #\space))) + (write-char #\newline) + (quit 100)) + + ; print help and exit + (when (getf options :help) + (opts:describe :usage-of "π°οΈ") + (quit 0)) + + ; reinit *default-timezone* as it is cached from compilation + (setup-default-timezone) + ; dot-time implies UTC, naturally + (when (getf options :dot-time) + (setf (getf options :utc) t)) + ; print clock face + (format t "~A" (β ts (if (getf options :utc) + local-time:+utc-zone+ + local-time:*default-timezone*))) + ; render dot-time offset if necessary + (when (getf options :dot-time) + (multiple-value-bind (offset-secs _dst _name) + (timestamp-subtimezone ts local-time:*default-timezone*) + (write-string + (format-dottime-offset (round (/ offset-secs 3600)))))) + ; write newline if necessary + (when (not (getf options :no-newline)) + (write-char #\newline))))) diff --git a/fun/π°οΈ/default.nix b/fun/π°οΈ/default.nix new file mode 100644 index 000000000000..230d9f02f137 --- /dev/null +++ b/fun/π°οΈ/default.nix @@ -0,0 +1,43 @@ +{ depot, ... }: + +let + inherit (depot.nix) + buildLisp + ; + + lib = buildLisp.library { + name = "libπ°οΈ"; + deps = [ + depot.third_party.lisp.local-time + ]; + + srcs = [ + ./lib.lisp + ]; + }; + + bin = buildLisp.program { + name = "π°οΈ"; + deps = [ + depot.third_party.lisp.unix-opts + depot.lisp.klatre + { + default = buildLisp.bundled "asdf"; + sbcl = buildLisp.bundled "uiop"; + } + lib + ]; + + srcs = [ + ./bin.lisp + ]; + + main = "π°οΈ.bin:π"; + + brokenOn = [ + "ecl" # refuses to create non-ASCII paths even on POSIXβ¦ + ]; + }; +in bin // { + inherit lib; +} diff --git a/fun/π°οΈ/lib.lisp b/fun/π°οΈ/lib.lisp new file mode 100644 index 000000000000..d23db033743e --- /dev/null +++ b/fun/π°οΈ/lib.lisp @@ -0,0 +1,32 @@ +(defpackage π°οΈ + (:use :cl) + (:import-from :local-time + :timestamp-subtimezone :*default-timezone* :sec-of) + (:export :β)) + +(in-package :π°οΈ) +(declaim (optimize (safety 3))) + +(defparameter *clock-emojis* + (vector #\π #\π§ ; 00:00 - 00:30 + #\π #\π ; 01:00 - 01:30 + #\π #\π ; 00:00 - 00:30 + #\π #\π ; 00:00 - 00:30 + #\π #\π ; 00:00 - 00:30 + #\π #\π ; 00:00 - 00:30 + #\π #\π‘ ; 00:00 - 00:30 + #\π #\π’ ; 00:00 - 00:30 + #\π #\π£ ; 00:00 - 00:30 + #\π #\π€ ; 00:00 - 00:30 + #\π #\π₯ ; 00:00 - 00:30 + #\π #\π¦)) ; 11:00 - 11:30 + +(defun β (timestamp &optional (tz *default-timezone*)) + "Convert a LOCAL-TIME:TIMESTAMP into the nearest Unicode clock face. + Use TZ (which defaults to LOCAL-TIME:*DEFAULT-TIMEZONE*) to determine + the UTC offset to factor when determining the local clock face." + (let* ((offset (multiple-value-bind (offset-secs _dst _name) + (timestamp-subtimezone timestamp tz) + offset-secs)) + (secs (+ (sec-of timestamp) offset))) + (elt *clock-emojis* (mod (round (/ secs 1800)) 24)))) |