Class PosixCommands

java.lang.Object
org.jline.builtins.PosixCommands

public class PosixCommands extends Object
POSIX-like command implementations for JLine applications.

This class provides implementations of common POSIX commands that can be used in JLine-based applications. The commands are designed to be reusable and independent of any specific command framework.

Available commands include:

  • cat - concatenate and print files
  • echo - display text
  • grep - search text patterns
  • ls - list directory contents
  • pwd - print working directory
  • head - display first lines of files
  • tail - display last lines of files
  • wc - word, line, character, and byte count
  • date - display or set date
  • sleep - suspend execution
  • sort - sort lines of text
  • clear - clear terminal screen
See Also:
  • Field Details

  • Constructor Details

    • PosixCommands

      public PosixCommands()
  • Method Details

    • cd

      public static void cd(PosixCommands.Context context, String[] argv) throws Exception
      Change directory command.

      Changes the current working directory to the specified directory. This version provides validation only and does not actually change the directory. Use the overloaded version with Consumer<Path> for actual directory changing functionality.

      Parameters:
      context - the execution context
      argv - command arguments
      Throws:
      Exception - if the command fails
      See Also:
    • cd

      public static void cd(PosixCommands.Context context, String[] argv, Consumer<Path> directoryChanger) throws Exception
      Change directory command with directory changer.

      Changes the current working directory to the specified directory. Supports POSIX-compliant options and behaviors:

      • No arguments: change to home directory
      • "-": change to previous directory (placeholder)
      • "-P": use physical directory structure (resolve symlinks)
      • "-L": follow symbolic links (default)

      The directoryChanger consumer is called with the resolved path if provided, allowing the caller to actually perform the directory change operation.

      Parameters:
      context - the execution context
      argv - command arguments
      directoryChanger - consumer to perform the actual directory change
      Throws:
      Exception - if the command fails
    • pwd

      public static void pwd(PosixCommands.Context context, String[] argv) throws Exception
      Print working directory command.

      Prints the absolute pathname of the current working directory to standard output. This command is equivalent to the POSIX pwd utility.

      Parameters:
      context - the execution context
      argv - command arguments (should be empty except for --help)
      Throws:
      Exception - if the command fails
    • echo

      public static void echo(PosixCommands.Context context, String[] argv) throws Exception
      Echo command - display text.

      Writes its arguments to standard output, followed by a newline. Supports the -n option to suppress the trailing newline. This command is equivalent to the POSIX echo utility.

      Parameters:
      context - the execution context
      argv - command arguments
      Throws:
      Exception - if the command fails
    • echo

      public static void echo(PosixCommands.Context context, Object[] argv) throws Exception
      Echo command - display text (Object array version for compatibility).
      Throws:
      Exception
    • cat

      public static void cat(PosixCommands.Context context, String[] argv) throws Exception
      Cat command - concatenate and print files.

      Reads files sequentially and writes them to standard output. If no files are specified, or if a file is "-", reads from standard input. Supports the -n option to number output lines. This command is equivalent to the POSIX cat utility.

      Parameters:
      context - the execution context
      argv - command arguments
      Throws:
      Exception - if the command fails
    • date

      public static void date(PosixCommands.Context context, String[] argv) throws Exception
      Date command - display current date and time.

      Displays the current date and time, or a specified date/time. Supports various output formats including ISO 8601, RFC 2822, and RFC 3339. Can parse date strings and display dates from epoch seconds. This command provides functionality similar to the POSIX date utility.

      Supported options:

      • -u, --utc: Display time in UTC
      • -r, --reference: Display time from epoch seconds
      • -d, --date: Parse and display specified date string
      • -I, --iso-8601: Output in ISO 8601 format
      • -R, --rfc-2822: Output in RFC 2822 format
      • --rfc-3339: Output in RFC 3339 format
      Parameters:
      context - the execution context
      argv - command arguments
      Throws:
      Exception - if the command fails
    • sleep

      public static void sleep(PosixCommands.Context context, String[] argv) throws Exception
      Sleep command - suspend execution.

      Suspends execution for a specified number of seconds. This command is equivalent to the POSIX sleep utility.

      Parameters:
      context - the execution context
      argv - command arguments (should contain the number of seconds)
      Throws:
      Exception - if the command fails
    • watch

      public static void watch(PosixCommands.Context context, String[] argv) throws Exception
      Watch command - execute a command repeatedly and display output.

      Executes a command repeatedly at specified intervals and displays the output. This version uses a basic command executor. For full shell integration, use the overloaded version with a CommandExecutor.

      Parameters:
      context - the execution context
      argv - command arguments
      Throws:
      Exception - if the command fails
      See Also:
    • watch

      public static void watch(PosixCommands.Context context, String[] argv, PosixCommands.CommandExecutor executor) throws Exception
      Watch command with command executor - execute a command repeatedly and display output.

      Executes a command repeatedly at specified intervals and displays the output. The command is executed using the provided CommandExecutor, which allows for full shell integration and complex command execution.

      Supported options:

      • -n, --interval: Specify the interval between executions (default: 1 second)
      • -a, --append: Append output instead of clearing the screen
      Parameters:
      context - the execution context
      argv - command arguments
      executor - the command executor for running the watched command
      Throws:
      Exception - if the command fails
    • ttop

      public static void ttop(PosixCommands.Context context, String[] argv) throws Exception
      Thread monitoring command - display and update sorted information about threads.

      Displays real-time information about Java threads in a top-like interface. Shows thread names, states, CPU usage, and other thread-related information. This command delegates to the TTop utility for full functionality.

      Parameters:
      context - the execution context
      argv - command arguments
      Throws:
      Exception - if the command fails
    • nano

      public static void nano(PosixCommands.Context context, String[] argv) throws Exception
      Text editor command - edit files with nano-like interface.

      Opens a text editor with a nano-like interface for editing files. Provides basic text editing functionality with keyboard shortcuts similar to the nano editor. This command delegates to the Nano utility.

      Parameters:
      context - the execution context
      argv - command arguments (typically file names to edit)
      Throws:
      Exception - if the command fails
    • less

      public static void less(PosixCommands.Context context, String[] argv) throws Exception
      Pager command - view files with less-like interface.

      Opens a pager for viewing files with a less-like interface. Supports navigation, searching, and other pager functionality. Can handle multiple files and supports glob patterns. This command delegates to the Less utility for full functionality.

      Parameters:
      context - the execution context
      argv - command arguments (typically file names to view)
      Throws:
      Exception - if the command fails
    • clear

      public static void clear(PosixCommands.Context context, String[] argv) throws Exception
      Clear command - clear terminal screen.

      Clears the terminal screen by sending the appropriate escape sequence. Only works when connected to a TTY terminal. This command is equivalent to the POSIX clear utility.

      Parameters:
      context - the execution context
      argv - command arguments (should be empty except for --help)
      Throws:
      Exception - if the command fails
    • wc

      public static void wc(PosixCommands.Context context, String[] argv) throws Exception
      Word count command - count lines, words, characters, and bytes.
      Throws:
      Exception
    • head

      public static void head(PosixCommands.Context context, String[] argv) throws Exception
      Head command - display first lines of files.
      Throws:
      Exception
    • tail

      public static void tail(PosixCommands.Context context, String[] argv) throws Exception
      Tail command - display last lines of files.
      Throws:
      Exception
    • grep

      public static void grep(PosixCommands.Context context, String[] argv) throws Exception
      Grep command - search text patterns.
      Throws:
      Exception
    • sort

      public static void sort(PosixCommands.Context context, String[] argv) throws Exception
      Sort command - sort lines of text.
      Throws:
      Exception
    • ls

      public static void ls(PosixCommands.Context context, String[] argv) throws Exception
      List directory contents command.
      Throws:
      Exception
    • getLsColorMap

      public static Map<String,String> getLsColorMap(String colorString)
      Get color map for ls command.
    • getColorMap

      public static Map<String,String> getColorMap(String colorString)
      Get color map from color string.
    • getLsColorMap

      public static Map<String,String> getLsColorMap(PosixCommands.Context session)
    • getColorMap

      public static Map<String,String> getColorMap(PosixCommands.Context session, String name, String def)
    • getColorMap

      public static Map<String,String> getColorMap(Function<String,Object> variables, String name, String def)
    • applyStyle

      public static String applyStyle(String text, Map<String,String> colors, String... types)
      Apply style to text using color map.
    • applyStyle

      public static void applyStyle(org.jline.utils.AttributedStringBuilder sb, Map<String,String> colors, String... types)
      Apply style to AttributedStringBuilder using color map.
    • parseOptions

      protected static Options parseOptions(PosixCommands.Context context, String[] usage, Object[] argv) throws Exception
      Throws:
      Exception
    • get

      protected static String get(PosixCommands.Context context, String name)