Skip to content

Instantly share code, notes, and snippets.

@johan-carlsson
Forked from bhb/README.md
Last active October 12, 2022 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johan-carlsson/6aec54a93aafadd198dc7125779ce6f3 to your computer and use it in GitHub Desktop.
Save johan-carlsson/6aec54a93aafadd198dc7125779ce6f3 to your computer and use it in GitHub Desktop.
Clojure friendly mode with nrepl starting on port 7888, inspired by https://github.com/slipset/friendly
{:paths ["."]
:deps {com.bhauman/rebel-readline {:mvn/version "0.1.4"}
venantius/pyro {:mvn/version "0.1.2"}
expound {:mvn/version "0.7.1"}
org.clojure/tools.nrepl {:mvn/version "0.2.12"}}}
(ns friendly
(:require [pyro.printer :as pyro]
[rebel-readline.clojure.main]
[expound.alpha :as expound]
[clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[clojure.stacktrace]
[clojure.tools.nrepl.server :as n]
[clojure.main]))
(def printer (expound/custom-printer {:print-specs? false
:show-valid-values? true
:theme :figwheel-theme}))
(defn repl-caught [e]
(let [ex (clojure.main/repl-exception e)
tr (.getStackTrace ex)
el (when-not (zero? (count tr)) (aget tr 0))
ex-m (Throwable->map ex)]
(binding [*out* *err*]
(cond
;; If the output is a clojure spec issue...
(::s/problems (:data ex-m))
;; print expound output
(do
(println (str (re-find #"Call to .* did not conform to spec\:" (.getMessage ex))
"\n"
(with-out-str (printer (:data ex-m))))))
(instance? clojure.lang.LispReader$ReaderException e)
(println (:cause (Throwable->map e)))
:else
;; otherwise print exception
(println (str (if (instance? clojure.lang.Compiler$CompilerException ex)
(str
(-> ex class .getSimpleName)
" " (.getMessage ex) " ")
(str " " (if el
(clojure.stacktrace/print-stack-trace ex)
"[trace missing]")))))))))
(defn -main []
(set! s/*explain-out* printer)
(st/instrument)
(pyro/swap-stacktrace-engine!)
(defonce server (n/start-server :port 7888))
(rebel-readline.clojure.main/repl :caught repl-caught))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment