Liking cljdoc? Tell your friends :D


Features  •  Setup  •  Usage  •  Options  •  Theming  •  Interop  •  Contributing



Intro

Fireworks is a themeable print debugging library for Clojure, ClojureScript, and Babashka. It is designed to make your workflow easier, faster, and more enjoyable. If you like the idea of printing with formatting and syntax coloring that matches the source code in your editor, this tool may be of interest.



Features

  • Achieve thematic parity between editor, terminal emulator, and browser dev console.

  • Customizable via system-wide .edn config file.

  • Novel typographic approach for printing metadata inline, alongside values.

  • Truncation of collections and long values such as strings.

  • Left-justification of values in maps.

`. -->
  • Trace values in threading macros such as ->.

  • High or low contrast rainbow brackets (optional).

  • Makes cljs color printing possible in all dark-mode-themed browser consoles.

  • Enhanced reflection for JS and Java values.

  • Say goodbye to mysterious things like #[Object #[Object]]

  • Find and highlight values in printed output.

  • Call site options let you easily try out different themes and formatting options.

  • Ships with several popular themes in light and dark variants.



Fireworks on Clojars

Light and dark variants of Alabaster, Neutral, Monokai, Solarized, Zenburn, and Degas.



Setup

Requires Clojure 1.10.3 or higher

If using with Babashka, requires Babashka v1.3.187 or higher


Add as a dependency to your project:

[io.github.paintparty/fireworks "0.5.0"]

Import into your namespace:

(ns myns.core
  (:require
    [fireworks.core :refer [?]]))


Usage

fireworks.core/? is a macro that prints the form, namespace info, and resulting value. It returns the resulting value.

(def x {:a "foo" :xyz "bar"})

(? x)

Calling fireworks.core/? with two arguments will print a label (instead of the form), the namespace info, and the result:

(? "My label" x)

The first argument can also be a map, which supplies various config options:

(? {:label      "My label"
    :theme      "Monokai Light"
    :coll-limit 10}
   x)


Tap-driven development

Fireworks provides a bevy of print-and-return macros and functions so that you can print values from your source without altering the execution of your program. By default, the printed output that Fireworks produces is optimized for speed of comprehension. When printing data structures, the primary goal is to provide the user with a high-level snapshot of the shape and contents of the data. This is often sufficient to enable understanding at glance, and doesn't requiring the user to switch context and interact with a entirely separate UI that might involve clicking and scrolling around just to look at a single nested value.

When it is necessary to view a data structure in its entirety, or without any truncation of values, you can pass specific options at the call site, or simply just switch the macro to ?log or ?pp.

Because Fireworks is designed to provide quick, rapid feedback to the terminal or browser dev console, it complements discovery-centric tools with a dedciated UI such as FlowStorm, Reveal, or Portal.

All the publicly available functions from fireworks.core and their behaviors are outlined in the table below. Every macro/function, unless noted otherwise, returns the value passed to it.
| Name | Prints with | Prints label? | Prints file info? | Notes | | :--- | :--- | :--- | :--- | :-- | | ? | Fireworks | ✓ | ✓ | | | ?- | Fireworks | × | × | | | ?-- | Fireworks | ✓ | ✓ | Does not return a value. Intended for user commentary. | | ?i | Fireworks | × | ✓ | Omits label | | ?l | Fireworks | ✓ | × | Omits file info |
| ?log | js/console.log* | ✓ | ✓ | | | ?log- | js/console.log* | × | × | | | ?pp | pp/pprint | ✓ | ✓ | | | ?pp- | pp/pprint | × | × | | | ?trace | Fireworks | ✓ | ✓ | Traces ->, ->> some->, some->>| | p-data | × | × | × | Returns a map describing the Fireworks formatting & printing used by ?.| | ?> | × | × | × | Sends value to tap> | | !? | × | × | × | Silences printing | | !?- | × | × | × | Silences printing | | !?i | × | × | × | Silences printing | | !?l | × | × | × | Silences printing | | !?log | × | × | × | Silences printing | | !?log- | × | × | × | Silences printing | | !?pp | × | × | × | Silences printing | | !?pp- | × | × | × | Silences printing | | !?> | × | × | × | Does not send to tap> |

  • ?log and ?log will dispatch to pp/pprint in a JVM context.


All the public macros and functions from fireworks.core:

[fireworks.core :refer [? !? ?- !?- ?-- !?-- ?> !?> ?i !?i ?l !?l ?log !?log ?log- !?log- ?pp !?pp ?pp- !?pp- ?trace !?trace p-data]]

Non-existent, but on the roadmap for a future release:
?as->, ?cond->, ?cond->>, and ?comp.



Getting the formatted string & other data

If you just want the formatted string, and/or other data that fireworks.core/? uses to construct the printed output, you can use the fireworks.core/p-data macro.

Calling fireworks.core/p-data in a ClojureScript (browser) context also provides vectors of css styles. This corresponds to the arguments that js/console.log requires for syntax-colored formatting. In a terminal context, p-data returns the same map as below, but with sgr escape codes for syntax coloring (instead of the %c tags), and no vectors of css styles:

;; ClojureScript

(p-data "foo")
=>
{:quoted-form   "foo",
 :formatted     {:string     "%c\"foo\"%c",
                 :css-styles ["color:#448C27;line-height:1.45;"
                              "color:#585858;line-height:1.45;"]},
 :file          "fireworks/core_test.cljc",
 :end-column    46,
 :ns-str        "fireworks.core-test",
 :file-info-str "fireworks.core-test:17:21",
 :column        21,
 :line          17,
 :end-line      17,
 :formatted+    {:string     "%cfireworks.core-test:17:21%c\n%c\"foo\"%c %c=>%c %c\"foo\"%c",
                 :css-styles ["color:#8c8c8c;font-style:italic;line-height:1.45;"
                              "color:#585858;line-height:1.45;"
                              "color:#448C27;line-height:1.45;"
                              "color:#585858;line-height:1.45;"
                              "color:#28cc7d;line-height:1.45;"
                              "color:#585858;line-height:1.45;"
                              "color:#448C27;line-height:1.45;"
                              "color:#585858;line-height:1.45;"]}}


Options

For cutting & pasting into your system-wide config, or trying things out at the call site:

{:theme                         "Alabaster Light"
 :mood                          :light            ; :light | :dark
 :line-height                   1.45
 :print-level                   7
 :non-coll-length-limit         33
 :non-coll-mapkey-length-limit  20
 :non-coll-result-length-limit  444
 :non-coll-depth-1-length-limit 59
 :coll-limit                    15
 :evaled-form-coll-limit        7
 :display-namespaces?           true
 :metadata-print-level          7
 :display-metadata?             true
 :metadata-position             :inline           ; :inline | :block
 :enable-rainbow-brackets?      true
 :bracket-contrast              :high             ; :high | :low
 :enable-terminal-truecolor?    false
 :enable-terminal-italics?      false
 :custom-printers               nil
 :find                          nil}

System-wide config

Fireworks is designed to pick up your preferred theming and formatting options from a system-wide .edn config file that lives in a globally accessible place outside of any projects. In order to make this work, you will need to set the environment variable FIREWORKS_CONFIG to the path of this file. This .edn config file can live anywhere on your computer, but by convention should be ~/.fireworks/config.edn. If you were to set the environment variable in your .zshrc (or similar), it would look like this:

export FIREWORKS_CONFIG="/Users/your-home-folder/.fireworks/config.edn"

You will need to substitute your-home-folder in the example above with the name of your user folder on your computer. When you setup this environment variable for the first time, and you are already running a Clojure(Script) project that you aim to use Fireworks in, you will probably need restart a new session from a new terminal instance, so that your new FIREWORKS_CONFIG env var will be accessible in your dev environment.

For the actual config.edn file, you can use the above example map (at the beginning of this section) as a starting point. Prior to doing this you can experiment with the various configuration options ala-carte via passing a leading options map to any of following macros:

?
?-
?i
?l
?trace


All the options

All of the available config options and their default values:


:mood "light"
Sets the mood to "dark" or "light". Will use the default light (or dark) theme, which is "Alabaster Light" (or "Alabaster Dark"). Defaults to "light".



:theme "Alabaster Light"
Sets the theme. This will override :mood setting. This must be one of the following 3 types of values:

  • A theme name which corresponds to the theme name of an stock fireworks theme in themes/. Currently, these include the following:

    "Alabaster Light"
    "Alabaster Dark"
    "Neutral Light"
    "Neutral Dark"
    "Solarized Light"
    "Solarized Dark"
    "Degas Light"
    "Degas Dark"
    "Zenburn Light"
    "Zenburn Dark"
    "Monokai Light"
    "Monokai Dark"

  • A path pointing to an .edn file on your computer, the contents of which constitute a valid fireworks theme.
    The path must be absolute e.g. "/Users/your-home-folder/.fireworks/my-theme.edn"
    This will not work: "~/.fireworks/my-theme.edn"
    If the map in this .edn file fails to satisfy the fireworks.specs.theme/theme spec it will issue a warning and fall back to the default light or dark theme (depending on the value of :mood).

  • A valid Fireworks theme, which is a map that satisfies the fireworks.specs.theme/theme spec. Typically, its structure will at minimum resemble the first example found in the theming section of this document.


:line-height 1.45
Sets the line-height. Only takes effect in browser consoles.


:coll-limit 15
Sets the max length of collections. Collections whose count are at least 2 greater than this number will be truncated. By default, Fireworks aggressively truncates collections to keep the display footprint of the printed output as short and narrow as possible.


:print-level 7
Sets the max depth of printing for nested collections.


:non-coll-length-limit 33
Sets the max length of things like strings, keywords, function names, etc., when they are nested more than one level deep inside a data structure. Values whose length exceeds this will be ellipsized.


:non-coll-mapkey-length-limit 20
Sets the max length of things like strings, keywords, function names, etc., when they are used as keys in maps. Longer values will be ellipsized.


:non-coll-result-length-limit 444
Sets the max length of a non-collection value such as a string, keyword, function name, etc. Only applies when the value itself is the result of the evaluation (not nested within a data structure).


:non-coll-depth-1-length-limit 69
Sets the max length of a non-collection value such as a string, keyword, function name, etc. Only applies when the value is nested one level deep inside the result, which would be a non-associative collection such as a vector or seq.


:enable-rainbow-brackets? true
Whether or not to use rainbow brackets. Rainbow brackets can be customized in your theme.


:bracket-contrast "high"
Sets the level of rainbow bracket intensity to "high" or "low". Default value can also be overridden by :bracket-contrast entry in a Fireworks theme map.


:display-namespaces? true
Whether or not to print out fully qualified namespaces for functions and classes. Note that even if set to true, namespaces may get dropped if the count of fully qualified symbol exceeds the :non-coll-length-limit or the :non-coll-mapkey-length-limit (in the case of map keys).


:enable-terminal-truecolor? false
If set to false (default value), Fireworks will convert the hex color values to sgr-rgb codes (x256) for terminal emulators that do not support 24-bit color. If you will be printing with Fireworks in a terminal, and your terminal emulator supports 24-bit color (most of them do), it is highly recommended to set this to true.


:enable-terminal-italics? false
If set to false (default value), any theme tokens specified to be italicized will not be italicized. If you will be printing with Fireworks in a terminal, and your terminal emulator supports italics (most of them do), it is highly recommended to set this option to true.


:enable-terminal-font-weights? false
If set to false (default value), any theme tokens specified to be bold will not be bold. If you will be printing with Fireworks in a terminal, and your terminal emulator supports bold fonts (most of them do), it is highly recommended to set this option to true.


:metadata-print-level 6
Sets the max depth of printing for metadata maps that contain nested collections.


:display-metadata? true
Print metadata values.


:metadata-position "inline"
Determines position of metadata relative to value that is carrying it. Options are "inline" (default), or "block".


:find nil
Find and highlight values in the printed output. See Highlighting values section.


:print-with nil
Although more of an edge-case, you can pass a :print-with option at the call site if you would like to print the value using a built-in clojure core printing function. The value must be one of pr, pr-str, prn, prn-str, print, or println. If you want to print with pprint or js/console.log, use ?fireworks.core/pp or ?fireworks.core/log.

(? {:label      "My label"
    :print-with prn}
   x)


Displaying metadata

By default, Fireworks offers a unique way of printing metadata inline, next to the values which carry them. The intent of this is to spatially and stylistically decouple the metadata from the value to which it is attached. In practice, I find this formatting much faster to comprehend as compared to conventional "block" positioning of the metadata (above the carrying value), especially when working with metadata-heavy code.

For data structures, the metadata map is displayed inline, immediately following the opening bracket. This means that any collection carrying metadata will always be display multi-line, with each value on its own line. Below is an example vector of three quoted symbols:

(? ^{:a "a"} ['foo 'bar 'baz]

Here is the same vector, with the second symbol in the vector carrying metadata:

(? ^{:a "a"} ['foo (with-meta (symbol "bar") {:b "b"}) 'baz]

If you would rather print metadata in the traditional "block" position, you can set the config value of :metadata-positioning to :block:



Highlighting values in printed output

Fireworks offers a :find option which takes a map containing a :pred entry. It will highlight any matches in the printed output.

(def x [1 33 99 777 -16]

(? {:find {:pred #(= % 777)}} x)

You can also pass a custom highlighting style:

(? {:find {:pred #(= % 777)
           :style {:background-color "#a0f7fd"}}}
   [1 33 99 777 -16])

Or pass multiple preds, with different styles:

(? [1 33 99 777 -16]
   {:find [{:pred #(= % 777)}
           {:pred #(= % -16)
            :style {:background-color "#a0f7fd"}}]})


Theming

Fireworks includes a handful of popular themes. Making your own to perfectly match your current editor theme is straightforward.

If you would like to make your own theme for use with Fireworks, check out docs/example-theme.edn. Notice how any of the keys in the :classes entry will act as a variable if the same keyword is used as a value in any of the other entries within the :classes, :syntax, or :printer maps.

For your own theme, you do not need to dictate every value that is present in the :theme map within the example (docs/example-theme.edn). For example, in the default "Alabaster Light", you can see how just a small handful of the tokens are specified. Internally, this gets merged with the base light theme, which specifies how most of the other values inherit from the basic classes in :classes.

{:name    "Alabaster Light"
 :desc    "Based on @tonsky's Alabaster theme."
 :mood    :light
 :langs   ["Clojure" "ClojureScript" "Babashka"]
 :theme   {:classes {:string     {:color "#448C27"}
                     :comment    {:color "#AA3731"
                                  :font-style :italic}
                     :constant   {:color "#7A3E9D"}
                     :definition {:color "#4d6dba"}
                     :annotation {:color "#999999"}}
           :printer {:function-args {:color "#999999"}}}}

The simplest way to make a theme is to just start experimenting within any namespace in your project:

(def my-theme
 {:name "Foo Dark" ;; Required. Name validated with: #"^[A-Z][^\t\n\r]+ (:?Dark|Light)$"
  :mood "dark"     ;; Required. "light" or "dark"
  :theme {:classes {:string {:color "#c7e62e"}}
          :syntax  {:js-object-key {:color "#888888"}}
          :printer {:function-args {:color "#bb8f44"}}}})

;; And then just try it out with some kind of sample value like this:
(? {:theme my-theme}
   {:string-sample  "string"
    :number-sample  1234
    :boolean-sample true
    :lamda-sample   #(inc %)
    :fn-sample      juxt
    :regex-sample   #"^hi$"
    :symbol-sample  'mysym})

Tweak the colors to your liking, save the theme as an .edn file somewhere on your computer, then set that path as the value of the :theme entry in your .edn config.

For a token's :color or :background-color, the value must be a string which is a valid css hex(a) color. This hex will be used for both browser dev consoles and terminal consoles. Your terminal must support 24bit (TrueColor) , and you must explicitly set :enable-terminal-truecolor? to true in order for the colors to render as expected. If you are using a terminal that does not support 24bit color, such as the Terminal app on macOS, and Fireworks config option :enable-terminal-truecolor? is set to false (which is default), the specified hex color will automatically get converted to its closest x256 equivalent.



Aligning your console background color and font

Fireworks can only color the foreground and background of "spans" of text. If you want to perfectly match the themed experience of your source code editor, you will need to manually set the font-family and background color of your terminal emulator and/or browser dev console.


Setting the background color and font in Chrome DevTools (ClojureScript)

If you are using Firefox, ignore this section and follow the instructions in the following section.

First, you will need to set the general appearance of your Chrome browser's DevTools UI to "Light" or "Dark", depending on whether you are using a light or dark Fireworks theme. This can be done by opening DevTools on any page, clicking the Settings gear icon button, and then Preferences > Appearance > Theme. Official instructions here.

Chrome does not offer direct options in the UI to set the exact background color or font-family of the console in dev tools. To make this simple, I created an extension called DevTools Console Customizer, available via The Chrome Web Store. The project page is here.

After making a change with this extension, you will need to close and reopen DevTools. If you are switching from a light to dark theme or vice-versa, remember to also reset the general appearance of DevTools in Settings > Preferences > Appearance > Theme, as described above.


Setting the background color and font in Firefox Developer Tools (ClojureScript)

In Firefox, this can be done by opening Firefox Developer Tools on any page, just right-click and select Inspect. Then click on the ••• button in the top right of this panel and select Settings to get to the Settings panel. You should see a Themes section which will let you select Light or Dark.

You can customize the font-family and background color of the dev console in Firefox, although this has to be done manually. Fireworks provides a starter userContent.css file to make this easy. You will need to place this file in the correct directory on your computer so that Firefox can read it when it launches. Follow the instructions outlined here to locate this directory. Please note that file, which is necessary file for customizing the Developer Tools console in FireFox, is called userContent.css, NOT userChrome.css (as mentioned in the linked tutorial). You can put this in the proper directory as explained in the linked tutorial and change it as needed to align with your chosen theme. Remember to quit and restart Firefox if you make any changes or updates to this userContent.css file.


Setting the background color and font-family of your terminal emulator

Most terminal emulators have preferences which allow you to change, among other things, the default font-family, background color, and foreground color. Below is a list of the background colors of all the stock Fireworks themes:

"Alabaster Light" #f7f7f7
"Alabaster Dark"  #0e1415
"Neutral Light"   #ffffff
"Neutral Dark"    #000000
"Degas Light"     #f5f9f9
"Degas Dark"      #363f4e
"Zenburn Light"   #f9f8f5
"Zenburn Dark"    #3f3f3f
"Solarized Light" #fdf6e3
"Solarized Dark"  #002b36
"Monokai Light"   #ffffff
"Monokai Dark"    #2d2a2e

If you are using a custom theme with Fireworks, you will probably want to use the background color for that custom theme.


Setting the foreground color of your terminal emulator

For theming parity between your editor and terminal emulator, this is probably not as important as setting the background color as described above. However, setting the foreground color will most likely improve your experience as it will ensure that the default foreground color of all other things printed (but not formatted with fireworks) to standard out will jive with the Fireworks theme you are using. Below is a list of the foreground colors of all the stock Fireworks themes:

"Alabaster Light" #585858
"Alabaster Dark"  #cecece
"Neutral Light"   #585858
"Neutral Dark"    #cecece
"Degas Light"     #585858
"Degas Dark"      #cecece
"Zenburn Light"   #666666
"Zenburn Dark"    #cecece
"Solarized Light" #666666
"Solarized Dark"  #999999
"Monokai Light"   #585858
"Monokai Dark"    #cecece


Printing conventions

The following sections are comparisons of various default printing conventions (clojure.pprint/pprint vs js/console.log vs fireworks.core/?).


Printing maps with data structures as keys

When printing maps that contain keys which are data-structures, clojure.pprint/pprint sometimes prints these collections keys on their own line, sometimes not. The resulting printed map can be very difficult to comprehend at a glance:

;; Example map with data structures as keys

{["abcdefghijklmnopqrstuvxyz" "ABCDEFGHIJKLMNOPQRSTUVXYZ"]
 "vector",

 :b
 "keyword",

 {:a "abcdefghijklmnopqrstuvxyz", :b "ABCDEFGHIJKLMNOPQRSTUVXYZ"}
 "map", 

 "d"
 "string"

 #{1 2 3}
 "set"}

=>

;; clojure.pprint/pprint output of above map

{["abcdefghijklmnopqrstuvxyz" "ABCDEFGHIJKLMNOPQRSTUVXYZ"]
 "vector",
 :b "keyword",
 {:a "abcdefghijklmnopqrstuvxyz", :b "ABCDEFGHIJKLMNOPQRSTUVXYZ"}
 "map", 
 "d" "string"
 #{1 2 3} "set"}

Fireworks will always print these maps consistently - every key on its own line & empty line between all entries:


Printing functions in ClojureScript

A sample vector of 3 functions:

(ns sandbox.browser)

(defn ab [x y] (+ x y))

(defn abc 
  ([x y] (+ x y))
  ([x y v] (+ x y v)))

(defn my-function-with-a-really-long-name
  [x y z]
  (+ x y z))

(def to-be-printed [ab abc my-function-with-a-really-long-name])

clojure.pprint/pprint will print the above to-be-printed vector of functions like this:

[#object[sandbox$browser$ab]
 #object[sandbox$browser$abc]
 #object[sandbox$browser$my_function_with_a_really_long_name]]

js/console.log will print something like this (differs by browser console):


[ƒ sandbox$browser$ab(x,y){
  return (x + y);
 },
 ƒ sandbox$browser$abc(var_args){
  var G__901213 = arguments.length;
  switch (G__901213) {
  case 2:
  return sandbox.browser.abc.cljs$core$IFn$_invoke$arity$2((arguments[(0)]),(arguments[(1)]));
  break;
  case…,
 ƒ sandbox$browser$my_function_with_a_really_long_name(x,y,z){
  return (x + y + z);
 }

Fireworks:

By default, Fireworks will print the function name with the fully-qualified namespace. This can be disabled by changing the config option :display-namespaces? to false.

If the fully-qualified name of the function + args vector exceeds the value of :non-coll-length-limit, the args will get ellipsized, the namespace will be dropped (if necessary), and the function name will be ellipsized (if necessary).


Printing functions in Clojure

Fireworks prints functions in Clojure the same way as it does in ClojureScript, except that the named args of the function are not available to be printed in the args vector.


Printing built-in JavaScript objects and functions (ClojureScript)

Example vector of built-in JS Functions and Constructors:

(def built-ins
 [js/decodeURI
  js/isFinite
  js/EvalError
  js/Date]

clojure.pprint/pprint result:

[#object[decodeURI]
 #object[isFinite]
 #object[EvalError]
 #object[Date]]

js/console.log result:

[f decodeURI() {[native code]}
 f isFinite() {[native code]}
 f EvalError() {[native code]}
 f Date() {[native code]}]

Fireworks:


JS built-in objects such as js/Math or js/JSON which cannot be called like functions or constructors are printed like this:


Performance

Meaningless performance test: fireworks.core/? vs clojure.pprint/pprint in JVM Clojure, printing a map of a dozen entries of various data types (found at fireworks.smoke-test/basic-samples).

Hardware:  Mac Mini
Processor: 3GHz 6-Core Intel
Memory:    16GB 2667 MHz DDR4

The test was run using the excellent taoensso.tufte library:

pId         nCalls   Min   Max    Mean  MAD    Clock   Total

:fireworks  1,000    4ms   19ms   5ms   ±10%   5.20s   46%
:pprint     1,000    5ms   11ms   6ms   ±9%    6.12s   54%

Even with all the specific formatting and syntax colorization, the performance of printing the values (in this test) with Fireworks (in JVM Clojure) is on average 1.2x faster than clojure.pprint/pprint.


Status / Roadmap

Alpha, subject to change. Currently, the enhanced interop reflection / print handling is focused more on the ClojureScript side. It would be nice to add more support for native Java types and data structures. Issues welcome, see contributing.


Contributing

Issues for bugs, improvements, or features are very welcome. Please file an issue for discussion before starting or issuing a PR.


Alternatives / Prior Art

Discovery:
FlowStorm, Reveal, Portal.

Serialization:
pp, fipp, zprint.

Debugging, tracing, observability:
playback, debux, hashp, telemere, ken spyscope, omni-trace, postmortem, sayid,

Printing and visualization:
puget, coll-pen, pp-grid


License

Copyright © 2024 Jeremiah Coyle

This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version, with the GNU Classpath Exception which is available at https://www.gnu.org/software/classpath/license.html.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close