Reading single characters...?

325 views
Skip to first unread message

Simon Brooke

unread,
Sep 20, 2013, 12:49:29 PM9/20/13
to clo...@googlegroups.com
I've discovered some interesting behaviour - not necessarily a bug, and (if it is a bug) not necessarily a bug in Clojure.

Essentially, to emulate a 1970s user interface, I want to read single key strokes from the console. I've found two recipes online, both using the JLine java package:


One of these recipes uses Terminal.getTerminal(), the other uses new ConsoleReader(). What happens in my context (Debian on 64 bit Intel) is that as soon as an instance of either class is instantiated, echoing to the console ceases. If you do (obviously, with the jline jar on the classpath):

(import 'jline.Terminal)
(def term (Terminal/getTerminal))
(.initializeTerminal term)
(.enableEcho term)

(or the equivalent things with a ConsoleReader), every keystroke is echoed twice. But if you do

user=> ((..ddiissaabblleeEEcchhoo  tteerrmm))
^Jnil

one single further character gets echoed and then nothing more, unless you type (blindly) (.enableEcho term), when the double-echoing behaviour resumes. This is consistent - on my Debian box - with Clojure 1.2, Clojure 1.3, and Clojure 1.5.1, all using jline 1.0. I haven't yet compiled up a little Java app to find out what happens without Clojure, and I haven't tried compiling a little command-line Clojure app, because I want to be able to read single characters in the context of the REPL, so if, as I hypothesise, jline is fighting with the REPL, proving whether it works outwith the context of the REPL doesn't really help me.

Has anyone else seen this behaviour? Is there any solution?

juan.facorro

unread,
Sep 22, 2013, 8:30:29 AM9/22/13
to clo...@googlegroups.com
According to the JLine's source code (v1.0) in UnixTerminal.java, echo is disabled with the command stty -echo which disables all echoing in the console (just tried it in a terminal since I wasn't familiar with this command).  

There are two projects for JLine in github, JLine and JLine2. The latter seems to be actively mantained, but after digging a little I found the same command is used when disabling echo in Unix terminals, see here and here.

I don't know of a way you could go around that, but maybe someone can help or suggest an alternative.

HTH,

J   

John Jacobsen

unread,
Sep 23, 2013, 12:11:14 AM9/23/13
to clo...@googlegroups.com
The JLine story is indeed confusing.  I believe https://github.com/jline/jline2 is the current one you want ([jline "2.11"] in your project.clj deps).  See the forthcoming Clojure Cookbook recipe.  If that recipe (or its trivial extension to echo repeated characters) indeed does not work correctly for you, now would be an excellent time to let people know so there is a chance to correct it before it goes to print!  

John

Simon Brooke

unread,
Sep 23, 2013, 5:57:04 AM9/23/13
to clo...@googlegroups.com
OK, thank you very much chaps. I shall investigate!
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/IDzqEQKkDD0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages