Skip to content

Commit

Permalink
Renames and docstring formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hlship committed Jun 23, 2023
1 parent 9a23c46 commit 4dae3c4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
11 changes: 8 additions & 3 deletions src/clj_commons/ansi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,14 @@
The span's font declaration may also be a map with the following keys:
- :font - keyword; the font declaration
- :width - number, the visual width of the span
- :pad - where to pad the span (:left or :right); defaults to :left
:font keyword
: the font declaration
:width number
: the visual width of the span
:pad keyword
: where to pad the span, :left or :right; default is :left
The map form of the font declaration is typically only used when a span width is specified.
The span will be padded with spaces to ensure that it is the specified width. `compose` tracks the number
Expand Down
15 changes: 7 additions & 8 deletions src/clj_commons/format/binary.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
(padding (- per-line line-count))
"|"))))))

(defn write-binary
(defn print-binary
"Formats a BinaryData into a hex-dump string, consisting of multiple lines; each line formatted as:
0000: 43 68 6F 6F 73 65 20 69 6D 6D 75 74 61 62 69 6C 69 74 79 2C 20 61 6E 64 20 73 65 65 20 77 68 65
Expand All @@ -105,7 +105,7 @@
A placeholder character (a space with magenta background) is used for any non-printable
character."
([data]
(write-binary data nil))
(print-binary data nil))
([data options]
(let [{show-ascii? :ascii
per-line-option :line-bytes} options
Expand All @@ -118,14 +118,13 @@
(write-line show-ascii? offset data (min per-line remaining) per-line)
(recur (long (+ per-line offset)))))))))


(defn format-binary
"Formats the data using [[write-binary]] and returns the result as a string."
([data]
(format-binary data nil))
([data options]
(with-out-str
(write-binary data options))))
(print-binary data options))))

(defn- match?
[byte-offset data-length data alternate-length alternate]
Expand Down Expand Up @@ -156,15 +155,15 @@
;; On the right/red side, we need nothing.
pad? (print " ")))))

(defn- write-delta-line
(defn- print-delta-line
[offset expected-length ^bytes expected actual-length actual]
(printf "%04X:" offset)
(write-byte-deltas :bold.bright-green true offset expected-length expected actual-length actual)
(print " |")
(write-byte-deltas :bold.bright-red false offset actual-length actual expected-length expected)
(println))

(defn write-binary-delta
(defn print-binary-delta
"Formats a hex dump of the expected data (on the left) and actual data (on the right). Bytes
that do not match are highlighted in green on the expected side, and red on the actual side.
When one side is shorter than the other, it is padded with `--` placeholders to make this
Expand All @@ -179,11 +178,11 @@
target-length (max actual-length expected-length)]
(loop [offset 0]
(when (pos? (- target-length offset))
(write-delta-line offset expected-length expected actual-length actual)
(print-delta-line offset expected-length expected actual-length actual)
(recur (long (+ bytes-per-diff-line offset)))))))

(defn format-binary-delta
"Formats the delta using [[write-binary-delta]] and returns the result as a string."
[expected actual]
(with-out-str
(write-binary-delta expected actual)))
(print-binary-delta expected actual)))
14 changes: 6 additions & 8 deletions src/clj_commons/format/exceptions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,14 @@
* A function that extracts the value from the stack frame map (typically, this is a keyword such
as :package or :name). The value is converted to a string.
* A string or regexp used for matching. Strings must match exactly.
* A resulting frame visibility (:hide, :omit, :terminate, or :show).
The default rules:
* omit everything in clojure.lang and java.lang.reflect.
* hide everything in sun.reflect
* terminate at speclj.*, clojure.main/main*, clojure.main/repl/read-eval-print, or nrepl.middleware.interruptible-eval
* omit everything in `clojure.lang` and `java.lang.reflect`.
* hide everything in `sun.reflect`
* terminate at `speclj.*`, `clojure.main/main*`, `clojure.main/repl/read-eval-print`, or `nrepl.middleware.interruptible-eval`
"
[[:package "clojure.lang" :omit]
[:package #"sun\.reflect.*" :hide]
Expand Down Expand Up @@ -677,10 +675,10 @@
([exception options]
(format-exception* (analyze-exception exception options) options)))

(defn write-exception
"Formats an exception via [[format-exception]], then writes it to `*out*`. Accepts the same options as `format-exception`."
(defn print-exception
"Formats an exception via [[format-exception]], then prints it to `*out*`. Accepts the same options as `format-exception`."
([exception]
(write-exception exception nil))
(print-exception exception nil))
([exception options]
(print (format-exception exception options))
(flush)))
Expand Down
10 changes: 5 additions & 5 deletions src/clj_commons/pretty/repl.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns clj-commons.pretty.repl
"Utilities to assist with REPL-oriented development."
(:require [clj-commons.format.exceptions :as e :refer [write-exception]]
(:require [clj-commons.format.exceptions :as e :refer [print-exception]]
[clojure.main :as main]
[clojure.repl :as repl]
[clojure.stacktrace :as st])
Expand All @@ -13,7 +13,7 @@
(defn pretty-repl-caught
"A replacement for `clojure.main/repl-caught` that prints the exception to `*err*`, without a stack trace or properties."
[e]
(write-exception e {:frame-limit 0 :properties false}))
(print-exception e {:frame-limit 0 :properties false}))

(defn uncaught-exception-handler
"Returns a reified UncaughtExceptionHandler that prints the formatted exception to `*err*`."
Expand All @@ -33,18 +33,18 @@
([] (pretty-pst *e))
([e-or-depth]
(if (instance? Throwable e-or-depth)
(write-exception e-or-depth nil)
(print-exception e-or-depth nil)
(pretty-pst *e e-or-depth)))
([e depth]
(binding [*out* *err*]
(write-exception e {:frame-limit depth}))))
(print-exception e {:frame-limit depth}))))

(defn pretty-print-stack-trace
"Replacement for `clojure.stacktrace/print-stack-trace` and `print-cause-trace`. These functions are used by `clojure.test`."
([tr] (pretty-print-stack-trace tr nil))
([tr n]
(println)
(write-exception tr {:frame-limit n})))
(print-exception tr {:frame-limit n})))

(defn install-pretty-exceptions
"Installs an override that outputs pretty exceptions when caught by the main REPL loop. Also, overrides
Expand Down
4 changes: 2 additions & 2 deletions test/demo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
(pst (make-exception))
(println "\nTesting reporting of repeats:")
(try (countdown 20)
(catch Throwable t (e/write-exception t))))
(catch Throwable t (e/print-exception t))))

(comment

Expand All @@ -83,7 +83,7 @@
(thread
(<!! ch)
(println (str "Thread #" i))
(e/write-exception e)))
(e/print-exception e)))
(close! ch))

(repl/install-pretty-exceptions)
Expand Down

0 comments on commit 4dae3c4

Please sign in to comment.