Package ortus.boxlang.runtime.cli
Class MiniConsole
java.lang.Object
ortus.boxlang.runtime.cli.MiniConsole
- All Implemented Interfaces:
AutoCloseable
MiniConsole - A lightweight, cross-platform command-line input handler with history support.
This class provides a zero-dependency solution for interactive command-line applications
that need features like:
- Arrow key navigation through command history
- Raw terminal input (character-by-character)
- Cross-platform support (Windows via PowerShell, POSIX via stty)
- Configurable prompts with color support
- Command history management
- Terminal control sequences (Ctrl+C, Ctrl+D, etc.)
The console automatically detects the operating system and uses the appropriate
method for raw input:
- Windows: PowerShell-based key reading
- POSIX (macOS/Linux): stty + dd for direct /dev/tty access
Example usage:
try ( MiniConsole console = new MiniConsole() ) {
console.setPrompt( "MyApp> " );
String input;
while ( ( input = console.readLine() ) != null ) {
if ( "exit".equals( input ) )
break;
// Process the input
System.out.println( "You entered: " + input );
}
}
Features:
- UP/DOWN arrows navigate command history
- TAB cycles forward through completions, SHIFT+TAB cycles backward
- Ctrl+C exits the current input
- Ctrl+D clears current line, or exits on empty line
- Backspace/Delete for editing
- Automatic history management (no duplicates)
- 256-color terminal support for prompts- Since:
- 1.6.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionConstructor with default settingsMiniConsole(String prompt) Constructor with custom promptMiniConsole(String prompt, ISyntaxHighlighter highlighter) Constructor with custom prompt and syntax highlighter -
Method Summary
Modifier and TypeMethodDescriptionvoidaddToHistory(String command) Add a command to the historystatic Stringbackground(int colorIndex) Create a 256-color background ANSI sequencevoidclear()This function clears the entire console screen and resets the cursor to the top-left corner.voidClear the command historyvoidclose()Close the console and release any resourcesstatic Stringcolor(int colorIndex) Create a 256-color foreground ANSI sequenceGet a copy of the command historygetHistoryCommand(int historyNum) Get a specific command from history by number (1-based)Get the last command from historyintGet the maximum history sizeGet the previous command (second-to-last) from historyGet the current prompt stringGet the current syntax highlighterstatic voidprintDebug(String text) Convenient static alias to print a debug message in magenta bold textstatic voidprintError(String text) Convenient static alias to print an error message in red bold textstatic voidConvenient static alias to print an info message in blue bold textstatic voidprintSuccess(String text) Convenient static alias to print a success message in green bold textstatic voidprintWarning(String text) Convenient static alias to print a warning message in yellow bold textreadLine()Read a line of input with full editing capabilitiesRead a line with a custom prompt (doesn't change the default prompt)registerTabProvider(ITabProvider provider) Register a tab completion providerstatic Stringreset()ANSI reset sequence to clear all formattingsetMaxHistorySize(int maxSize) Set the maximum number of history entries to retainSet the prompt string displayed before user inputsetSyntaxHighlighter(ISyntaxHighlighter highlighter) Set a syntax highlighter for real-time input coloringvoidDisplay the command history to System.out
-
Constructor Details
-
MiniConsole
public MiniConsole()Constructor with default settings -
MiniConsole
Constructor with custom prompt- Parameters:
prompt- The prompt string to display
-
MiniConsole
Constructor with custom prompt and syntax highlighter- Parameters:
prompt- The prompt string to displayhighlighter- The syntax highlighter to use, or null to disable
-
-
Method Details
-
setPrompt
Set the prompt string displayed before user input- Parameters:
prompt- The new prompt string (null defaults to "> ")
-
getPrompt
Get the current prompt string- Returns:
- The current prompt string
-
setSyntaxHighlighter
Set a syntax highlighter for real-time input coloring- Parameters:
highlighter- The syntax highlighter to use, or null to disable- Returns:
- this console instance for method chaining
-
getSyntaxHighlighter
Get the current syntax highlighter- Returns:
- The current syntax highlighter, or null if none set
-
registerTabProvider
Register a tab completion provider- Parameters:
provider- The tab completion provider to register- Returns:
- This MiniConsole instance for method chaining
-
setMaxHistorySize
Set the maximum number of history entries to retain- Parameters:
maxSize- Maximum history size (default: 1000)
-
getMaxHistorySize
public int getMaxHistorySize()Get the maximum history size- Returns:
- Maximum number of history entries
-
getHistory
Get a copy of the command history- Returns:
- List of command history entries
-
addToHistory
Add a command to the history- Parameters:
command- The command to add (null/empty commands are ignored)
-
clearHistory
public void clearHistory()Clear the command history -
showHistory
public void showHistory()Display the command history to System.out -
getLastCommand
Get the last command from history- Returns:
- The last command, or null if no history
-
getPreviousCommand
Get the previous command (second-to-last) from history- Returns:
- The previous command, or null if not enough history
-
getHistoryCommand
Get a specific command from history by number (1-based)- Parameters:
historyNum- The history number (1-based)- Returns:
- The command, or null if not found
-
color
Create a 256-color foreground ANSI sequence- Parameters:
colorIndex- Color index (0-255)- Returns:
- ANSI escape sequence for foreground color
-
background
Create a 256-color background ANSI sequence- Parameters:
colorIndex- Color index (0-255)- Returns:
- ANSI escape sequence for background color
-
reset
ANSI reset sequence to clear all formatting- Returns:
- ANSI reset sequence
-
printError
Convenient static alias to print an error message in red bold text- Parameters:
text- The message text
-
printSuccess
Convenient static alias to print a success message in green bold text- Parameters:
text- The message text
-
printWarning
Convenient static alias to print a warning message in yellow bold text- Parameters:
text- The message text
-
printInfo
Convenient static alias to print an info message in blue bold text- Parameters:
text- The message text
-
printDebug
Convenient static alias to print a debug message in magenta bold text- Parameters:
text- The message text
-
clear
public void clear()This function clears the entire console screen and resets the cursor to the top-left corner. -
readLine
Read a line of input with full editing capabilities- Returns:
- The input line, or null on EOF/exit
- Throws:
IOException- If an I/O error occurs
-
readLine
Read a line with a custom prompt (doesn't change the default prompt)- Parameters:
customPrompt- Temporary prompt for this read operation- Returns:
- The input line, or null on EOF/exit
- Throws:
IOException- If an I/O error occurs
-
close
public void close()Close the console and release any resources- Specified by:
closein interfaceAutoCloseable
-