Class Console

java.lang.Object
com.codename1.cli.Console

public final class Console extends Object

What a command needs to know about the terminal it writes to, and the entry point of the console API.

Answered from the terminal itself where there is one, and from the conventions command-line tools share otherwise: COLUMNS and LINES for the size, NO_COLOR and CLICOLOR_FORCE for color, COLORTERM and TERM for how many colors, and whether each stream is a terminal at all, so output piped to a file or another program carries no escape codes.

The rest of the API builds on this class:

  • Ansi composes styled text, colors up to 24-bit, links and cursor movement.
  • Table and Tree render rows and hierarchies, aligned by display width.
  • ProgressBar, MultiProgress and Spinner show work in progress, redrawn in place on a terminal and as plain lines in a log.
  • Prompt asks questions: confirmation, text, passwords, numbers and arrow-key selection lists, falling back to reading lines when standard input is not a terminal.
  • Method Details

    • width

      public static int width()
      The number of columns: COLUMNS when it holds a number, otherwise the terminal's width, otherwise 80. The variable wins, as it does for Python's shutil.get_terminal_size, so a script can fix the width.
    • height

      public static int height()
      The number of rows: LINES when it holds a number, otherwise the terminal's height, otherwise 24.
    • colorEnabled

      public static boolean colorEnabled()
      Whether style(String, Console.Style...) adds color: never when NO_COLOR is set, always when CLICOLOR_FORCE is set to anything but 0, and otherwise when TERM names a terminal other than dumb and standard output is a terminal, so output piped to a file or another program carries no escape codes. On Windows, where TERM is usually unset, a console is enough.
    • colorLevel

      public static Console.ColorLevel colorLevel()
      The color level: Console.ColorLevel.NONE when colorEnabled() says no, Console.ColorLevel.TRUECOLOR when COLORTERM says truecolor or 24bit (or inside Windows Terminal), Console.ColorLevel.ANSI256 when TERM mentions 256color, and Console.ColorLevel.ANSI16 otherwise.
    • unicode

      public static boolean unicode()
      Whether box drawing and other non-ASCII symbols are safe to print: false only when a locale variable (LC_ALL, LC_CTYPE, LANG, the first one set) names an encoding other than UTF-8.
    • isTerminal

      public static boolean isTerminal()
      Whether standard output is a terminal rather than a pipe or a file.
    • isErrorTerminal

      public static boolean isErrorTerminal()
      Whether standard error is a terminal.
    • isInputTerminal

      public static boolean isInputTerminal()
      Whether standard input is a terminal, so a person can answer a Prompt key by key.
    • print

      public static void print(String text)
      Writes text to standard output.
    • println

      public static void println(String text)
      Writes text and a line break to standard output.
    • readLine

      public static String readLine(String prompt)
      Writes prompt and reads one line of standard input, without its line break and decoded as UTF-8, or returns null at end of input. The line is read as the terminal delivers it, edited and echoed by the terminal itself; Prompt.text(String) is the interactive question.
    • printError

      public static void printError(String text)
      Writes text and a line break to standard error.
    • style

      public static String style(String text, Console.Style... styles)
      text with ANSI escapes for styles around it, or text itself when colorEnabled() says no.
    • displayWidth

      public static int displayWidth(String text)
      The number of columns text takes on a terminal: escape codes take none, East Asian wide characters and emoji take two, combining marks none.
    • strip

      public static String strip(String text)
      text with every ANSI escape sequence removed.