protected static Dimension getSizeViaResize ( ) { BufferedReader bin = null ; try { Process proc = Runtime . getRuntime ( ) . exec ( "resize" ) ; InputStream in = proc . getInputStream ( ) ; bin = new BufferedReader ( new InputStreamReader ( in ) ) ; Pattern regex = Pattern . compile ( "([0-9]+)" ) ; String line ; int columns = - 1 , lines = - 1 ; while ( ( line = bin . readLine ( ) ) != null ) { if ( line . indexOf ( "COLUMNS" ) != - 1 ) { Matcher match = regex . matcher ( line ) ; if ( match . find ( ) ) { columns = safeToInt ( match . group ( ) ) ; } } else if ( line . indexOf ( "LINES" ) != - 1 ) { Matcher match = regex . matcher ( line ) ; if ( match . find ( ) ) { lines = safeToInt ( match . group ( ) ) ; } } } if ( columns != - 1 && lines != - 1 ) { return new Dimension ( columns , lines ) ; } return null ; } catch ( PatternSyntaxException pse ) { return null ; // logging a warning here may be annoying } catch ( SecurityException se ) { return null ; // logging a warning here may be annoying } catch ( IOException ioe ) { return null ; // logging a warning here may be annoying } finally { StreamUtil . close ( bin ) ; } }
Tries to obtain the terminal dimensions by running resize .