Skip to content

Instantly share code, notes, and snippets.

@greglook
Last active November 18, 2022 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greglook/037e64fd5b7da0f84469769c93780f72 to your computer and use it in GitHub Desktop.
Save greglook/037e64fd5b7da0f84469769c93780f72 to your computer and use it in GitHub Desktop.
Clojure rebel-readline customizations
{:paths ["src" "resources"]
,,,
:aliases
{;; For a basic clojure REPL with an initial namespace and pretty-printing:
;; clj -M:repl
:repl
{:extra-paths ["dev" "test"]
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.3.0"}
com.amperity/dialog {:mvn/version "1.0.1"}
mvxcvi/puget {:mvn/version "1.3.2"}}
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"
"-Ddialog.profile=repl"]
:main-opts ["-e" "(require,'puget.printer)"
"-e" "(clojure.main/repl,:init,#(do,(require,'acme.repl),(in-ns,'acme.repl)),:print,puget.printer/cprint)"]}
;; For a fancier rebel-readline experience with autocomplete and interruptible eval:
;; clojure -M:rebel
:rebel
{:extra-paths ["dev" "test"]
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.3.0"}
org.clojure/tools.reader {:mvn/version "1.3.6"}
com.bhauman/rebel-readline {:mvn/version "0.1.4"}
com.amperity/dialog {:mvn/version "1.0.1"}
mvxcvi/puget {:mvn/version "1.3.2"}}
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"
"-Ddialog.profile=repl"]
:main-opts ["-m" "acme.repl.main"]}
,,,}}
(ns acme.repl
(:require
[clojure.java.io :as io]
[clojure.repl :refer :all]
[clojure.tools.namespace.repl :refer [refresh]]
,,,))
;; other repl utilities
(ns acme.repl.main
"Main setup for the Acme REPL."
(:require
[clojure.repl :as clj-repl]
;; Prevent a weird startup issue with lazy-loading
[clojure.tools.reader.edn]
[puget.printer :as puget]
[rebel-readline.clojure.line-reader :as rebel-reader]
[rebel-readline.clojure.main :as rebel-main]
[rebel-readline.core :as rebel-core]
[rebel-readline.jline-api :as rebel-jline]))
(def print-options
"REPL value printer options."
{,,,})
(defn init-ns
"Initialize the REPL namespace entry."
[]
(require 'acme.repl)
(in-ns 'acme.repl))
(defn interruptable-eval
"Evaluate the provided form in a way that is interruptable with Ctrl+C."
[form]
(eval
`(let [thread# (Thread/currentThread)]
(clj-repl/set-break-handler!
(fn handle-interrupt#
[_signal#]
(.stop thread#)))
~form)))
(defn print-value
"Print a syntax highlighted clojure value with Puget. This printer respects
the current color settings set in the service.
See `rebel-readline.main/syntax-highlight-prn`."
[x]
(binding [*out* (.. rebel-jline/*line-reader* getTerminal writer)]
(puget/cprint x print-options)))
(defn -main
"Main entry point to start a new REPL."
[]
(rebel-core/ensure-terminal
(rebel-main/repl
:init init-ns
:eval interruptable-eval
:print print-value)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment