Class MiniConsole

java.lang.Object
ortus.boxlang.runtime.cli.MiniConsole
All Implemented Interfaces:
AutoCloseable

public class MiniConsole extends Object implements 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
  • Constructor Details

    • MiniConsole

      public MiniConsole()
      Constructor with default settings
    • MiniConsole

      public MiniConsole(String prompt)
      Constructor with custom prompt
      Parameters:
      prompt - The prompt string to display
    • MiniConsole

      public MiniConsole(String prompt, ISyntaxHighlighter highlighter)
      Constructor with custom prompt and syntax highlighter
      Parameters:
      prompt - The prompt string to display
      highlighter - The syntax highlighter to use, or null to disable
  • Method Details

    • setPrompt

      public MiniConsole setPrompt(String prompt)
      Set the prompt string displayed before user input
      Parameters:
      prompt - The new prompt string (null defaults to "> ")
    • getPrompt

      public String getPrompt()
      Get the current prompt string
      Returns:
      The current prompt string
    • setSyntaxHighlighter

      public MiniConsole setSyntaxHighlighter(ISyntaxHighlighter highlighter)
      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

      public ISyntaxHighlighter getSyntaxHighlighter()
      Get the current syntax highlighter
      Returns:
      The current syntax highlighter, or null if none set
    • registerTabProvider

      public MiniConsole registerTabProvider(ITabProvider provider)
      Register a tab completion provider
      Parameters:
      provider - The tab completion provider to register
      Returns:
      This MiniConsole instance for method chaining
    • setMaxHistorySize

      public MiniConsole setMaxHistorySize(int maxSize)
      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

      public List<String> getHistory()
      Get a copy of the command history
      Returns:
      List of command history entries
    • addToHistory

      public void addToHistory(String command)
      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

      public String getLastCommand()
      Get the last command from history
      Returns:
      The last command, or null if no history
    • getPreviousCommand

      public String getPreviousCommand()
      Get the previous command (second-to-last) from history
      Returns:
      The previous command, or null if not enough history
    • getHistoryCommand

      public String getHistoryCommand(int historyNum)
      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

      public static String color(int colorIndex)
      Create a 256-color foreground ANSI sequence
      Parameters:
      colorIndex - Color index (0-255)
      Returns:
      ANSI escape sequence for foreground color
    • background

      public static String background(int colorIndex)
      Create a 256-color background ANSI sequence
      Parameters:
      colorIndex - Color index (0-255)
      Returns:
      ANSI escape sequence for background color
    • reset

      public static String reset()
      ANSI reset sequence to clear all formatting
      Returns:
      ANSI reset sequence
    • printError

      public static void printError(String text)
      Convenient static alias to print an error message in red bold text
      Parameters:
      text - The message text
    • printSuccess

      public static void printSuccess(String text)
      Convenient static alias to print a success message in green bold text
      Parameters:
      text - The message text
    • printWarning

      public static void printWarning(String text)
      Convenient static alias to print a warning message in yellow bold text
      Parameters:
      text - The message text
    • printInfo

      public static void printInfo(String text)
      Convenient static alias to print an info message in blue bold text
      Parameters:
      text - The message text
    • printDebug

      public static void printDebug(String text)
      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

      public String readLine() throws IOException
      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

      public String readLine(String customPrompt) throws IOException
      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:
      close in interface AutoCloseable