Package org.aesh.terminal.utils
Class Parser
java.lang.Object
org.aesh.terminal.utils.Parser
String/Parser util methods
- Author:
- Ståle W. Pedersen
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final charBackslash character constant.static final charDollar sign character constant.static final charDouble quote character constant.static final charSingle quote character constant.static final StringSpace string constant.static final charSpace character constant. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanarrayContains(int[] source, int[] target) Check if a source array contains a target array.static intarrayIndexOf(int[] source, int[] target) Find the index of a target array within a source array.static intarrayIndexOf(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.static booleancontainsNonEscapedDollar(String buffer) Check if a string contains a non-escaped dollar sign.static booleancontainsNonEscapedSpace(String buffer) Check if a string contains a non-escaped space.static booleanCheck if a string contains open quotes, checking for both single and double quotes.static booleandoesStringContainOpenQuote(String text, int flags) Check if a string contain open quotes.static booleanCheck if a word contains any escaped spaces.static booleanCheck if a word contains only escaped spaces (no regular spaces).static intfindAllOccurrences(String word, String pattern) Find the number of occurrences of a pattern in a word.static StringfindCurrentWordFromCursor(String text, int cursor) Return the word "connected" to cursor, the word ends at cursor position.static StringSearch backwards for a non-escaped space and only return work containing non-escaped spacestatic StringfindFirstWord(String buffer) If string contain space, return the text before the first space.static booleanfindIfWordEndWithSpace(String word) Check if a word ends with a non-escaped space.static intfind number of spaces in the given word. escaped spaces are not countedstatic StringfindStartsWith(List<String> completionList) Return the biggest common startsWith stringstatic StringfindStartsWithTerminalString(List<TerminalString> completionList) Return the biggest common startsWith stringstatic StringfindWordClosestToCursor(String text, int cursor) Find the word closest to the cursor position.static StringformatDisplayCompactListTerminalString(List<TerminalString> displayList, int termWidth) Format output to columns with flexible sizes and no redundant space between themstatic StringformatDisplayList(String[] displayList, int termHeight, int termWidth) Format completions so that they look similar to GNU Readlinestatic StringformatDisplayList(List<String> displayList, int termHeight, int termWidth) Format completions so that they look similar to GNU Readlinestatic StringformatDisplayListTerminalString(List<TerminalString> displayList, int termHeight, int termWidth) Format completions so that they look similar to GNU Readlinestatic StringfromCodePoints(int[] input) Convert an array of code points to a string.static booleanisTrimmedArrayEmpty(int[] input) Check if an array contains only space characters (code point 32).static StringPad a string to the left with spaces.static StringPad a string to the right with spaces.splitBySizeKeepWords(String words, int size) Split a string by size while keeping words intact.static StringstripAwayAnsiCodes(String text) Remove all ANSI escape codes from a string.Convert escaped spaces to regular spaces in all strings in a list.static voidConvert escaped spaces to regular spaces in all terminal strings in a list.static StringConvert escaped spaces to regular spaces in a word.static StringConvert regular spaces to escaped spaces in a word.static int[]Convert a string to an array of code points.static StringSimilar to String.trim(), but do not remove spaces that are escapedstatic StringtrimInFront(String buffer) Only trim space in front of the wordstatic StringtrimOptionName(String word) Remove leading dashes from an option name.
-
Field Details
-
SPACE
Space string constant.- See Also:
-
SPACE_CHAR
public static final char SPACE_CHARSpace character constant.- See Also:
-
BACK_SLASH
public static final char BACK_SLASHBackslash character constant.- See Also:
-
SINGLE_QUOTE
public static final char SINGLE_QUOTESingle quote character constant.- See Also:
-
DOUBLE_QUOTE
public static final char DOUBLE_QUOTEDouble quote character constant.- See Also:
-
DOLLAR
public static final char DOLLARDollar sign character constant.- See Also:
-
-
Method Details
-
formatDisplayList
Format completions so that they look similar to GNU Readline- Parameters:
displayList- to formattermHeight- max heighttermWidth- max width- Returns:
- formatted string to be outputted
-
formatDisplayList
Format completions so that they look similar to GNU Readline- Parameters:
displayList- to formattermHeight- max heighttermWidth- 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 formattermHeight- max heighttermWidth- 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 formattermWidth- max width- Returns:
- formatted string to be outputted
-
padRight
Pad a string to the right with spaces.- Parameters:
n- the total widths- the string to pad- Returns:
- the padded string
-
padLeft
Pad a string to the left with spaces.- Parameters:
n- the total widths- the string to pad- Returns:
- the padded string
-
splitBySizeKeepWords
Split a string by size while keeping words intact.- Parameters:
words- the string to splitsize- the maximum line size- Returns:
- list of split strings
-
trimOptionName
Remove leading dashes from an option name.- Parameters:
word- the option name to trim- Returns:
- the option name without leading dashes
-
findIfWordEndWithSpace
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
Return the biggest common startsWith string- Parameters:
completionList- list to compare- Returns:
- biggest common startsWith string
-
findStartsWithTerminalString
Return the biggest common startsWith string- Parameters:
completionList- list to compare- Returns:
- biggest common startsWith string
-
findWordClosestToCursor
Find the word closest to the cursor position.- Parameters:
text- the text to search incursor- the cursor position- Returns:
- the word closest to the cursor
-
findCurrentWordFromCursor
Return the word "connected" to cursor, the word ends at cursor position. Note that cursor position starts at 0- Parameters:
text- to parsecursor- position- Returns:
- word connected to cursor
-
findEscapedSpaceWordCloseToEnd
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
Check if a string contain open quotes. Escaped quotes does not count.- Parameters:
text- textflags- flags,1to ignore double quotes,2to ignore single quotes. Other values will check for both.- Returns:
- true if it contains open quotes, else false
-
doesStringContainOpenQuote
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
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
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
find number of spaces in the given word. escaped spaces are not counted- Parameters:
word- to check- Returns:
- number of spaces
-
findAllOccurrences
Find the number of occurrences of a pattern in a word.- Parameters:
word- the word to search inpattern- the pattern to search for- Returns:
- the number of occurrences
-
switchEscapedSpacesToSpacesInList
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
Convert escaped spaces to regular spaces in all terminal strings in a list.- Parameters:
list- the list of terminal strings to modify
-
switchSpacesToEscapedSpacesInWord
Convert regular spaces to escaped spaces in a word.- Parameters:
word- the word to convert- Returns:
- the word with spaces escaped
-
switchEscapedSpacesToSpacesInWord
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
Similar to String.trim(), but do not remove spaces that are escaped- Parameters:
buffer- input- Returns:
- trimmed buffer
-
trimInFront
Only trim space in front of the word- Parameters:
buffer- input- Returns:
- trimmed buffer
-
findFirstWord
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
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
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
Remove all ANSI escape codes from a string.- Parameters:
text- the text containing ANSI codes- Returns:
- the text with ANSI codes removed
-
toCodePoints
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
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 intarget- 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 insourceOffset- the starting offset in the source arraysourceCount- the number of elements to search in sourcetarget- the target array to findtargetOffset- the starting offset in the target arraytargetCount- the number of elements to match from targetfromIndex- 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 intarget- the target array to find- Returns:
- true if source contains target
-