Skip to content

Small Java library to setup / interact with a Windows terminal

License

Notifications You must be signed in to change notification settings

alexarchambault/native-terminal

Repository files navigation

native-terminal

Build status Maven Central

native-terminal is a small Java library to setup / interact with terminals in a native fashion. It allows to

  • query the terminal size, and
  • on Windows, change the console mode so that it accepts ANSI escape codes.

It relies on internals of the jansi library to do so, and also works from GraalVM native images.

Compared to using jline, native-terminal only and solely calls the right ioctl system calls (or kernel32.dll system calls, like SetConsoleMode or GetConsoleScreenBufferInfo, on Windows), lowering the odds of something going wrong when generating or using a GraalVM native image for example.

Usage

Add to your build.sbt

libraryDependencies += "io.github.alexarchambault.native-terminal" % "native-terminal" % "0.0.7"

The latest version is Maven Central.

Change terminal mode

Change the terminal mode on Windows, so that it accepts ANSI escape codes with

import io.github.alexarchambault.nativeterm.NativeTerminal;

boolean success = NativeTerminal.setupAnsi();

A returned value of false means ANSI escape codes aren't supported by the Windows version you're running on. These are supposed to be supported by Windows 10 build 10586 (Nov. 2015) onwards.

Calling this method is safe on other platforms. It simply returns true in that case.

Get terminal size

import io.github.alexarchambault.nativeterm.NativeTerminal;
import io.github.alexarchambault.nativeterm.TerminalSize;

TerminalSize size = NativeTerminal.size();
int width = size.getWidth();
int height = size.getHeight();

License

All files in this repository, except NativeImageFeature.java, can be used either under the Apache 2.0 license, or the GNU GPL version 2 license, at your convenience.

The NativeImageFeature.java file, originally based on a GNU GPL version 2 only file, is licensed only under the GNU GPL version 2 license.