Class AliasManager

java.lang.Object
org.aesh.readline.alias.AliasManager

public class AliasManager extends Object
Manages Aliases
Author:
Ståle W. Pedersen
  • Constructor Details

    • AliasManager

      public AliasManager(File aliasFile, boolean persistAlias) throws IOException
      Creates a new AliasManager with the specified alias file and persistence setting. If the alias file exists, aliases are read from it during initialization.
      Parameters:
      aliasFile - the file to read and persist aliases from/to, may be null
      persistAlias - if true, aliases will be persisted to the alias file
      Throws:
      IOException - if an I/O error occurs while reading the alias file
  • Method Details

    • verifyNoNewAliasConflict

      public boolean verifyNoNewAliasConflict(String aliasName) throws AliasConflictException
      It is not allowed to create an alias if it conflicts with a command already present
      Parameters:
      aliasName - name of the alias
      Returns:
      true if there is no conflict
      Throws:
      AliasConflictException - if the alias name conflicts with an existing command
    • persist

      public void persist()
      Persists all aliases to the alias file. This method writes all current aliases to the configured alias file if persistence is enabled and an alias file was specified. The aliases are sorted before writing. If the file already exists, it is deleted and recreated.
    • printAllAliases

      public String printAllAliases()
      Returns a formatted string containing all defined aliases sorted alphabetically. Each alias is printed on a separate line in the format "alias name='value'".
      Returns:
      a string representation of all aliases, with each alias on a separate line
    • getAlias

      public Optional<Alias> getAlias(String name)
      Retrieves an alias by its name.
      Parameters:
      name - the name of the alias to retrieve
      Returns:
      an Optional containing the Alias if found, or an empty Optional if not found
    • getAliasName

      public Optional<String> getAliasName(String input)
      Expands an alias if the first word of the input matches an alias name. If a matching alias is found, returns the alias value with the remainder of the input appended.
      Parameters:
      input - the input string to check for alias expansion
      Returns:
      an Optional containing the expanded command if an alias matches, or an empty Optional if no alias matches the first word
    • findAllMatchingNames

      public List<String> findAllMatchingNames(String name)
      Finds all alias names that start with the given prefix. This method is useful for tab completion functionality.
      Parameters:
      name - the prefix to match against alias names
      Returns:
      a list of alias names that start with the given prefix
    • getAllNames

      public List<String> getAllNames()
      Returns a list of all defined alias names.
      Returns:
      a list containing the names of all defined aliases
    • removeAlias

      public String removeAlias(String buffer)
      Removes one or more aliases based on the unalias command input. The buffer should contain the unalias command followed by one or more alias names to remove, separated by spaces.
      Parameters:
      buffer - the unalias command string containing alias names to remove
      Returns:
      null if all aliases were successfully removed, an error message if an alias was not found, or usage information if the command is invalid
    • parseAlias

      public String parseAlias(String buffer)
      Parses an alias command and either creates a new alias, lists specified aliases, or returns all aliases.

      Supported formats:

      • "alias" - returns all defined aliases
      • "alias --help" - returns usage information
      • "alias name=value" - creates or updates an alias
      • "alias name1 name2" - lists the specified aliases
      Parameters:
      buffer - the alias command string to parse
      Returns:
      null if an alias was successfully created, a string containing alias definitions when listing, an error message if the command is invalid, or usage information
    • aliasUsage

      public String aliasUsage()
      Returns the usage information for the alias command.
      Returns:
      a string containing the alias command usage syntax
    • unaliasUsage

      public String unaliasUsage()
      Returns the usage information for the unalias command.
      Returns:
      a string containing the unalias command usage syntax