Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class Console
- Object
- Console
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:
Ansicomposes styled text, colors up to 24-bit, links and cursor movement.TableandTreerender rows and hierarchies, aligned by display width.ProgressBar,MultiProgressandSpinnershow work in progress, redrawn in place on a terminal and as plain lines in a log.Promptasks questions: confirmation, text, passwords, numbers and arrow-key selection lists, falling back to reading lines when standard input is not a terminal.
Nested types
enum Console.ColorLevel | How many colors the terminal shows, the level Ansi converts colors down to. |
enum Console.Style | The styles style can apply. |
Methods
public static int width() | The number of columns: COLUMNS when it holds a number, otherwise the terminal’s width, otherwise 80. |
public static int height() | The number of rows: LINES when it holds a number, otherwise the terminal’s height, otherwise 24. |
public static boolean colorEnabled() | Whether 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. |
public static Console.ColorLevel colorLevel() | The color level: ColorLevel.NONE when colorEnabled says no, ColorLevel.TRUECOLOR when COLORTERM says truecolor or 24bit (or inside Windows Terminal), ColorLevel.ANSI256 when TERM mentions 256color, and ColorLevel.ANSI16 otherwise. |
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. |
public static boolean isTerminal() | Whether standard output is a terminal rather than a pipe or a file. |
public static boolean isErrorTerminal() | Whether standard error is a terminal. |
public static boolean isInputTerminal() | Whether standard input is a terminal, so a person can answer a Prompt key by key. |
public static void print(String text) | Writes text to standard output. |
public static void println(String text) | Writes text and a line break to standard output. |
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. |
public static void printError(String text) | Writes text and a line break to standard error. |
public static String style(String text, Console.Style... styles) | text with ANSI escapes for styles around it, or text itself when colorEnabled says no. |
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. |
public static String strip(String text) | text with every ANSI escape sequence removed. |
Inherited methods
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 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:
ColorLevel.NONE when colorEnabled says no,
ColorLevel.TRUECOLOR when COLORTERM says truecolor or 24bit
(or inside Windows Terminal), ColorLevel.ANSI256 when TERM mentions
256color, and 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.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)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.