private String readInput ( PrintStream out , boolean echo ) throws InterruptedException { StringBuilder sb = new StringBuilder ( ) ; Key inputKey ; do { CommandOperation input = commandInvocation . getInput ( ) ; inputKey = input . getInputKey ( ) ; if ( inputKey == Key . CTRL_C || inputKey == Key . CTRL_D ) { throw new InterruptedException ( inputKey . name ( ) ) ; } else if ( inputKey == Key . BACKSPACE && sb . length ( ) > 0 ) { sb . setLength ( sb . length ( ) - 1 ) ; if ( echo ) { // move cursor left out . print ( Buffer . printAnsi ( "1D" ) ) ; out . flush ( ) ; // overwrite it with space out . print ( " " ) ; // move cursor back again out . print ( Buffer . printAnsi ( "1D" ) ) ; out . flush ( ) ; } } else if ( inputKey . isPrintable ( ) ) { if ( echo ) out . print ( inputKey . getAsChar ( ) ) ; sb . append ( inputKey . getAsChar ( ) ) ; } } while ( inputKey != Key . ENTER && inputKey != Key . ENTER_2 ) ; return ( sb . length ( ) == 0 ) ? null : sb . toString ( ) ; }
Performs the hard work