0

I am setting up a Console Command interface where the input line should always be the line which is below every other line. So if an other thread prints something while I write something in the input line, it interrupts. So it messes everything up, and I can't edit my input clearly in Windows, Linux...

So if this arrow is arrow where my Application starts reading the line from the console
>

and if I write something
> myInput

and some other threads prints something.
> myInputLogFromOtherThread

then the input will interrupt with the latest log and the cursor will jump to the no mans land and I can't edit my input like I said above (i can still edit it, but i dont see the changes)

I have tried lots of differend Api's like JLine, Cursors, Lanterna but they didn't worked for me. Lanterna lagged, Cursors and JLine didn't run at all as they should, and I didn't found any helpful documentation, how to make something I want

Here is my code, and at the println event I want to insert the line to be printed to the console, between the input line > myInput and the line before the input.

    public static void main(String[] args) {

        //On System.out.println event
        System.setOut(new PrintStream(System.out) {
            public void println(String s) {
                super.println(s); // So i want that this line, gets insertet between the last line and the line before the last line
                /*
                 * So it should be inserted
                 * 
                 * SomeRandomLogging
                 *  <- here, that it doesn't interrupt our input
                 * > myInput
                 */
            }
        });

        System.out.print("> ");

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("LogFromOtherFread");
            }
        }).start();

        final List<String> lines = new ArrayList<>();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
            String line = null;
            while ((line = in.readLine()) != null) {
                lines.add(line);
            }
        }
    }

So I expect that after the Application moves the line which gets printed, to the console, before the input line, it should work like a command interface e.g. Linux, Bukkit (Minecraft Server Api),...

Gebes
  • 312
  • 2
  • 9

1 Answers1

0

You can look into some terminal-control libraries to force the cursor to a certain column / row

here is a post, you can look into Charva, or lanterna

EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • I once used lanterna, but if I ran it on a linux Server, it needed minutes to print smth, in the swing terminal it needed seconds – Gebes Jun 20 '19 at 06:00