Search code examples
javacommand-line-interfacepicocli

Can i do scrollable results with picocli?


I want to know if i can implement scrollable results with picocli app. Example:

GetTracks

which outputs the list of results

>TrackA 
 TrackB
 TrackC

and user can scroll up or down and select a track he wants ? I want to know if this functionality is possible with picocli?


Solution

  • Unfortunately no, picocli only deals with how command arguments are parsed and represented to the programmer. Any further menus will need to be handled by you, the programmer. You can make something similar to this using a simple number entering workflow. Where the use enters the number of the desired menu item:

    >>> GetTracks
    
    1 ) Track 1
    2 ) Track 2
    3 ) Track 3
    
    select a track:
    

    You may be able to produce this workflow you are looking for by leveraging a library like lanterna. A VERY simple implementation might look like this:

    public class OutputChar {
    
        public static void main(String[] args) throws IOException {
            Terminal terminal = new DefaultTerminalFactory().createTerminal();
            Screen screen = new TerminalScreen(terminal);
            TextGraphics graphics = screen.newTextGraphics();
    
            screen.startScreen();
            screen.clear();
    
            graphics.putString(0, 0, "Track 1");
            graphics.putString(0, 1, "Track 2");
            graphics.putString(0, 2, "Track 3");
            screen.refresh();
    
            int selectedTrack = 0;
            KeyStroke stroke = screen.readInput();
    
            if (stroke instanceof MouseAction) {
                MouseAction action = (MouseAction) stroke;
                MouseActionType actionType = action.getActionType();
                switch (actionType) {
                    case MouseActionType.SCROLL_UP: selectedTrack++;
                            break;
    
                    case MouseActionType.SCROLL_DOWN: selectedTrack--;
                            break;
                }
            }
            
            screen.stopScreen();
        }
    
    }
    

    The above example will read a single input from either keyboard or mouse, and change the selected track depending on the direction of the scroll. You will want to do some more logic and workflow control (to rectify some pretty obvious bugs), but this should get you started, if you decide to go down this path.