Class ReaderUtils

java.lang.Object
org.jline.reader.impl.ReaderUtils

public class ReaderUtils extends Object
Utility methods for LineReader implementations.

This class provides helper methods for working with LineReader variables and options. It includes methods for retrieving variables of different types (string, boolean, integer, long) with default values, checking if options are set, and calculating string distances for completion matching.

These utilities are primarily used by the LineReader implementation classes to access configuration values in a consistent way, with proper type conversion and default handling.

See Also:
  • Method Details

    • isSet

      public static boolean isSet(LineReader reader, LineReader.Option option)
      Checks if a LineReader option is set.

      This method safely handles null readers by returning false.

      Parameters:
      reader - the LineReader to check, may be null
      option - the option to check
      Returns:
      true if the reader is not null and the option is set, false otherwise
    • getString

      public static String getString(LineReader reader, String name, String def)
      Gets a string variable from a LineReader with a default value.

      This method safely handles null readers by returning the default value.

      Parameters:
      reader - the LineReader to get the variable from, may be null
      name - the name of the variable to get
      def - the default value to return if the variable is not set or the reader is null
      Returns:
      the variable value as a string, or the default value
    • getBoolean

      public static boolean getBoolean(LineReader reader, String name, boolean def)
      Gets a boolean variable from a LineReader with a default value.

      This method safely handles null readers by returning the default value. String values are converted to boolean according to these rules:

      • Empty string, "on", "1", and "true" (case-insensitive) are considered true
      • All other strings are considered false
      Parameters:
      reader - the LineReader to get the variable from, may be null
      name - the name of the variable to get
      def - the default value to return if the variable is not set or the reader is null
      Returns:
      the variable value as a boolean, or the default value
    • getInt

      public static int getInt(LineReader reader, String name, int def)
      Gets an integer variable from a LineReader with a default value.

      This method safely handles null readers by returning the default value. String values are parsed as integers, with a fallback to 0 if parsing fails.

      Parameters:
      reader - the LineReader to get the variable from, may be null
      name - the name of the variable to get
      def - the default value to return if the variable is not set or the reader is null
      Returns:
      the variable value as an integer, or the default value
    • getLong

      public static long getLong(LineReader reader, String name, long def)
      Gets a long variable from a LineReader with a default value.

      This method safely handles null readers by returning the default value. String values are parsed as longs, with a fallback to 0 if parsing fails.

      Parameters:
      reader - the LineReader to get the variable from, may be null
      name - the name of the variable to get
      def - the default value to return if the variable is not set or the reader is null
      Returns:
      the variable value as a long, or the default value
    • distance

      public static int distance(String word, String cand)
      Calculates the edit distance between a word and a candidate string.

      This method is used for fuzzy matching in completion. It uses the Levenshtein distance algorithm to determine how similar two strings are, with special handling for candidates that are longer than the word being matched.

      Parameters:
      word - the word to match against
      cand - the candidate string to check
      Returns:
      the edit distance between the strings (lower values indicate closer matches)