aaf09c52cad9

Fix hash table pretty printing.  Thanks, CLR.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 09 Nov 2018 21:23:48 -0500
parents 3cddaf4f6564
children edbdc9c9cb0a
branches/tags (none)
files package.lisp src/debugging.lisp

Changes

--- a/package.lisp	Wed Nov 07 18:08:28 2018 -0500
+++ b/package.lisp	Fri Nov 09 21:23:48 2018 -0500
@@ -289,10 +289,8 @@
     :dis
     :gimme
     :hex
-    :pht
     :pr
-    :print-hash-table
-    :print-hash-table-concisely
+    :pretty-print-hash-table
     :print-table
     :prl
     :shut-up
--- a/src/debugging.lisp	Wed Nov 07 18:08:28 2018 -0500
+++ b/src/debugging.lisp	Fri Nov 09 21:23:48 2018 -0500
@@ -138,58 +138,20 @@
   (values))
 
 
-(defun print-hash-table (hash-table &optional (stream t))
-  "Print a pretty representation of `hash-table` to `stream.`
-
-  Respects `*print-length*` when printing the elements.
+(defun pretty-print-hash-table (*standard-output* ht)
+  (pprint-logical-block
+      (*standard-output* (hash-table-contents ht) :prefix "{" :suffix "}")
+    (pprint-exit-if-list-exhausted)
+    (loop (destructuring-bind (k v) (pprint-pop)
+            (write k)
+            (write-string ": ")
+            (write v)
+            (pprint-exit-if-list-exhausted)
+            (write-string ", ")
+            (pprint-newline :linear)))))
 
-  "
-  (let* ((keys (hash-table-keys hash-table))
-         (vals (hash-table-values hash-table))
-         (count (hash-table-count hash-table))
-         (key-width (-<> keys
-                      (mapcar (compose #'length #'prin1-to-string) <>)
-                      (reduce #'max <> :initial-value 0)
-                      (clamp 0 20 <>))))
-    (print-unreadable-object (hash-table stream :type t)
-      (princ
-        ;; Something shits the bed and output gets jumbled (in SBCL at least) if
-        ;; we try to print to `stream` directly in the format statement inside
-        ;; `print-unreadable-object`, so instead we can just render to a string
-        ;; and `princ` that.
-        (format nil ":test ~A :count ~D {~%~{~{  ~vs ~s~}~%~}}"
-                (hash-table-test hash-table)
-                count
-                (loop
-                  :with limit = (or *print-length* 40)
-                  :for key :in keys
-                  :for val :in vals
-                  :for i :from 0 :to limit
-                  :collect
-                  (if (= i limit)
-                    (list key-width :too-many-items (list (- count i) :more))
-                    (list key-width key val))))
-        stream)))
-  (terpri stream)
-  (values))
 
-(defun pht (hash-table &optional (stream t))
-  "Synonym for `print-hash-table` for less typing at the REPL."
-  (print-hash-table hash-table stream))
-
-(defun print-hash-table-concisely (hash-table &optional (stream t))
-  "Print a concise representation of `hash-table` to `stream.`
-
-  Should respect `*print-length*` when printing the elements.
-
-  "
-  (print-unreadable-object (hash-table stream :type t)
-    (prin1 (hash-table-test hash-table))
-    (write-char #\space stream)
-    (prin1 (hash-table-contents hash-table) stream)))
-
-(defmethod print-object ((object hash-table) stream)
-  (print-hash-table-concisely object stream))
+(set-pprint-dispatch 'hash-table 'pretty-print-hash-table)
 
 
 #+sbcl