• Stars
    star
    252
  • Rank 161,312 (Top 4 %)
  • Language
    Clojure
  • License
    The Unlicense
  • Created about 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Canonical Colorizing Clojure Printer

Puget

Build Status Coverage Status cljdoc

Puget is a Clojure library for printing Clojure and EDN values. Under the hood, Puget formats data into print documents and uses fipp to render them.

output example

Puget offers several features which set it apart from FIPP and Clojure's native pretty-printing functions. Syntax coloring is the most widely used, followed by canonical printing. Custom value rendering is supported using type dispatch to select print handlers.

Installation

Puget releases are published on Clojars. To use the latest version with Leiningen, add the following dependency to your project definition:

Clojars Project

See Whidbey for nREPL and Leiningen integration.

Usage

Puget's printing is controlled by a map of options which configure things like print width, sorting mode, color scheme and style, whether to print metadata, and so on. The default options are held in the dynamic var puget.printer/*options*, which can be bound using with-options. See the puget.printer namespace documentation for the full set of options.

These options are used to construct a printer to render values with. pprint and pprint-str will automatically create a PrettyPrinter record from the current and passed options, or you can use pretty-printer or canonical-printer to construct one manually. render-out and render-str take a printer and a value if you need maximum control over the printing.

Syntax Coloring

Puget's first major feature is colorizing the printed data by rendering it with embedded markup. Different syntax elements are given different colors to make the printed output much easier for humans to parse. This is similar to syntax highlighting, but much easier since the code works directly with the data instead of parsing it from text!

Elements are mapped to color codes by the :color-scheme option. The :print-color option can be set to enable colorization using the with-color macro - alternately, the cprint function always prints with colored output enabled.

Puget supports three different kinds of color markup:

  • :ansi (the default) adds ANSI color escapes for terminal outputs.
  • :html-inline adds HTML span elements with inline style attributes.
  • :html-classes adds span elements with semantic class attributes.

See the puget.color.ansi namespace for the available ANSI color styles which can be applied to syntax elements.

Canonical Representation

Puget also provides canonical serialization of data. In most cases, if two data values are equal, they should be printed identically. This is important for when the printed data is hashed, but it also makes it easier to process maps and other structures with similar contents.

Puget uses the arrangement library to sort the values in sets and the keys in maps so they are always printed the same way. This can be disabled with the :sort-keys option, or enabled only for collections under a certain size.

Most printing is done with the PrettyPrinter class, but the library also offers the CanonicalPrinter for serializing data in a stricter (and more compact) fashion.

=> (require '[puget.printer :as puget])

=> (puget/pprint #{'x :a :z 3 1.0})
#{1.0 3 :a :z x}

=> (def usd (java.util.Currency/getInstance "USD"))
#'user/usd

=> (puget/pprint usd)
#<java.util.Currency@4cc4ee24 USD>

=> (puget/render-out (puget/canonical-printer) usd)
; IllegalArgumentException: No defined representation for class java.util.Currency: USD

Type Extensions

All of Clojure's primitive types are given their standard print representations. To handle non-standard data types, Puget supports a mechanism to dispatch to custom print handlers. These take precedence over the normal rendering mechanisms.

This can be used to provide an EDN tagged-literal representation for certain types, or just avoid trying to pretty-print types which the engine struggles with (such as Datomic database values).

Before rendering a value, the printer checks for a :print-handlers function. If available, it is called with the type of the value to be printed. If the lookup returns a handler, that function is called with the value and the result is used as the rendered format of the value.

The puget.dispatch namespace has functions to help build handler lookup functions. The inheritance-lookup constructor provides semantics similar to Clojure's multimethod dispatch.

As an example, extending #inst formatting to clj-time's DateTime:

=> (require '[clj-time.core :as t]
            '[clj-time.format :as f])

=> (puget/pprint (t/now))
#<org.joda.time.DateTime 2014-05-14T00:58:40.922Z>

=> (def time-handlers
     {org.joda.time.DateTime
      (puget/tagged-handler
        'inst
        (partial f/unparse (f/formatters :date-time)))})
#'user/time-handlers

=> (puget/pprint (t/now) {:print-handlers time-handlers})
#inst "2014-05-14T01:05:53.885Z"

If no handler is specified for a given type and it's not a built-in EDN type, Puget refers to the :print-fallback option, which must be one of:

  • :pretty (the default) prints a colored representation of the unknown value (not valid EDN!).
  • :print falls back to the standard pr-str representation.
  • :error throws an exception for types with no defined representation.
  • A function which will be called with the printer and the unknown value to render, returning the formatted value.

License

This is free and unencumbered software released into the public domain. See the UNLICENSE file for more information.

More Repositories

1

cljstyle

A tool for formatting Clojure code
Clojure
293
star
2

whidbey

nREPL middleware to pretty-print colored values
Clojure
158
star
3

blocks

Clojure content-addressable data storage.
Clojure
113
star
4

clj-pgp

Clojure wrapper for the Bouncy Castle OpenPGP library
Clojure
83
star
5

clj-cbor

Native Clojure CBOR codec implementation.
Clojure
70
star
6

clj-hiera

Generate Clojure namespace hierarchy graphs
Clojure
66
star
7

merkle-db

High-scalability analytics database built on immutable merkle-trees
Clojure
44
star
8

merkledag-core

Library to operate on a content-addressed graph of nodes with directed merkle-hash links
Clojure
25
star
9

vault

Content-addressable data storage system
Clojure
22
star
10

clj-arrangement

Micro-library to provide a total-ordering comparator for Clojure.
Clojure
20
star
11

clj-multiformats

Clojure(Script) implementations of the self-describing multiformat specs
Clojure
20
star
12

edn-tool

Pretty-print your EDN
Clojure
17
star
13

alphabase

A simple Clojure(script) library to encode binary data in different bases using alphabets.
Clojure
15
star
14

blocks-s3

Clojure content-addressable block store backed by Amazon S3.
Clojure
12
star
15

merkledag-ledger

Ledger financial translation to the Clojure merkledag data structure.
Clojure
11
star
16

test.carly

Generative test harness for stateful system behavior
Clojure
10
star
17

solanum

Lightweight monitoring daemon for Riemann metrics
Clojure
8
star
18

directive

Clojure DSL for declarative command-line interface construction.
Clojure
4
star
19

toolkit

Simple ruby tool to manage common user configuration and scripts.
Ruby
4
star
20

lein-cprint

Like lein-pprint, but with colors!
Clojure
4
star
21

merkledag-ref

Reference pointer tracking for MerkleDAG data graphs
Clojure
3
star
22

baton

Distributed lock management interface
Clojure
2
star
23

toolkit-packages

Public configuration and script toolkit packages.
Ruby
2
star
24

hoard

A command-line tool for archiving your important data
Clojure
2
star
25

ansible-pki-tls

Ansible role for installing a custom Certificate Authority and server certificate.
2
star
26

merkledag-browser

Clojurescript application to browse merkledag data structures.
Clojure
1
star
27

dynr53

Dynamic DNS updater script for AWS Route53.
Ruby
1
star
28

merkledag-server

Clojure server for block and merkledag data over an HTTP API
Clojure
1
star
29

monitoring-config

Ansible playbooks to configure a TRIG monitoring stack.
Clojure
1
star
30

body-app

Personal health and diet tracking app built on ClojureScript and IPFS
Clojure
1
star
31

ansible-bsd-nginx

Ansible role for installing nginx as a reverse proxy in FreeBSD.
1
star
32

inadynr53

Minimal backend to integrate inadyn and Route53
Clojure
1
star
33

dataphage

Scripts for collecting personal data from third party services.
Clojure
1
star