Class Console
java.lang.Object
com.codename1.cli.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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumHow many colors the terminal shows, the levelAnsiconverts colors down to.static enumThe stylesstyle(String, Console.Style...)can apply. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanWhetherstyle(String, Console.Style...)adds color: never whenNO_COLORis set, always whenCLICOLOR_FORCEis set to anything but0, and otherwise whenTERMnames a terminal other thandumband standard output is a terminal, so output piped to a file or another program carries no escape codes.static Console.ColorLevelThe color level:Console.ColorLevel.NONEwhencolorEnabled()says no,Console.ColorLevel.TRUECOLORwhenCOLORTERMsaystruecoloror24bit(or inside Windows Terminal),Console.ColorLevel.ANSI256whenTERMmentions256color, andConsole.ColorLevel.ANSI16otherwise.static intdisplayWidth(String text) The number of columnstexttakes on a terminal: escape codes take none, East Asian wide characters and emoji take two, combining marks none.static intheight()The number of rows:LINESwhen it holds a number, otherwise the terminal's height, otherwise 24.static booleanWhether standard error is a terminal.static booleanWhether standard input is a terminal, so a person can answer aPromptkey by key.static booleanWhether standard output is a terminal rather than a pipe or a file.static voidWritestextto standard output.static voidprintError(String text) Writestextand a line break to standard error.static voidWritestextand a line break to standard output.static StringWritespromptand reads one line of standard input, without its line break and decoded as UTF-8, or returns null at end of input.static Stringtextwith every ANSI escape sequence removed.static Stringstyle(String text, Console.Style... styles) static booleanunicode()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.static intwidth()The number of columns:COLUMNSwhen it holds a number, otherwise the terminal's width, otherwise 80.
-
Method Details
-
width
public static int width()The number of columns:COLUMNSwhen it holds a number, otherwise the terminal's width, otherwise 80. The variable wins, as it does for Python'sshutil.get_terminal_size, so a script can fix the width. -
height
public static int height()The number of rows:LINESwhen it holds a number, otherwise the terminal's height, otherwise 24. -
colorEnabled
public static boolean colorEnabled()Whetherstyle(String, Console.Style...)adds color: never whenNO_COLORis set, always whenCLICOLOR_FORCEis set to anything but0, and otherwise whenTERMnames a terminal other thandumband standard output is a terminal, so output piped to a file or another program carries no escape codes. On Windows, whereTERMis usually unset, a console is enough. -
colorLevel
The color level:Console.ColorLevel.NONEwhencolorEnabled()says no,Console.ColorLevel.TRUECOLORwhenCOLORTERMsaystruecoloror24bit(or inside Windows Terminal),Console.ColorLevel.ANSI256whenTERMmentions256color, andConsole.ColorLevel.ANSI16otherwise. -
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 aPromptkey by key. -
print
Writestextto standard output. -
println
Writestextand a line break to standard output. -
readLine
Writespromptand 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
Writestextand a line break to standard error. -
style
-
displayWidth
The number of columnstexttakes on a terminal: escape codes take none, East Asian wide characters and emoji take two, combining marks none. -
strip
-