private int getTerminalWidth2() throws IOException {
        String osName = System.getProperty("os.name");
        boolean isOSX = osName.startsWith("Mac OS X");
        boolean isLinux = osName.startsWith("Linux")
                || osName.startsWith("LINUX");
        if (!isLinux && !isOSX) {
            return UNKNOWN_WIDTH; // actually, this might also work on Solaris
                                  // but this hasn't been tested
        }
        ProcessBuilder builder = new ProcessBuilder(which("sh").toString(),
                "-c", "stty -a < /dev/tty");
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        ByteArrayOutputStream resultBytes = new ByteArrayOutputStream();
        try {
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) >= 0) {
                resultBytes.write(buf, 0, len);
            }
        } finally {
            in.close();
        }

        String result = new String(resultBytes.toByteArray());
        // System.out.println("result=" + result);

        try {
            if (process.waitFor() != 0) {
                return UNKNOWN_WIDTH;
            }
        } catch (InterruptedException e) {
            return UNKNOWN_WIDTH;
        }

        String pattern;
        if (isOSX) {
            // Extract columns from a line such as this:
            // speed 9600 baud; 39 rows; 80 columns;
            pattern = "(\\d+) columns";
        } else {
            // Extract columns from a line such as this:
            // speed 9600 baud; rows 50; columns 83; line = 0;
            pattern = "columns (\\d+)";
        }
        Matcher m = Pattern.compile(pattern).matcher(result);
        if (!m.find()) {
            return UNKNOWN_WIDTH;
        }
        result = m.group(1);

        try {
            return Integer.parseInt(result);
        } catch (NumberFormatException e) {
            return UNKNOWN_WIDTH;
        }
    }