Basic colour in the standard REPL

427 views
Skip to first unread message

frou

unread,
Aug 27, 2010, 7:09:31 AM8/27/10
to Clojure
Is it or would it be possible to add some basic text colouring to the
standard REPL (the one started with the "clj" shell script).

It would be nice to be able to make the prompt, e.g. "user=>" coloured
(green in my case) so that you get at-a-glance distinction between
your inputs and the results printed.

Thank you

Ramakrishnan Muthukrishnan

unread,
Aug 27, 2010, 9:48:50 AM8/27/10
to clo...@googlegroups.com

If you use rlwrap to invoke the clojure repl, you can do that with
--prompr-colour option

eg;

rlwrap -p"red" java -cp blah blah clojure.main

--
  Ramakrishnan

Chouser

unread,
Aug 27, 2010, 10:13:31 AM8/27/10
to clo...@googlegroups.com

I didn't know about that. Cool tip, but for me it pretty easily
gets confused when the prompt changes (such as when moving
between namespaces).

I've been using this for a while -- not perfect either, but it
attempts to print a green line before each prompt, and color
return values blue.

(defn my-repl []
(binding [*pprint* true]
(clojure.main/repl
:prompt #(printf
"\033[32m-----\033[m\n%s=> "
(ns-name *ns*))
:print (try
(fn [x]
(print "\033[34m")
(if *pprint*
(clojure.pprint/pprint x)
(prn x))
(print "\033[m")
(flush))
(catch Exception e
(prn e))))))

You can either just run (my-repl) at the REPL, or start Clojure
with -e '(my-repl)' If you then run a little test, you can see
each of the different colors:

(do (println "hi") 5)

--Chouser
http://joyofclojure.com/

frou

unread,
Aug 27, 2010, 12:52:57 PM8/27/10
to Clojure
Excellent. Thank you both.

On Aug 27, 3:13 pm, Chouser <chou...@gmail.com> wrote:
> On Fri, Aug 27, 2010 at 9:48 AM, Ramakrishnan Muthukrishnan
>
> <vu3...@gmail.com> wrote:

Mike Meyer

unread,
Aug 27, 2010, 1:47:28 PM8/27/10
to clo...@googlegroups.com, ma...@frou.org

The repl is missing a lot of things a modern command line should have
- history, line editing, autocompletion, etc.

The already mentioned rlwrap can provide all of those. However, most
of the writeups on the web just use it, without noticing that the
syntax can be adjusted, and defaults to C-like syntax. I generally
fixed that by using --quote-chars='"' and --complete-filenames (the
latter removes / and . from the default set of break characters, which
may be why I haven't seen problems with the prompt color). The
other break chars just tweak autocompletion and word erase a little,
so I haven't really fooled with those.

<mike

--
Mike Meyer <m...@mired.org> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

Reply all
Reply to author
Forward
0 new messages