Class PosixCommands
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for executing shell commands in watch.static classContext for command execution, providing I/O streams and current directory.static classComparator for sorting strings with various options. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringApply style to text using color map.static voidapplyStyle(org.jline.utils.AttributedStringBuilder sb, Map<String, String> colors, String... types) Apply style to AttributedStringBuilder using color map.static voidcat(PosixCommands.Context context, String[] argv) Cat command - concatenate and print files.static voidcd(PosixCommands.Context context, String[] argv) Change directory command.static voidcd(PosixCommands.Context context, String[] argv, Consumer<Path> directoryChanger) Change directory command with directory changer.static voidclear(PosixCommands.Context context, String[] argv) Clear command - clear terminal screen.static voiddate(PosixCommands.Context context, String[] argv) Date command - display current date and time.static voidecho(PosixCommands.Context context, Object[] argv) Echo command - display text (Object array version for compatibility).static voidecho(PosixCommands.Context context, String[] argv) Echo command - display text.protected static Stringget(PosixCommands.Context context, String name) getColorMap(String colorString) Get color map from color string.getColorMap(PosixCommands.Context session, String name, String def) getLsColorMap(String colorString) Get color map for ls command.getLsColorMap(PosixCommands.Context session) static voidgrep(PosixCommands.Context context, String[] argv) Grep command - search text patterns.static voidhead(PosixCommands.Context context, String[] argv) Head command - display first lines of files.static voidless(PosixCommands.Context context, String[] argv) Pager command - view files with less-like interface.static voidls(PosixCommands.Context context, String[] argv) List directory contents command.static voidnano(PosixCommands.Context context, String[] argv) Text editor command - edit files with nano-like interface.protected static OptionsparseOptions(PosixCommands.Context context, String[] usage, Object[] argv) static voidpwd(PosixCommands.Context context, String[] argv) Print working directory command.static voidsleep(PosixCommands.Context context, String[] argv) Sleep command - suspend execution.static voidsort(PosixCommands.Context context, String[] argv) Sort command - sort lines of text.static voidtail(PosixCommands.Context context, String[] argv) Tail command - display last lines of files.static voidttop(PosixCommands.Context context, String[] argv) Thread monitoring command - display and update sorted information about threads.static voidwatch(PosixCommands.Context context, String[] argv) Watch command - execute a command repeatedly and display output.static voidwatch(PosixCommands.Context context, String[] argv, PosixCommands.CommandExecutor executor) Watch command with command executor - execute a command repeatedly and display output.static voidwc(PosixCommands.Context context, String[] argv) Word count command - count lines, words, characters, and bytes.
-
Field Details
-
DEFAULT_LS_COLORS
- See Also:
-
DEFAULT_GREP_COLORS
- See Also:
-
-
Constructor Details
-
PosixCommands
public PosixCommands()
-
-
Method Details
-
cd
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 contextargv- 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 contextargv- command argumentsdirectoryChanger- consumer to perform the actual directory change- Throws:
Exception- if the command fails
-
pwd
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 contextargv- command arguments (should be empty except for --help)- Throws:
Exception- if the command fails
-
echo
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 contextargv- command arguments- Throws:
Exception- if the command fails
-
echo
Echo command - display text (Object array version for compatibility).- Throws:
Exception
-
cat
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 contextargv- command arguments- Throws:
Exception- if the command fails
-
date
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 contextargv- command arguments- Throws:
Exception- if the command fails
-
sleep
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 contextargv- command arguments (should contain the number of seconds)- Throws:
Exception- if the command fails
-
watch
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 contextargv- 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 contextargv- command argumentsexecutor- the command executor for running the watched command- Throws:
Exception- if the command fails
-
ttop
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 contextargv- command arguments- Throws:
Exception- if the command fails
-
nano
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 contextargv- command arguments (typically file names to edit)- Throws:
Exception- if the command fails
-
less
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 contextargv- command arguments (typically file names to view)- Throws:
Exception- if the command fails
-
clear
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 contextargv- command arguments (should be empty except for --help)- Throws:
Exception- if the command fails
-
wc
Word count command - count lines, words, characters, and bytes.- Throws:
Exception
-
head
Head command - display first lines of files.- Throws:
Exception
-
tail
Tail command - display last lines of files.- Throws:
Exception
-
grep
Grep command - search text patterns.- Throws:
Exception
-
sort
Sort command - sort lines of text.- Throws:
Exception
-
ls
List directory contents command.- Throws:
Exception
-
getLsColorMap
-
getColorMap
-
getLsColorMap
-
getColorMap
-
getColorMap
-
applyStyle
-
applyStyle
-
parseOptions
-
get
-