Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select UI element in Java CLI

I would like to use a select element in a Java CLI, similar to this example:

enter image description here

that I stole from https://github.com/go-survey/survey

I found plenty of information about how you could do it in Go, but I did not find something useful for Java except the library https://github.com/mabe02/lanterna which is not really what I am looking for because I tries to create a whole GUI in a shell, and does not support powershell or cmd on Windows.

Can anyone tell me how to do this?

like image 927
J Fabian Meier Avatar asked Dec 05 '25 23:12

J Fabian Meier


1 Answers

I can't remember such ready to use library. But you may create one. You can utilize ANSI escape codes for coloring text and VT100 control codes for cursor positioning.

Here is an example for color:

public static final String RED = "\033[0;31m"; // RED
public static final String RESET = "\033[0m"; // Text Reset

public static void main(String[] args) {
        System.out.println(RED + "This text is red!" + RESET);
    }

and for position:

System.out.print(String.format("\033[%d;%dH", row, column));
like image 187
Oleg Imanilov Avatar answered Dec 07 '25 11:12

Oleg Imanilov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!