npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

packed-printer

v0.3.0

Published

Compact pretty printing

Downloads

8

Readme

packed-printer

A Clojure and Clojurescript library to pretty print data in a packed manner.

This printer likes rectangular shapes (the wider the better) and despises staircases.

It's an adaptation of [minimum raggedness](https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_raggedness -- used by TeX for non-justified text) to data.

Packed printer in one sentence

Usage

Coordinates [net.cgrand/packed-printer "0.3.0"].

(require '[net.cgrand.packed-printer :refer [pprint]])

Relaxed mode:

=> (pprint (partition 10 (range 50)) :width 15)
;...5....0....5
((0 1 2 3 4
  5 6 7 8 9)
 (10 11 12 13 14
  15 16 17 18 19)
 (20 21 22 23 24
  25 26 27 28 29)
 (30 31 32 33 34
  35 36 37 38 39)
 (40 41 42 43 44
  45 46 47 48 49))

Strict mode:

=> (pprint (partition 10 (range 50)) :width 15 :strict true)
;...5....0....5
((0 1 2 3 4
  5 6 7 8 9)
 (10 11 12
  13 14 15 16
  17 18 19)
 (20 21 22
  23 24 25 26
  27 28 29)
 (30 31 32
  33 34 35 36
  37 38 39)
 (40 41 42
  43 44 45 46
  47 48 49))

On code (not intended for that):

=> (pprint '(defn render [lines]
     (doseq [{:keys [indent] [head & spans] :spans} lines]
       (prsp indent) (prsp (leading-spaces head true)) (print (str head))
       (doseq [span spans]  (prsp (leading-spaces span false)) (print (str span)))
       (newline)))
     :coll-indents {"(" 2})
(defn render [lines]
  (doseq [{:keys [indent],  [head & spans] :spans} lines]
    (prsp indent) (prsp (leading-spaces head true)) (print (str head))
    (doseq [span spans] (prsp (leading-spaces span false))
      (print (str span)))
    (newline)))

Values are indented when they can't be set on the same line:

=> (pprint '{:foo :bar :baz :quux} :width 10)
{:foo :bar,
 :baz :quux}
=> (pprint '{:foo :bar :baz :quux} :width 6)
{:foo
   :bar,
 :baz
   :quux}

Comparison

clojure.pprint

=> (binding [clojure.pprint/*print-right-margin* 30]
     (clojure.pprint/pprint {:a :b :c {:e :f :g :h :i :j :k :l} :m :n :o {:p {:q :r :s :t}}}))

;        1    1    2    2    3
;...5....0....5....0....5....0
{:a :b,
 :c
 {:e :f,
  :g :h,
  :i :j,
  :k :l},
 :m :n,
 :o {:p {:q :r, :s :t}}}

zprint

=> (czprint {:a :b :c {:e :f :g :h :i :j :k :l} :m :n :o {:p {:q :r :s :t}}} 30 {:map {:nl-separator? true}})

;        1    1    2    2    3
;...5....0....5....0....5....0
{:a :b,
 :c {:e :f,
     :g :h,
     :i :j,
     :k :l},
 :m :n,
 :o {:p {:q :r, :s :t}}}

packed-printer

=> (pprint {:a :b :c {:e :f :g :h :i :j :k :l} :m :n :o {:p {:q :r :s :t}}} :width 30)

;        1    1    2    2    3
;...5....0....5....0....5....0
{:a :b, :c {:e :f, :g :h,
            :i :j, :k :l},
 :m :n, :o {:p {:q :r, :s :t}}}

And in strict mode:

=> (pprint {:a :b :c {:e :f :g :h :i :j :k :l} :m :n :o {:p {:q :r :s :t}}} :width 30 :strict true)

;        1    1    2    2    3
;...5....0....5....0....5....0
{:a :b, :c {:e :f, :g :h,
            :i :j, :k :l},
 :m :n, :o {:p {:q :r,
                :s :t}}}

And in between:

=> (pprint {:a :b :c {:e :f :g :h :i :j :k :l} :m :n :o {:p {:q :r :s :t}}} :width 30 :strict 1)

;        1    1    2    2    3
;...5....0....5....0....5....0
{:a :b, :c {:e :f, :g :h, :i :j, :k :l},
 :m :n, :o {:p {:q :r, :s :t}}}

Stages

There are three stages: spans (which converts data into spans), layout (which finds the optimal layout), and render (which turns the layout in side effects).

The main stage in layout and is fixed. core/spans and core/render are multimethods and thus can be extended. Dispatch occurs based on the values of options :to (the target, defaults is :text and :as the input format (default :edn). core/spans dispatches on the pair [to as] while core/render dispatches only on to.

core/spans

It must returns a sequences of spans (see protocol core/Span).

core/render

It takes a sequence of lines, a line being a map with keys :indent (the amount of whitespace by which to indent the line) and :spans a sequence of spans.

When :to is :text, spans must implement the text/Text protocol.

Implementation

Complexity is O(n * w^3) where n is the length (in spans) of the data to layout and w is the desired width.

The w^3 may be frightening but it's just an upper bound: dynamic programming is used, giving a cache size of O(n * w^3) but in practice this cache is very sparsely populated. Binding core/*print-stats* to true causes the actual value for w^3 to be printed out.

Minimum raggedness

The usual algorithm found for minimum raggedness is O(n^2) and uses dynamic programming.

However if you are laying out things on a grid whose number of columns is low then O(n*w) is possible. The key insight is that the cost of a layout depends only of the position in the list of words/spans to layout and the position in the current line -- hence the n*w.

Adaptation for data

To layout data, two parameters are added: current indentation and next line indentation. Both are bounded by w. Well, they are bounded by w only when :strict true.

In relaxed mode, how far in the margin can things be printed is bounded in practice by the cost function. Making the cost function more punitive (increasing strictness) will result in less margin prints.

License

Copyright © 2017 Christophe Grand

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.