-
-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use system calls to get terminal size on Linux / Mac #4497
base: 0.12.x
Are you sure you want to change the base?
Use system calls to get terminal size on Linux / Mac #4497
Conversation
if (mill.main.client.Util.hasConsole()) | ||
try { | ||
NativeTerminal.getSize(); | ||
canUse = true; | ||
} catch (Throwable ex) { | ||
canUse = false; | ||
} | ||
else canUse = false; | ||
|
||
canUseNativeTerminal = canUse; | ||
} | ||
|
||
static void writeTerminalDims(boolean tputExists, Path serverDir) throws Exception { | ||
String str; | ||
|
||
try { | ||
if (java.lang.System.console() == null) str = "0 0"; | ||
if (!mill.main.client.Util.hasConsole()) str = "0 0"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we're checking mill.main.client.Util.hasConsole()
twice here; once in the static
blockl and once in writeTerminalDims
. We can probably skip one of those checks right?
Trying this out on my macbook seems to add about ~200ms latency (~150ms -> ~350ms), running |
I'm getting the same numbers :/ Printing the duration of some of the calls to get the terminal size, I get things like
So once the native things are setup, getting the terminal size is sub-ms. Upon initialization, jansi reads a binary file (like |
If the resource/tempdir stuff is circumventable, does that mean that it isn't necessary for the jni functionality? |
Not necessarily, we can load the native library from elsewhere on disk if we want to. I just pushed code that loads it via the coursier cache (this needs coursier/coursier#3272, so this is going to need new coursier and coursier-interface releases). If the native library is already in cache, there's no need to write a temporary file. From native images, we can also statically link the native library, so that nothing needs to be loaded at runtime. But this needs the JNI library to push static libraries somewhere (like here or here), and jansi doesn't. It worked fine for other JNI libraries in Scala CLI, although I haven't seen that being done elsewhere. |
From Java 22 onwards, it should be possible to use FFM, JLine is able to for sure. This doesn't need to load custom native libraries upfront (although I have very little experience with it for now). |
Unpacking the native library and caching it via coursier sounds reasonable. We just need to make sure Coursier is not on the hot path so we don't need to classload all the coursier/scala code when the library is already unpacked. |
(Seems this is surfacing issues with newer versions of coursier) |
This makes Mill use my native-terminal library (formerly windows-ansi) to get the terminal size. That way, Mill doesn't run
tput
commands every ~100 ms to query the terminal size.