Package com.calebleavell.jatui.modules
Class TUIModule
java.lang.Object
com.calebleavell.jatui.modules.TUIModule
- Direct Known Subclasses:
ApplicationModule,ContainerModule,FunctionModule,TextInputModule,TextModule
The abstract class for all TUIModules.
A TUIModule is the basic runtime unit of a TUI that can have children and be run. It is immutable for structural properties (e.g, scanner, ansi, etc.), but mutable for runtime state (e.g., currentRunningChild).
Use
Use
TUIModule (and Jatui in general) is not thread-safe and is intended to be managed on a single thread.
A TUIModule is the basic runtime unit of a TUI that can have children and be run. It is immutable for structural properties (e.g, scanner, ansi, etc.), but mutable for runtime state (e.g., currentRunningChild).
Use
TUIModule.Builder to construct a TUIModule (note: all concrete subclasses will have their own builder).
Use
ContainerModule as the minimal implementation for this class. TUIModule (and Jatui in general) is not thread-safe and is intended to be managed on a single thread.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTUIModule.Builder<B extends TUIModule.Builder<B>>Builder forTUIModule.static enumFields that can be recursively updated in the Builder. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ScannerReads from System.in.protected static final org.slf4j.LoggerThe Logger for the module, provided by the slf4j facadeprotected booleanWhether this module is flagged to restart.static final StringThe standard message for when a module isn't named -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTUIModule(TUIModule.Builder<?> builder) Builds aTUIModulebased on the state ofbuilder -
Method Summary
Modifier and TypeMethodDescriptionabstract voidThe module-specific logic to run.org.fusesource.jansi.AnsigetAnsi()If this module displays text, this is the ansi that determines the text styling of that module (e.g., coloring, bolding, etc.).booleanWhether ansi is enabled applies to modules who may display text (e.g.,TextModule).TheApplicationModulethis module is tied to.Recursively searches for a child and returns it if it exists.<T extends TUIModule.Builder<?>>
TRecursively searches for a child and returns it as T, if it exists as type T.When a module is running it runs its children, and its currently running child runs its children, and that child's currently running child runs its children, etc.getCurrentRunningChild(String name) Searches for a child in the current running branch and returns it, if it exists.getName()PrintStream that outputs data to the defined location.The Scanner that reads input from the defined source.voidnavigateTo(TUIModule.Builder<?> module) Runs the module with this module as the parent (important for checking the current running child and getting the current running branch).voidrestart()Terminates the running module and flags it to run again.voidrestartChild(String moduleName) Terminates the running child of this module and flags it to run again.static booleanshallowStructuralEquals(TUIModule first, TUIModule second) Performs a structural equality check on two modules.voidstart()Runs this module as a "root", meaning a new scheduling stack is created.booleanstructuralEquals(TUIModule other) Checks equality for properties given by the builder.voidStop this module from running any more children.voidterminateChild(String moduleName) Terminates a currently running child of this module (seeterminate()).toString()Returns the name of this module.Recursively generates toString with info for this scene and all children.toTreeString(int maxDepth) Recursively generates toString with info for this scene and all children.
-
Field Details
-
DEFAULT_SCANNER
Reads from System.in.
It is strongly discouraged to close this Scanner, as it would close the scanner for all modules using it and potentially kill input for the entire TUI. -
UNNAMED_ERROR
The standard message for when a module isn't named- See Also:
-
logger
protected static final org.slf4j.Logger loggerThe Logger for the module, provided by the slf4j facade -
restart
protected boolean restartWhether this module is flagged to restart.
If restart is true while the module is running, it will run again after completion.
-
-
Constructor Details
-
TUIModule
Builds aTUIModulebased on the state ofbuilder- Parameters:
builder- TheTUIModule.Builderthat is building the module.
-
-
Method Details
-
getName
- Returns:
name
-
getChildren
- Returns:
children
-
getChild
Recursively searches for a child and returns it if it exists.- Parameters:
name- The name of the child to search for.- Returns:
- The child, if it exists.
-
getChild
Recursively searches for a child and returns it as T, if it exists as type T.- Type Parameters:
T- The type of the child- Parameters:
name- The name of the child to search for.type- The type of the child to search for
(e.g.,ContainerModule.Builder.class)- Returns:
- The child as T, if it exists.
-
start
public void start()Runs this module as a "root", meaning a new scheduling stack is created. It will run all children from this module.
For most use cases, only the top-level module will need to be run via this method (most likely anApplicationModule). If you need to manually run a module that is a child of another module that will be running, usenavigateTo(Builder)orModuleFactory.run(String, TUIModule, String). This ensures a unified scheduler across all running modules. -
doRunLogic
public abstract void doRunLogic()The module-specific logic to run.- Implementation Requirements:
- The children of this module are automatically run after this, so there is no need to implement logic that handles scheduling the behavior of children.
-
terminate
public void terminate()Stop this module from running any more children.
Recursively propagates the termination to its children.
If a terminated module is run again, it will no longer be terminated. -
terminateChild
Terminates a currently running child of this module (seeterminate()).
All children higher up in the running branch will not be terminated.- Parameters:
moduleName- The name of the child to terminate.
-
restart
public void restart()Terminates the running module and flags it to run again.
Note: restarting a module that isn't running does nothing. -
restartChild
Terminates the running child of this module and flags it to run again.
Note: restarting a module that isn't running does nothing. -
getCurrentRunningChild
- Returns:
- The
currentRunningChild. Will be null if there is no currently running child.
-
getCurrentRunningChild
Searches for a child in the current running branch and returns it, if it exists.
(seegetCurrentRunningBranch())- Parameters:
name- The name of the child to search for- Returns:
- The matching child in the current running branch, or null if not found.
-
getCurrentRunningBranch
When a module is running it runs its children, and its currently running child runs its children, and that child's currently running child runs its children, etc.
This creates a "branch" of children that are currently running, stemming from this module to the leaf child at the very end.
Note: the leaf child may also have children but simply isn't running them yet.- Returns:
- The current running branch of modules stemming from this module.
-
getApplication
TheApplicationModulethis module is tied to. An application module is primarily used for TUI input storage, as well as for providing a clean way to enter/exit the TUI.- Returns:
- The
ApplicationModulethat this module is tied to.
-
getAnsi
public org.fusesource.jansi.Ansi getAnsi()If this module displays text, this is the ansi that determines the text styling of that module (e.g., coloring, bolding, etc.). Ansi is provided by Jansi.- Returns:
- The ansi stored in the module.
-
getScanner
The Scanner that reads input from the defined source. It is set to System.in by default (provided byDEFAULT_SCANNER).- Returns:
- The reference to the Scanner used by this module (Note that not every module will use the Scanner).
-
getPrintStream
PrintStream that outputs data to the defined location. It is set toSystem.inby default.- Returns:
- The reference to the PrintStream used by this module (Note that not every module will use the PrintStream).
-
getAnsiEnabled
public boolean getAnsiEnabled()Whether ansi is enabled applies to modules who may display text (e.g.,TextModule). If ansi is disabled, only the raw text is displayed.- Returns:
- Whether ansi is enabled for this module.
-
toString
Returns the name of this module. For a formatted string of the module hierarchy stemming from this module, seetoTreeString(). -
toTreeString
Recursively generates toString with info for this scene and all children. Automatically caps recursion to 6 iterations.- Returns:
- formatted string
-
toTreeString
Recursively generates toString with info for this scene and all children.- Parameters:
maxDepth- How deep to allow the recursion to go- Returns:
- formatted string
-
structuralEquals
Checks equality for properties given by the builder.
For TUIModule, this includes:
- name
- application (Note: checks structural equality, not reference equality)
- children
- ansi
- scanner
- printStream
- enableAnsi
Note: Runtime properties (e.g., currentRunningChild, terminated), are not considered.
- Parameters:
other- The TUIModule to compare- Returns:
- true if this module equals
otheraccording to builder-provided properties - Implementation Note:
- This method intentionally does not override
Object.equals(Object)so that things like HashMaps still check by method reference. This method is merely for checking structural equality, which is generally only necessary for manual testing.
-
shallowStructuralEquals
Performs a structural equality check on two modules. Does not check the equality of children.
Checks equality ofname,application,ansi,scanner,printStream, andenableAnsi. Structural equality, not reference equality, is checked forapplication.
-