Package me.scolastico.tools.console
Class ConsoleManager
- java.lang.Object
-
- me.scolastico.tools.console.ConsoleManager
-
public class ConsoleManager extends java.lang.Object
ConsoleManager for easy creating of console application which looks like a normal console window. Also protects input from being overwritten by the output. Only works with LINUX and WINDOWS. Known problems with IDE consoles! https://go.scolasti.co/ansiproblems
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConsoleManager.ExitCommand
Default command for exiting the software through System.exit(0).static class
ConsoleManager.ListCommandsCommand
Default command for listing all available commands.
-
Constructor Summary
Constructors Constructor Description ConsoleManager()
-
Method Summary
Modifier and Type Method Description static void
disable()
Stop the ConsoleManager.static void
enable()
Start the ConsoleManager with daemon threads.static void
enable(boolean daemon)
Start the ConsoleManager.static void
enable(boolean daemon, boolean alsoErrStream)
Start the ConsoleManager.static int
executeCommand(java.lang.String[] args)
Execute a command manually.static int
executeCommand(java.lang.String command, java.lang.String[] args)
Execute a command manually.static java.lang.String[]
getLastOutput()
Get the last outputted lines.static boolean
isEnabled()
Is the ConsoleManager enabled?static void
registerCommand(java.util.concurrent.Callable<java.lang.Integer> command)
Register a command.static void
registerPreOutputModificator(ConsolePreOutputModificatorInterface modificator)
Add a console pre output modificator to change the output before its send to the console.static void
setAppendTime(boolean appendTime)
Should the current time stamp be added before an output? Will use "yyyy/MM/dd HH:mm:ss" format.static void
setNotFoundMessage(java.lang.String notFoundMessage)
If an command is not found will respond with this message.static void
setPrefix(java.lang.String prefix)
Set the prefix for the input.static void
setRegisterDefaults(boolean registerDefaults)
Should the default commands are registered if no command with the same name is already registered?static void
setStoreLineNumber(int storeLineNumber)
Set how much lines from the output should be stored in an string array.
-
-
-
Method Detail
-
disable
public static void disable() throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.io.IOException
Stop the ConsoleManager. This function also resets the most internal values including the last output array.- Throws:
java.lang.InterruptedException
- InterruptedException from Thread.sleep() while waiting for the thread shutdown.java.util.concurrent.TimeoutException
- If the threads are not stopping this exception will be triggered after 10 seconds.java.io.IOException
- IOExceptions from the System.out and System.in read/write actions.
-
enable
public static void enable() throws java.io.IOException
Start the ConsoleManager with daemon threads.- Throws:
java.io.IOException
- IOExceptions from the System.out and System.in read/write actions.
-
enable
public static void enable(boolean daemon) throws java.io.IOException
Start the ConsoleManager.- Parameters:
daemon
- Start the threads as daemon's? Default is true.- Throws:
java.io.IOException
- IOExceptions from the System.out and System.in read/write actions.
-
enable
public static void enable(boolean daemon, boolean alsoErrStream) throws java.io.IOException
Start the ConsoleManager.- Parameters:
daemon
- Start the threads as daemon's? Default is true.alsoErrStream
- Also care about the error stream? Default is false.- Throws:
java.io.IOException
- IOExceptions from the System.out and System.in read/write actions.
-
executeCommand
public static int executeCommand(java.lang.String[] args)
Execute a command manually.- Parameters:
args
- Arguments with command at position 0.- Returns:
- Status code from command or 404 if command not found.
-
executeCommand
public static int executeCommand(java.lang.String command, java.lang.String[] args)
Execute a command manually.- Parameters:
command
- The command name.args
- Arguments for the command.- Returns:
- Status code from command or 404 if command not found.
-
isEnabled
public static boolean isEnabled()
Is the ConsoleManager enabled?- Returns:
- True if the ConsoleManager is enabled.
-
getLastOutput
public static java.lang.String[] getLastOutput()
Get the last outputted lines.- Returns:
- The last lines from System.out.
-
setStoreLineNumber
public static void setStoreLineNumber(int storeLineNumber)
Set how much lines from the output should be stored in an string array. If the max is reached old lines will be overwritten.- Parameters:
storeLineNumber
- Default is 1024.
-
setRegisterDefaults
public static void setRegisterDefaults(boolean registerDefaults)
Should the default commands are registered if no command with the same name is already registered?- Parameters:
registerDefaults
- Default is true.
-
setPrefix
public static void setPrefix(java.lang.String prefix)
Set the prefix for the input.- Parameters:
prefix
- Default is "> "
-
setNotFoundMessage
public static void setNotFoundMessage(java.lang.String notFoundMessage)
If an command is not found will respond with this message. To disable set to NULL.- Parameters:
notFoundMessage
- Default is "Command '%' not found! Try 'list-commands' to get a list of all commands!".
-
setAppendTime
public static void setAppendTime(boolean appendTime)
Should the current time stamp be added before an output? Will use "yyyy/MM/dd HH:mm:ss" format.- Parameters:
appendTime
- Default is true.
-
registerCommand
public static void registerCommand(java.util.concurrent.Callable<java.lang.Integer> command)
Register a command. If the same command is registered twice it will overwrite the old one. The command will be defined though the Command annotation ("name" value). This guide show you how to create a command: https://picocli.info/quick-guide.html- Parameters:
command
- Picocli callable object which will be registered as a valid command. All picocli features are available.
-
registerPreOutputModificator
public static void registerPreOutputModificator(ConsolePreOutputModificatorInterface modificator)
Add a console pre output modificator to change the output before its send to the console.- Parameters:
modificator
- The modificator to register.
-
-