Class Parser

java.lang.Object
org.aesh.terminal.utils.Parser

public final class Parser extends Object
String/Parser util methods
Author:
Ståle W. Pedersen
  • Field Details

  • Method Details

    • formatDisplayList

      public static String formatDisplayList(String[] displayList, int termHeight, int termWidth)
      Format completions so that they look similar to GNU Readline
      Parameters:
      displayList - to format
      termHeight - max height
      termWidth - max width
      Returns:
      formatted string to be outputted
    • formatDisplayList

      public static String formatDisplayList(List<String> displayList, int termHeight, int termWidth)
      Format completions so that they look similar to GNU Readline
      Parameters:
      displayList - to format
      termHeight - max height
      termWidth - max width
      Returns:
      formatted string to be outputted
    • formatDisplayListTerminalString

      public static String formatDisplayListTerminalString(List<TerminalString> displayList, int termHeight, int termWidth)
      Format completions so that they look similar to GNU Readline
      Parameters:
      displayList - to format
      termHeight - max height
      termWidth - max width
      Returns:
      formatted string to be outputted
    • formatDisplayCompactListTerminalString

      public static String formatDisplayCompactListTerminalString(List<TerminalString> displayList, int termWidth)
      Format output to columns with flexible sizes and no redundant space between them
      Parameters:
      displayList - to format
      termWidth - max width
      Returns:
      formatted string to be outputted
    • padRight

      public static String padRight(int n, String s)
      Pad a string to the right with spaces.
      Parameters:
      n - the total width
      s - the string to pad
      Returns:
      the padded string
    • padLeft

      public static String padLeft(int n, String s)
      Pad a string to the left with spaces.
      Parameters:
      n - the total width
      s - the string to pad
      Returns:
      the padded string
    • splitBySizeKeepWords

      public static List<String> splitBySizeKeepWords(String words, int size)
      Split a string by size while keeping words intact.
      Parameters:
      words - the string to split
      size - the maximum line size
      Returns:
      list of split strings
    • trimOptionName

      public static String trimOptionName(String word)
      Remove leading dashes from an option name.
      Parameters:
      word - the option name to trim
      Returns:
      the option name without leading dashes
    • findIfWordEndWithSpace

      public static boolean findIfWordEndWithSpace(String word)
      Check if a word ends with a non-escaped space.
      Parameters:
      word - the word to check
      Returns:
      true if the word ends with a space that is not escaped
    • findStartsWith

      public static String findStartsWith(List<String> completionList)
      Return the biggest common startsWith string
      Parameters:
      completionList - list to compare
      Returns:
      biggest common startsWith string
    • findStartsWithTerminalString

      public static String findStartsWithTerminalString(List<TerminalString> completionList)
      Return the biggest common startsWith string
      Parameters:
      completionList - list to compare
      Returns:
      biggest common startsWith string
    • findWordClosestToCursor

      public static String findWordClosestToCursor(String text, int cursor)
      Find the word closest to the cursor position.
      Parameters:
      text - the text to search in
      cursor - the cursor position
      Returns:
      the word closest to the cursor
    • findCurrentWordFromCursor

      public static String findCurrentWordFromCursor(String text, int cursor)
      Return the word "connected" to cursor, the word ends at cursor position. Note that cursor position starts at 0
      Parameters:
      text - to parse
      cursor - position
      Returns:
      word connected to cursor
    • findEscapedSpaceWordCloseToEnd

      public static String findEscapedSpaceWordCloseToEnd(String text)
      Search backwards for a non-escaped space and only return work containing non-escaped space
      Parameters:
      text - text
      Returns:
      text with only non-escaped space
    • doesStringContainOpenQuote

      public static boolean doesStringContainOpenQuote(String text, int flags)
      Check if a string contain open quotes. Escaped quotes does not count.
      Parameters:
      text - text
      flags - flags, 1 to ignore double quotes, 2 to ignore single quotes. Other values will check for both.
      Returns:
      true if it contains open quotes, else false
    • doesStringContainOpenQuote

      public static boolean doesStringContainOpenQuote(String text)
      Check if a string contains open quotes, checking for both single and double quotes.
      Parameters:
      text - the text to check
      Returns:
      true if the text contains unclosed quotes
    • doWordContainOnlyEscapedSpace

      public static boolean doWordContainOnlyEscapedSpace(String word)
      Check if a word contains only escaped spaces (no regular spaces).
      Parameters:
      word - the word to check
      Returns:
      true if all spaces in the word are escaped
    • doWordContainEscapedSpace

      public static boolean doWordContainEscapedSpace(String word)
      Check if a word contains any escaped spaces.
      Parameters:
      word - the word to check
      Returns:
      true if the word contains at least one escaped space
    • findNumberOfSpacesInWord

      public static int findNumberOfSpacesInWord(String word)
      find number of spaces in the given word. escaped spaces are not counted
      Parameters:
      word - to check
      Returns:
      number of spaces
    • findAllOccurrences

      public static int findAllOccurrences(String word, String pattern)
      Find the number of occurrences of a pattern in a word.
      Parameters:
      word - the word to search in
      pattern - the pattern to search for
      Returns:
      the number of occurrences
    • switchEscapedSpacesToSpacesInList

      public static List<String> switchEscapedSpacesToSpacesInList(List<String> list)
      Convert escaped spaces to regular spaces in all strings in a list.
      Parameters:
      list - the list of strings to convert
      Returns:
      a new list with escaped spaces converted
    • switchEscapedSpacesToSpacesInTerminalStringList

      public static void switchEscapedSpacesToSpacesInTerminalStringList(List<TerminalString> list)
      Convert escaped spaces to regular spaces in all terminal strings in a list.
      Parameters:
      list - the list of terminal strings to modify
    • switchSpacesToEscapedSpacesInWord

      public static String switchSpacesToEscapedSpacesInWord(String word)
      Convert regular spaces to escaped spaces in a word.
      Parameters:
      word - the word to convert
      Returns:
      the word with spaces escaped
    • switchEscapedSpacesToSpacesInWord

      public static String switchEscapedSpacesToSpacesInWord(String word)
      Convert escaped spaces to regular spaces in a word.
      Parameters:
      word - the word to convert
      Returns:
      the word with escaped spaces converted to regular spaces
    • trim

      public static String trim(String buffer)
      Similar to String.trim(), but do not remove spaces that are escaped
      Parameters:
      buffer - input
      Returns:
      trimmed buffer
    • trimInFront

      public static String trimInFront(String buffer)
      Only trim space in front of the word
      Parameters:
      buffer - input
      Returns:
      trimmed buffer
    • findFirstWord

      public static String findFirstWord(String buffer)
      If string contain space, return the text before the first space. Spaces in the beginning and end is removed with Parser.trim(..)
      Parameters:
      buffer - input
      Returns:
      first word
    • containsNonEscapedDollar

      public static boolean containsNonEscapedDollar(String buffer)
      Check if a string contains a non-escaped dollar sign.
      Parameters:
      buffer - the string to check
      Returns:
      true if it contains a non-escaped dollar sign
    • containsNonEscapedSpace

      public static boolean containsNonEscapedSpace(String buffer)
      Check if a string contains a non-escaped space.
      Parameters:
      buffer - the string to check
      Returns:
      true if it contains a non-escaped space
    • stripAwayAnsiCodes

      public static String stripAwayAnsiCodes(String text)
      Remove all ANSI escape codes from a string.
      Parameters:
      text - the text containing ANSI codes
      Returns:
      the text with ANSI codes removed
    • toCodePoints

      public static int[] toCodePoints(String s)
      Convert a string to an array of code points. Uses direct iteration instead of Stream API for better performance.
      Parameters:
      s - the string to convert
      Returns:
      array of code points
    • fromCodePoints

      public static String fromCodePoints(int[] input)
      Convert an array of code points to a string.
      Parameters:
      input - the code points to convert
      Returns:
      the resulting string
    • isTrimmedArrayEmpty

      public static boolean isTrimmedArrayEmpty(int[] input)
      Check if an array contains only space characters (code point 32).
      Parameters:
      input - the array to check
      Returns:
      true if the array is empty or contains only spaces
    • arrayIndexOf

      public static int arrayIndexOf(int[] source, int[] target)
      Find the index of a target array within a source array.
      Parameters:
      source - the source array to search in
      target - the target array to find
      Returns:
      the index of the target in source, or -1 if not found
    • arrayIndexOf

      public static int arrayIndexOf(int[] source, int sourceOffset, int sourceCount, int[] target, int targetOffset, int targetCount, int fromIndex)
      Find the index of a target array within a source array with offsets.
      Parameters:
      source - the source array to search in
      sourceOffset - the starting offset in the source array
      sourceCount - the number of elements to search in source
      target - the target array to find
      targetOffset - the starting offset in the target array
      targetCount - the number of elements to match from target
      fromIndex - the index to start searching from
      Returns:
      the index of the target in source, or -1 if not found
    • arrayContains

      public static boolean arrayContains(int[] source, int[] target)
      Check if a source array contains a target array.
      Parameters:
      source - the source array to search in
      target - the target array to find
      Returns:
      true if source contains target