3

I'm learning Clojure.

To practice I started rewriting Java app I've written for my son to solve inequalities (like 3 + 2 ? 7).

In java I did (note using backslash escape chars)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class JavaTest {
    public static void main(String[] args) throws Exception {
        System.out.print("3 + 2 ? 7\b\b\b");
        System.out.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        reader.readLine();
    }
}

and see expected result: cursor placed on the question sign position and when I write a char it overwrites the question sign. correct output from java

But when I do the same (I believe so) in Clojure

(print "3 + 2 ? 7\b\b\b")
(flush)

(let [reader (java.io.BufferedReader. *in*)]
      (.readLine reader))

I see that cursor is placed at the correct position for milliseconds and then is moved at the end of the line. And when I put a char, it is placed at very end of the string I printed. incorrect output from clojure

As a workaround I could use clojure-lanterna library, as suggested here, but I'd like to solve such a simple thing without any libraries (as I actually did in Java).

Any ideas on this? Or maybe someone can explain the reason of the behavior. I tried to find it in Clojure sources, but without success.

Thanks in advance!

  • 1
    Just tried your Clojure code (wrapped it in a function `(defn read-test [] ...)`, then called it) - it works as expected. How are you invoking your Clojure code? – Aleph Aleph Oct 11 '18 at 20:21
  • I just do `clj clojure-test.clj` in terminal. Forgot to mention, that I use Linux Mint 18, it might be relevant. – Uladzimir Kapylou Oct 11 '18 at 20:28
  • It works for me too so as @AlephAleph suggested we need to know how you execute your code. – Piotrek Bzdyl Oct 11 '18 at 20:47
  • My code is in clojure-test.clj file (above is a full listing). I run it as described in my previous comment. I've also tried to wrap it into a function as @AlephAleph suggested, and still get wrong behavior. I also tried to do `(load-file "clojure-test.clj")` from repl with the same result (both wrapped and bare versions). Which OS/shell do you use? Might it be linked with the issue? – Uladzimir Kapylou Oct 11 '18 at 21:02
  • @glts's answer solves the question for me (bash, Ubuntu 18.04). – Aleph Aleph Oct 11 '18 at 21:05

2 Answers2

4

Use clojure instead of clj.

clj wraps the ordinary Clojure REPL in rlwrap, which provides a more ergonomic command-line editing experience; presumably here it is messing with the cursor in some way.

glts
  • 21,808
  • 12
  • 73
  • 94
0

You can achieve the above behavior by calling the native Java methods:

  (.println System/out "jjjabcdef 99999 777"  )
  (.println System/out "jjjabcdef 99999 \b\b\b 777"  )

=>

jjjabcdef 99999 777
jjjabcdef 999 777
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • This doesn't help. I tried this before, and I've retried this just now. It works in your case, because you continue printing new chars in the same string, where \b are placed. But it doesn't work when you ask a user to enter a symbol. – Uladzimir Kapylou Oct 11 '18 at 20:32