Skip to content

Terminal width

h908714124 edited this page Aug 27, 2021 · 14 revisions

Java doesn't have a built-in way to determine the actual number of characters per row in the terminal that launches the JVM. By default, a terminal width of 80 columns is assumed. On many terminals, this leaves a lot of empty space on the right when the usage information is printed.

If you have a library like jline to determine the actual terminal width, then this information can be passed to the StandardErrorHandler, to make better use of the available space:

public static void main(String[] args) {
  int width = TerminalBuilder.terminal().getWidth();
  DeleteCommand command = new DeleteCommandParser().parse(List.of(args))
    .orElseThrow(failure -> {
      StandardErrorHandler.builder()
          .withTerminalWidth(width)
          .build().printErrorMessage(failure);
      System.exit(1);
      return new RuntimeException();
    });
  // ...
}
Clone this wiki locally