--- old/src/java.base/share/classes/java/io/Console.java 2018-08-24 15:15:26.996841300 +0200 +++ new/src/java.base/share/classes/java/io/Console.java 2018-08-24 15:15:25.447764700 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,8 +25,11 @@ package java.io; -import java.util.*; import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.Formatter; +import java.util.IllegalFormatException; + import jdk.internal.misc.JavaIOAccess; import jdk.internal.misc.SharedSecrets; import sun.nio.cs.StreamDecoder; @@ -390,6 +393,20 @@ pw.flush(); } + /** + * Obtains the width of the console window. + * + * @return The width or -1 if it cannot be obtained. + */ + public native int width(); + + /** + * Obtains the height of the console window. + * + * @return The height or -1 if it cannot be obtained. + */ + public native int height(); + private Object readLock; private Object writeLock; private Reader reader; @@ -401,6 +418,7 @@ private boolean restoreEcho; private boolean shutdownHookInstalled; private static native String encoding(); + /* * Sets the console echo status to {@code on} and returns the previous * console on/off status. --- old/src/java.base/unix/native/libjava/Console_md.c 2018-08-24 15:15:37.011607000 +0200 +++ new/src/java.base/unix/native/libjava/Console_md.c 2018-08-24 15:15:35.531578600 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,11 +27,34 @@ #include "jni_util.h" #include "jvm.h" #include "java_io_Console.h" - #include +#include +#include #include #include +JNIEXPORT jint JNICALL +Java_java_io_Console_width(JNIEnv *env, jobject this) +{ + struct winsize w; + + if (ioctl(0, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0 || ioctl(2, TIOCGWINSZ, &w) == 0) { + return w.ws_col; + } + return -1; +} + +JNIEXPORT jint JNICALL +Java_java_io_Console_height(JNIEnv *env, jobject this) +{ + struct winsize w; + + if (ioctl(0, TIOCGWINSZ, &w) == 0 || ioctl(1, TIOCGWINSZ, &w) == 0 || ioctl(2, TIOCGWINSZ, &w) == 0) { + return w.ws_row; + } + return -1; +} + JNIEXPORT jboolean JNICALL Java_java_io_Console_istty(JNIEnv *env, jclass cls) { --- old/src/java.base/windows/native/libjava/Console_md.c 2018-08-24 15:15:43.734279700 +0200 +++ new/src/java.base/windows/native/libjava/Console_md.c 2018-08-24 15:15:42.683548700 +0200 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,12 +27,40 @@ #include "jni_util.h" #include "jvm.h" #include "java_io_Console.h" - #include #include static HANDLE hStdOut = INVALID_HANDLE_VALUE; static HANDLE hStdIn = INVALID_HANDLE_VALUE; + +JNIEXPORT jint JNICALL +Java_java_io_Console_width(JNIEnv *env, jobject this) +{ + CONSOLE_SCREEN_BUFFER_INFO csbi; + + if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { + if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) { + return -1; + } + } + + return csbi.dwSize.X; +} + +JNIEXPORT jint JNICALL +Java_java_io_Console_height(JNIEnv *env, jobject this) +{ + CONSOLE_SCREEN_BUFFER_INFO csbi; + + if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { + if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) { + return -1; + } + } + + return csbi.dwSize.Y; +} + JNIEXPORT jboolean JNICALL Java_java_io_Console_istty(JNIEnv *env, jclass cls) {