Class PosixCommands
- java.lang.Object
-
- org.jline.builtins.PosixCommands
-
public class PosixCommands extends java.lang.ObjectPOSIX-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:
- POSIX Utilities
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePosixCommands.CommandExecutorInterface for executing shell commands in watch.static classPosixCommands.ContextContext for command execution, providing I/O streams and current directory.static classPosixCommands.SortComparatorComparator for sorting strings with various options.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_GREP_COLORSstatic java.lang.StringDEFAULT_LS_COLORS
-
Constructor Summary
Constructors Constructor Description PosixCommands()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringapplyStyle(java.lang.String text, java.util.Map<java.lang.String,java.lang.String> colors, java.lang.String... types)Apply style to text using color map.static voidapplyStyle(org.jline.utils.AttributedStringBuilder sb, java.util.Map<java.lang.String,java.lang.String> colors, java.lang.String... types)Apply style to AttributedStringBuilder using color map.static voidcat(PosixCommands.Context context, java.lang.String[] argv)Cat command - concatenate and print files.static voidcd(PosixCommands.Context context, java.lang.String[] argv)Change directory command.static voidcd(PosixCommands.Context context, java.lang.String[] argv, java.util.function.Consumer<java.nio.file.Path> directoryChanger)Change directory command with directory changer.static voidclear(PosixCommands.Context context, java.lang.String[] argv)Clear command - clear terminal screen.static voiddate(PosixCommands.Context context, java.lang.String[] argv)Date command - display current date and time.static voidecho(PosixCommands.Context context, java.lang.Object[] argv)Echo command - display text (Object array version for compatibility).static voidecho(PosixCommands.Context context, java.lang.String[] argv)Echo command - display text.protected static java.lang.Stringget(PosixCommands.Context context, java.lang.String name)static java.util.Map<java.lang.String,java.lang.String>getColorMap(java.lang.String colorString)Get color map from color string.static java.util.Map<java.lang.String,java.lang.String>getColorMap(java.util.function.Function<java.lang.String,java.lang.Object> variables, java.lang.String name, java.lang.String def)static java.util.Map<java.lang.String,java.lang.String>getColorMap(PosixCommands.Context session, java.lang.String name, java.lang.String def)static java.util.Map<java.lang.String,java.lang.String>getLsColorMap(java.lang.String colorString)Get color map for ls command.static java.util.Map<java.lang.String,java.lang.String>getLsColorMap(PosixCommands.Context session)static voidgrep(PosixCommands.Context context, java.lang.String[] argv)Grep command - search text patterns.static voidhead(PosixCommands.Context context, java.lang.String[] argv)Head command - display first lines of files.static voidless(PosixCommands.Context context, java.lang.String[] argv)Pager command - view files with less-like interface.static voidls(PosixCommands.Context context, java.lang.String[] argv)List directory contents command.static voidnano(PosixCommands.Context context, java.lang.String[] argv)Text editor command - edit files with nano-like interface.protected static OptionsparseOptions(PosixCommands.Context context, java.lang.String[] usage, java.lang.Object[] argv)static voidpwd(PosixCommands.Context context, java.lang.String[] argv)Print working directory command.static voidsleep(PosixCommands.Context context, java.lang.String[] argv)Sleep command - suspend execution.static voidsort(PosixCommands.Context context, java.lang.String[] argv)Sort command - sort lines of text.static voidtail(PosixCommands.Context context, java.lang.String[] argv)Tail command - display last lines of files.static voidttop(PosixCommands.Context context, java.lang.String[] argv)Thread monitoring command - display and update sorted information about threads.static voidwatch(PosixCommands.Context context, java.lang.String[] argv)Watch command - execute a command repeatedly and display output.static voidwatch(PosixCommands.Context context, java.lang.String[] argv, PosixCommands.CommandExecutor executor)Watch command with command executor - execute a command repeatedly and display output.static voidwc(PosixCommands.Context context, java.lang.String[] argv)Word count command - count lines, words, characters, and bytes.
-
-
-
Field Detail
-
DEFAULT_LS_COLORS
public static final java.lang.String DEFAULT_LS_COLORS
- See Also:
- Constant Field Values
-
DEFAULT_GREP_COLORS
public static final java.lang.String DEFAULT_GREP_COLORS
- See Also:
- Constant Field Values
-
-
Method Detail
-
cd
public static void cd(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments- Throws:
java.lang.Exception- if the command fails- See Also:
cd(Context, String[], Consumer)
-
cd
public static void cd(PosixCommands.Context context, java.lang.String[] argv, java.util.function.Consumer<java.nio.file.Path> directoryChanger) throws java.lang.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:
java.lang.Exception- if the command fails
-
pwd
public static void pwd(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments (should be empty except for --help)- Throws:
java.lang.Exception- if the command fails
-
echo
public static void echo(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments- Throws:
java.lang.Exception- if the command fails
-
echo
public static void echo(PosixCommands.Context context, java.lang.Object[] argv) throws java.lang.Exception
Echo command - display text (Object array version for compatibility).- Throws:
java.lang.Exception
-
cat
public static void cat(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments- Throws:
java.lang.Exception- if the command fails
-
date
public static void date(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments- Throws:
java.lang.Exception- if the command fails
-
sleep
public static void sleep(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments (should contain the number of seconds)- Throws:
java.lang.Exception- if the command fails
-
watch
public static void watch(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments- Throws:
java.lang.Exception- if the command fails- See Also:
watch(Context, String[], CommandExecutor)
-
watch
public static void watch(PosixCommands.Context context, java.lang.String[] argv, PosixCommands.CommandExecutor executor) throws java.lang.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:
java.lang.Exception- if the command fails
-
ttop
public static void ttop(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments- Throws:
java.lang.Exception- if the command fails
-
nano
public static void nano(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments (typically file names to edit)- Throws:
java.lang.Exception- if the command fails
-
less
public static void less(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments (typically file names to view)- Throws:
java.lang.Exception- if the command fails
-
clear
public static void clear(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.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 contextargv- command arguments (should be empty except for --help)- Throws:
java.lang.Exception- if the command fails
-
wc
public static void wc(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.Exception
Word count command - count lines, words, characters, and bytes.- Throws:
java.lang.Exception
-
head
public static void head(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.Exception
Head command - display first lines of files.- Throws:
java.lang.Exception
-
tail
public static void tail(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.Exception
Tail command - display last lines of files.- Throws:
java.lang.Exception
-
grep
public static void grep(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.Exception
Grep command - search text patterns.- Throws:
java.lang.Exception
-
sort
public static void sort(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.Exception
Sort command - sort lines of text.- Throws:
java.lang.Exception
-
ls
public static void ls(PosixCommands.Context context, java.lang.String[] argv) throws java.lang.Exception
List directory contents command.- Throws:
java.lang.Exception
-
getLsColorMap
public static java.util.Map<java.lang.String,java.lang.String> getLsColorMap(java.lang.String colorString)
Get color map for ls command.
-
getColorMap
public static java.util.Map<java.lang.String,java.lang.String> getColorMap(java.lang.String colorString)
Get color map from color string.
-
getLsColorMap
public static java.util.Map<java.lang.String,java.lang.String> getLsColorMap(PosixCommands.Context session)
-
getColorMap
public static java.util.Map<java.lang.String,java.lang.String> getColorMap(PosixCommands.Context session, java.lang.String name, java.lang.String def)
-
getColorMap
public static java.util.Map<java.lang.String,java.lang.String> getColorMap(java.util.function.Function<java.lang.String,java.lang.Object> variables, java.lang.String name, java.lang.String def)
-
applyStyle
public static java.lang.String applyStyle(java.lang.String text, java.util.Map<java.lang.String,java.lang.String> colors, java.lang.String... types)Apply style to text using color map.
-
applyStyle
public static void applyStyle(org.jline.utils.AttributedStringBuilder sb, java.util.Map<java.lang.String,java.lang.String> colors, java.lang.String... types)Apply style to AttributedStringBuilder using color map.
-
parseOptions
protected static Options parseOptions(PosixCommands.Context context, java.lang.String[] usage, java.lang.Object[] argv) throws java.lang.Exception
- Throws:
java.lang.Exception
-
get
protected static java.lang.String get(PosixCommands.Context context, java.lang.String name)
-
-