Class ConsoleEngineImpl

All Implemented Interfaces:
CommandRegistry, ConsoleEngine

public class ConsoleEngineImpl extends JlineCommandRegistry implements ConsoleEngine
Manage console variables, commands and script execution.
  • Constructor Details

  • Method Details

    • setLineReader

      public void setLineReader(LineReader reader)
      Description copied from interface: ConsoleEngine
      Sets the LineReader instance to be used by this console engine.

      The LineReader is used for reading input from the user and providing features like command history, tab completion, and line editing.

      Specified by:
      setLineReader in interface ConsoleEngine
      Parameters:
      reader - the LineReader instance to use
    • isExecuting

      public boolean isExecuting()
      Description copied from interface: ConsoleEngine
      Checks if the console engine is currently executing a script.

      This method determines whether the console engine is in the process of executing a script, as opposed to processing interactive commands.

      Specified by:
      isExecuting in interface ConsoleEngine
      Returns:
      true if the console engine is executing a script, false otherwise
    • setSystemRegistry

      public void setSystemRegistry(SystemRegistry systemRegistry)
      Description copied from interface: ConsoleEngine
      Sets the SystemRegistry instance to be used by this console engine.

      The SystemRegistry is used for executing commands and managing the console environment. It provides access to registered commands and handles command execution.

      Specified by:
      setSystemRegistry in interface ConsoleEngine
      Parameters:
      systemRegistry - the SystemRegistry instance to use
    • setScriptExtension

      public void setScriptExtension(String extension)
      Description copied from interface: ConsoleEngine
      Sets the file name extension used by console scripts.

      This method configures the file extension that the console engine will recognize as indicating a console script. Files with this extension will be treated as console scripts when executing scripts.

      Specified by:
      setScriptExtension in interface ConsoleEngine
      Parameters:
      extension - the file extension to use for console scripts (e.g., ".jsh")
    • hasAlias

      public boolean hasAlias(String name)
      Description copied from interface: ConsoleEngine
      Checks if an alias with the specified name exists.

      This method determines whether an alias has been defined for the specified name. Aliases can be used to create shortcuts for commands or command sequences.

      Specified by:
      hasAlias in interface ConsoleEngine
      Parameters:
      name - the alias name to check
      Returns:
      true if an alias with the specified name exists, false otherwise
    • getAlias

      public String getAlias(String name)
      Description copied from interface: ConsoleEngine
      Returns the value of the alias with the specified name.

      This method retrieves the command or command sequence that the specified alias is defined to represent. Aliases can be used to create shortcuts for commands or command sequences.

      Specified by:
      getAlias in interface ConsoleEngine
      Parameters:
      name - the alias name
      Returns:
      the value of the alias, or null if no alias with the specified name exists
    • getPipes

      public Map<String,List<String>> getPipes()
      Description copied from interface: ConsoleEngine
      Returns all defined pipes.

      This method retrieves a map of all pipes defined in the console engine. Pipes are used to connect the output of one command to the input of another.

      Specified by:
      getPipes in interface ConsoleEngine
      Returns:
      a map of defined pipes, where keys are pipe names and values are pipe definitions
    • getNamedPipes

      public List<String> getNamedPipes()
      Description copied from interface: ConsoleEngine
      Returns the names of all named pipes.

      This method retrieves a list of all named pipes defined in the console engine. Named pipes are pipes that have been given a specific name for easier reference.

      Specified by:
      getNamedPipes in interface ConsoleEngine
      Returns:
      a list of named pipe names
    • scriptCompleters

      public List<Completer> scriptCompleters()
      Description copied from interface: ConsoleEngine
      Returns completers for scripts and variables.

      This method retrieves a list of completers that can be used for tab completion of script names and variable names in the console.

      Specified by:
      scriptCompleters in interface ConsoleEngine
      Returns:
      a list of completers for scripts and variables
    • scripts

      public Map<String,Boolean> scripts()
      Description copied from interface: ConsoleEngine
      Returns all scripts found in the PATH environment variable.

      This method searches for scripts in the directories specified by the PATH environment variable and returns a map of script names to a boolean indicating whether each script is a console script.

      Specified by:
      scripts in interface ConsoleEngine
      Returns:
      a map where keys are script file names and values are true if the script is a console script
    • expandParameters

      public Object[] expandParameters(String[] args) throws Exception
      Description copied from interface: ConsoleEngine
      Substitutes argument references with their values.

      This method expands arguments that reference variables or other values, replacing them with their actual values. For example, a reference like "$VAR" might be replaced with the value of the variable "VAR".

      Specified by:
      expandParameters in interface ConsoleEngine
      Parameters:
      args - the arguments to be expanded
      Returns:
      the expanded arguments with references replaced by their values
      Throws:
      Exception - if an error occurs during expansion
    • expandToList

      public String expandToList(List<String> params)
      Description copied from interface: ConsoleEngine
      Expands a list of script parameters to a string representation.

      This method converts a list of script parameters into a string that can be used in script execution. The parameters may be expanded or formatted according to the console engine's rules.

      Specified by:
      expandToList in interface ConsoleEngine
      Parameters:
      params - the list of script parameters to expand
      Returns:
      a string representation of the expanded parameters list
    • execute

      public Object execute(Path script, String cmdLine, String[] args) throws Exception
      Description copied from interface: ConsoleEngine
      Executes either a JLine script or a ScriptEngine script with the specified arguments.

      This method executes the specified script file with the specified arguments, determining whether it is a JLine script or a ScriptEngine script based on its extension or content.

      Specified by:
      execute in interface ConsoleEngine
      Parameters:
      script - the script file to execute
      cmdLine - the raw command line as entered by the user
      args - the arguments to pass to the script
      Returns:
      the result of executing the script
      Throws:
      Exception - if an error occurs during execution
    • expandCommandLine

      public String expandCommandLine(String line)
      Description copied from interface: ConsoleEngine
      Substitutes a command line with a system registry invoke method call.

      This method expands a command line by replacing it with a call to the system registry's invoke method. This is used to handle command execution through the system registry.

      Specified by:
      expandCommandLine in interface ConsoleEngine
      Parameters:
      line - the command line to be expanded
      Returns:
      the expanded command line with the system registry invoke method call
    • execute

      public Object execute(String cmd, String line, String[] args) throws Exception
      Description copied from interface: ConsoleEngine
      Executes a command line that does not contain a command known by the system registry.

      This method handles the execution of command lines that are not recognized as commands by the system registry. If the line is neither a JLine script nor a ScriptEngine script, it will be evaluated as a ScriptEngine statement.

      Specified by:
      execute in interface ConsoleEngine
      Parameters:
      cmd - the parsed command or script name
      line - the raw command line as entered by the user
      args - the parsed arguments of the command
      Returns:
      the result of executing the command line
      Throws:
      Exception - if an error occurs during execution
    • purge

      public void purge()
      Description copied from interface: ConsoleEngine
      Deletes temporary console variables.

      This method removes all temporary console variables, which are typically created during command execution and are not meant to persist between commands.

      Specified by:
      purge in interface ConsoleEngine
    • putVariable

      public void putVariable(String name, Object value)
      Description copied from interface: ConsoleEngine
      Creates or updates a console variable.

      This method creates a new console variable with the specified name and value, or updates an existing variable if one with the specified name already exists.

      Specified by:
      putVariable in interface ConsoleEngine
      Parameters:
      name - the name of the variable to create or update
      value - the value to assign to the variable
    • getVariable

      public Object getVariable(String name)
      Description copied from interface: ConsoleEngine
      Gets the value of a console variable.

      This method retrieves the value of the console variable with the specified name.

      Specified by:
      getVariable in interface ConsoleEngine
      Parameters:
      name - the name of the variable to get
      Returns:
      the value of the variable, or null if no variable with the specified name exists
    • hasVariable

      public boolean hasVariable(String name)
      Description copied from interface: ConsoleEngine
      Tests if a variable with the specified name exists.

      This method determines whether a console variable with the specified name exists.

      Specified by:
      hasVariable in interface ConsoleEngine
      Parameters:
      name - the name of the variable to check
      Returns:
      true if a variable with the specified name exists, false otherwise
    • executeWidget

      public boolean executeWidget(Object function)
      Description copied from interface: ConsoleEngine
      Executes a widget function.

      This method executes the specified widget function, which can be used to perform custom actions in the console.

      Specified by:
      executeWidget in interface ConsoleEngine
      Parameters:
      function - the widget function to execute
      Returns:
      true if the function was executed successfully, false otherwise
    • consoleOption

      public <T> T consoleOption(String option, T defval)
      Description copied from interface: ConsoleEngine
      Reads a console option value with a default value if the option doesn't exist.

      This method retrieves the value of a console option, returning a default value if the option doesn't exist. Console options are used to configure the behavior of the console engine and its components.

      Specified by:
      consoleOption in interface ConsoleEngine
      Type Parameters:
      T - the type of the option value
      Parameters:
      option - the name of the option to read
      defval - the default value to return if the option doesn't exist
      Returns:
      the value of the option, or the default value if the option doesn't exist
    • setConsoleOption

      public void setConsoleOption(String name, Object value)
      Description copied from interface: ConsoleEngine
      Sets a console option value.

      This method sets the value of a console option. Console options are used to configure the behavior of the console engine and its components.

      Specified by:
      setConsoleOption in interface ConsoleEngine
      Parameters:
      name - the name of the option to set
      value - the value to assign to the option
    • postProcess

      public ConsoleEngine.ExecutionResult postProcess(String line, Object result, String output)
      Description copied from interface: ConsoleEngine
      Post-processes the result of executing a command.

      This method processes the result of executing a command, handling any special cases such as assigning the result to a console variable. If the result is to be assigned to a console variable, this method will return null.

      Specified by:
      postProcess in interface ConsoleEngine
      Parameters:
      line - the command line that was executed
      result - the result of executing the command
      output - the redirected output of the command, if any
      Returns:
      the processed result, or null if the result was assigned to a console variable
    • postProcess

      public ConsoleEngine.ExecutionResult postProcess(Object result)
      Description copied from interface: ConsoleEngine
      Post-processes the result of executing a command.

      This method processes the result of executing a command, handling any special cases. This is a convenience method that calls ConsoleEngine.postProcess(String, Object, String) with a null command line and output.

      Specified by:
      postProcess in interface ConsoleEngine
      Parameters:
      result - the result of executing the command
      Returns:
      the processed result
    • invoke

      public Object invoke(CommandRegistry.CommandSession session, String command, Object... args) throws Exception
      Description copied from interface: CommandRegistry
      Execute a command.
      Specified by:
      invoke in interface CommandRegistry
      Overrides:
      invoke in class AbstractCommandRegistry
      Parameters:
      session - the data of the current command session
      command - the name of the command
      args - arguments of the command
      Returns:
      result of the command execution
      Throws:
      Exception - in case of error
    • trace

      public void trace(Object object)
      Description copied from interface: ConsoleEngine
      Prints an object if tracing is enabled.

      This method prints the specified object to the console if tracing is enabled. Tracing can be used for debugging or logging purposes.

      Specified by:
      trace in interface ConsoleEngine
      Parameters:
      object - the object to print
    • println

      public void println(Object object)
      Description copied from interface: ConsoleEngine
      Prints an object to the console.

      This method prints the specified object to the console, regardless of whether tracing is enabled.

      Specified by:
      println in interface ConsoleEngine
      Parameters:
      object - the object to print
    • persist

      public void persist(Path file, Object object)
      Description copied from interface: ConsoleEngine
      Persists an object to a file.

      This method serializes the specified object and writes it to the specified file. The object can later be read back using the ConsoleEngine.slurp(Path) method.

      Specified by:
      persist in interface ConsoleEngine
      Parameters:
      file - the file to write the object to
      object - the object to persist
    • slurp

      public Object slurp(Path file) throws IOException
      Description copied from interface: ConsoleEngine
      Reads an object from a file.

      This method reads and deserializes an object from the specified file. The object should have been written using the ConsoleEngine.persist(Path, Object) method.

      Specified by:
      slurp in interface ConsoleEngine
      Parameters:
      file - the file to read the object from
      Returns:
      the deserialized object
      Throws:
      IOException - if an I/O error occurs while reading the file