Class Commands

java.lang.Object
org.jline.builtins.Commands

public class Commands extends Object
Provides built-in commands for JLine applications.

This class contains implementations of common terminal commands and utilities such as:

  • tmux - Terminal multiplexer
  • nano - Text editor
  • less - View file contents with pagination
  • history - Display and manage command history
  • complete - Edit command-specific tab completions
  • widget - Manipulate line reader widgets
  • keymap - Manipulate keymaps
  • setopt/unsetopt - Set/unset line reader options
  • setvar - Set line reader variables
  • colors - View color tables and ANSI styles
  • highlighter - Manage syntax highlighting themes

These commands can be used to provide a familiar command-line interface in JLine-based applications.

  • Constructor Details

    • Commands

      public Commands()
  • Method Details

    • tmux

      public static void tmux(Terminal terminal, PrintStream out, PrintStream err, Supplier<Object> getter, Consumer<Object> setter, Consumer<Terminal> runner, String[] argv) throws Exception
      Implements a terminal multiplexer similar to tmux.
      Parameters:
      terminal - the terminal to use
      out - the output stream
      err - the error stream
      getter - supplier to get the current tmux instance
      setter - consumer to set the current tmux instance
      runner - consumer to run a terminal
      argv - command arguments
      Throws:
      Exception - if an error occurs
    • nano

      public static void nano(Terminal terminal, PrintStream out, PrintStream err, Path currentDir, String[] argv) throws Exception
      Launches the nano text editor with the specified arguments. This is a convenience method that calls nano(Terminal, PrintStream, PrintStream, Path, String[], ConfigurationPath) with a null ConfigurationPath.
      Parameters:
      terminal - the terminal to use
      out - the output stream
      err - the error stream
      currentDir - the current directory
      argv - command arguments
      Throws:
      Exception - if an error occurs
    • nano

      public static void nano(Terminal terminal, PrintStream out, PrintStream err, Path currentDir, String[] argv, ConfigurationPath configPath) throws Exception
      Launches the nano text editor with the specified arguments.
      Parameters:
      terminal - the terminal to use
      out - the output stream
      err - the error stream
      currentDir - the current directory
      argv - command arguments
      configPath - the configuration path or null for default
      Throws:
      Exception - if an error occurs
    • less

      public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err, Path currentDir, Object[] argv) throws Exception
      Launches the less pager with the specified arguments. This is a convenience method that calls less(Terminal, InputStream, PrintStream, PrintStream, Path, Object[], ConfigurationPath) with a null ConfigurationPath.
      Parameters:
      terminal - the terminal to use
      in - the input stream
      out - the output stream
      err - the error stream
      currentDir - the current directory
      argv - command arguments
      Throws:
      Exception - if an error occurs
    • less

      public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err, Path currentDir, Object[] argv, ConfigurationPath configPath) throws Exception
      Launches the less pager with the specified arguments.
      Parameters:
      terminal - the terminal to use
      in - the input stream
      out - the output stream
      err - the error stream
      currentDir - the current directory
      argv - command arguments (can be String or Source objects)
      configPath - the configuration path or null for default
      Throws:
      Exception - if an error occurs
    • findFiles

      protected static List<Path> findFiles(Path root, String files) throws IOException
      Finds files matching a glob pattern.
      Parameters:
      root - the root directory to search from
      files - the glob pattern to match
      Returns:
      a list of matching paths
      Throws:
      IOException - if an I/O error occurs
    • history

      public static void history(LineReader reader, PrintStream out, PrintStream err, Path currentDir, String[] argv) throws Exception
      Displays or manipulates the command history.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      currentDir - the current directory
      argv - command arguments
      Throws:
      Exception - if an error occurs
    • historyId

      private static int historyId(int id, int minId, int maxId)
      Resolves a history ID, handling negative indices.
      Parameters:
      id - the history ID to resolve
      minId - the minimum valid ID
      maxId - the maximum valid ID
      Returns:
      the resolved history ID
    • retrieveHistoryId

      private static int retrieveHistoryId(History history, String s) throws IllegalArgumentException
      Retrieves a history entry ID from a string, which can be either a number or a command prefix.
      Parameters:
      history - the history to search in
      s - the string to parse
      Returns:
      the history ID
      Throws:
      IllegalArgumentException - if the history event is not found
    • complete

      public static void complete(LineReader reader, PrintStream out, PrintStream err, Map<String,List<Completers.CompletionData>> completions, String[] argv) throws Options.HelpException
      Edits command-specific tab completions.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      completions - the map of command completions
      argv - command arguments
      Throws:
      Options.HelpException - if help is requested or an error occurs
    • widget

      public static void widget(LineReader reader, PrintStream out, PrintStream err, Function<String,Widget> widgetCreator, String[] argv) throws Exception
      Manipulates line reader widgets.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      widgetCreator - function to create widgets from function names
      argv - command arguments
      Throws:
      Exception - if an error occurs
    • keymap

      public static void keymap(LineReader reader, PrintStream out, PrintStream err, String[] argv) throws Options.HelpException
      Manipulates line reader keymaps.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      argv - command arguments
      Throws:
      Options.HelpException - if help is requested or an error occurs
    • setopt

      public static void setopt(LineReader reader, PrintStream out, PrintStream err, String[] argv) throws Options.HelpException
      Sets line reader options.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      argv - command arguments
      Throws:
      Options.HelpException - if help is requested or an error occurs
    • unsetopt

      public static void unsetopt(LineReader reader, PrintStream out, PrintStream err, String[] argv) throws Options.HelpException
      Unsets line reader options.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      argv - command arguments
      Throws:
      Options.HelpException - if help is requested or an error occurs
    • doSetOpts

      private static void doSetOpts(LineReader reader, PrintStream out, PrintStream err, List<String> options, boolean match, boolean set)
      Helper method to set or unset line reader options.
      Parameters:
      reader - the line reader
      out - the output stream
      err - the error stream
      options - the list of options to set/unset
      match - whether to use pattern matching
      set - whether to set (true) or unset (false) the options
    • displayValue

      private static void displayValue(StringBuilder sb, Object value)
      Helper method to display a keymap binding value.
      Parameters:
      sb - the string builder to append to
      value - the binding value to display
    • setvar

      public static void setvar(LineReader lineReader, PrintStream out, PrintStream err, String[] argv) throws Options.HelpException
      Sets or displays line reader variables.
      Parameters:
      lineReader - the line reader
      out - the output stream
      err - the error stream
      argv - command arguments
      Throws:
      Options.HelpException - if help is requested or an error occurs
    • colors

      public static void colors(Terminal terminal, PrintStream out, String[] argv) throws Options.HelpException, IOException
      Displays color tables and ANSI styles.
      Parameters:
      terminal - the terminal to use
      out - the output stream
      argv - command arguments
      Throws:
      Options.HelpException - if help is requested or an error occurs
      IOException - if an I/O error occurs
    • highlighter

      public static void highlighter(LineReader lineReader, Terminal terminal, PrintStream out, PrintStream err, String[] argv, ConfigurationPath configPath) throws Options.HelpException
      Manages syntax highlighting themes.
      Parameters:
      lineReader - the line reader
      terminal - the terminal to use
      out - the output stream
      err - the error stream
      argv - command arguments
      configPath - the configuration path
      Throws:
      Options.HelpException - if help is requested or an error occurs
    • switchTheme

      private static void switchTheme(PrintStream err, Path config, String theme)
      Helper method to switch the syntax highlighting theme.
      Parameters:
      err - the error stream
      config - the configuration path
      theme - the theme to switch to
    • replaceFileName

      private static String replaceFileName(Path path, String name)
      Helper method to replace a file name in a path.
      Parameters:
      path - the original path
      name - the new file name
      Returns:
      the path with the file name replaced
    • compileStyle

      private static AttributedStyle compileStyle(String reference, String colorDef)
      Helper method to compile a style from a color definition.
      Parameters:
      reference - the style reference name
      colorDef - the color definition
      Returns:
      the compiled AttributedStyle