Search code examples
clojure

Backspace esc char in Clojure's console


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!


Solution

  • 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.