Skip to content

Commit

Permalink
Merge branch 'tagged-literals'
Browse files Browse the repository at this point in the history
  • Loading branch information
greglook committed Mar 11, 2015
2 parents 5de74e4 + 9de90f7 commit 4f805cd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 31 deletions.
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,51 @@ TL;DR: pretty-print colored REPL values by default!
The easiest way to use Whidbey is as a Leiningen plugin. Note that this requires
Leiningen version 2.4.2 or higher for functionality in REPLy 0.3.1.

To pretty-print all values with [Puget](https://github.com/greglook/puget), add
the following to your `user`, `system`, or `repl` profile:

[![Clojars Project](http://clojars.org/mvxcvi/whidbey/latest-version.svg)](http://clojars.org/mvxcvi/whidbey)

To pretty-print all values with [Puget](https://github.com/greglook/puget) (the
main motivation of this project), you can add the following in your `user`,
`system`, or `repl` profile:
### Configuration

```clojure
:plugins [[mvxcvi/whidbey "0.5.1"]]
Whidbey passes rendering options into Puget from the `:whidbey` key in the
profile map:

; customize printing options:
```clojure
:whidbey {:width 180
:map-delimiter ""
:extend-notation true
:print-meta true
:color-scheme {:delimiter [:blue]
:tag [:bold :red]
...}}
...}
...}
```

See the Puget
[`*options*`](https://github.com/greglook/puget/blob/master/src/puget/printer.clj)
var for the available configuration.

Additionally, Whidbey adds some convenience tagged-literal extensions for binary
data and URIs. The extensions update the `default-data-readers` var to support
round-tripping the tagged representations:

```clojure
=> (java.net.URI. "http://github.com/greglook")
#whidbey/uri "http://github.com/greglook"

=> (.getBytes "foo bar baz")
#whidbey/bin "Zm9vIGJhciBiYXo="

=> #whidbey/bin "b25lIG1vcmUgdGltZSwgbXVzaWNzIGdvdCBtZSBmZWVsaW5nIHNvIGZyZWU="
#whidbey/bin "b25lIG1vcmUgdGltZSwgbXVzaWNzIGdvdCBtZSBmZWVsaW5nIHNvIGZyZWU="
```

This is controlled by the `:extend-notation` option, which defaults to `true`.
You can disable the extensions by setting it to `false`, or for more selective
control you can specify a collection of keywords to enable. The keys match the
tag names, so currently `:bin` and `:uri` are valid.

### Troubleshooting

Sometimes, there are types which Puget has trouble rendering. These can be
Expand All @@ -54,31 +77,14 @@ rendering. If you want to use these types' `print-method` instead, set the
...}
```

Whidbey may also conflict with existing REPL customizations, so if necessary you
can add the [profile configuration](src/whidbey/plugin.clj) yourself.

If you experience errors, you can check how the profiles are being merged using
the lein-pprint or [lein-cprint](https://github.com/greglook/lein-cprint)
plugins:
Whidbey may also conflict with existing REPL customizations. If you experience
errors, you can check how the profiles are being merged using the lein-pprint or
[lein-cprint](https://github.com/greglook/lein-cprint) plugins:

```bash
$ lein with-profile +repl cprint :injections :repl-options
$ lein with-profile +repl cprint :repl-options
```

## Project Status

Whidbey used to require quite a bit of setup. Fortunately, the following changes
have made things a lot nicer:
- [X] [REPLy #138](https://github.com/trptcolin/reply/pull/138) to support
message context on interactive evals.
- [X] [REPLy release 0.3.1](https://github.com/trptcolin/reply) so that it
doesn't need to be installed locally.
- [X] [Leiningen](https://github.com/technomancy/leiningen) upgrade to REPLy
version 0.3.1 or higher, so that it doesn't need to be cloned locally. (Done
as of 2.4.2)
- [ ] [NREPL-55](http://dev.clojure.org/jira/browse/NREPL-55) for a better way
to control rendering middleware in the REPL.

## License

This is free and unencumbered software released into the public domain.
Expand Down
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
:deploy-branches ["master"]
:eval-in-leiningen true

:dependencies [[mvxcvi/puget "0.8.0-SNAPSHOT"]])
:dependencies [[mvxcvi/puget "0.8.0-SNAPSHOT"]
[org.clojure/data.codec "0.1.0"]])
45 changes: 43 additions & 2 deletions src/whidbey/repl.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
(ns whidbey.repl
(:require
[clojure.data.codec.base64 :as b64]
[clojure.tools.nrepl.middleware.pr-values :refer [pr-values]]
[whidbey.render :as render]))
[puget.data :as data]
[whidbey.render :as render])
(:import
java.net.URI))


(defn read-bin
"Reads a base64-encoded string into a byte array. Suitable as a data-reader
for `whidbey/bin` literals."
^bytes
[^String bin]
(b64/decode (.getBytes bin)))


(defn read-uri
"Constructs a URI from a string value. Suitable as a data-reader for
`whidbey/uri` literals."
^URI
[^String uri]
(URI. uri))


(defmacro extend-notation!
"Implements the `ExtendedNotation` protocol from Puget for the given type,
setting the rendering function and updating the reader in `*data-readers*`."
[tag t renderer reader]
`(when-not (extends? data/ExtendedNotation ~t)
(data/extend-tagged-value ~t '~tag ~renderer)
(alter-var-root #'default-data-readers assoc '~tag ~reader)))


(defn init!
"Initializes the repl to use Whidbey's customizations."
[options]
(render/update-options! options)
(alter-var-root #'pr-values (constantly identity)))
(alter-var-root #'pr-values (constantly identity))
(let [extend-option (:extend-notation options true)
should-extend? (case extend-option
true (constantly true)
false (constantly false)
#(some (partial = %) extend-option))]
(when (should-extend? :bin)
(extend-notation! whidbey/bin
(class (byte-array 0))
#(apply str (map char (b64/encode %)))
read-bin))
(when (should-extend? :uri)
(extend-notation! whidbey/uri URI str read-uri))))

0 comments on commit 4f805cd

Please sign in to comment.