public class TerminalTextUtils extends Object
Modifier and Type | Method | Description |
---|---|---|
static String |
fitString(String string,
int availableColumnSpace) |
Given a string that may or may not contain CJK characters, returns the substring which will fit inside
availableColumnSpace columns. |
static String |
fitString(String string,
int fromColumn,
int availableColumnSpace) |
Given a string that may or may not contain CJK characters, returns the substring which will fit inside
availableColumnSpace columns. |
static String |
getANSIControlSequenceAt(String string,
int index) |
Given a string and an index in that string, returns the ANSI control sequence beginning on this index.
|
static int |
getANSIControlSequenceLength(String string,
int index) |
Given a string and an index in that string, returns the number of characters starting at index that make up
a complete ANSI control sequence.
|
static int |
getColumnIndex(String s,
int stringCharacterIndex) |
Given a string and a character index inside that string, find out what the column index of that character would
be if printed in a terminal.
|
static int |
getColumnIndex(String s,
int stringCharacterIndex,
TabBehaviour tabBehaviour,
int firstCharacterColumnPosition) |
Given a string and a character index inside that string, find out what the column index of that character would
be if printed in a terminal.
|
static int |
getColumnWidth(String s) |
Given a string, returns how many columns this string would need to occupy in a terminal, taking into account that
CJK characters takes up two columns.
|
static int |
getStringCharacterIndex(String s,
int columnIndex) |
This method does the reverse of getColumnIndex, given a String and imagining it has been printed out to the
top-left corner of a terminal, in the column specified by
columnIndex , what is the index of that
character in the string. |
static int |
getTrueWidth(String s) |
Deprecated.
Call
getColumnWidth(s) instead |
static List<String> |
getWordWrappedText(int maxWidth,
String... lines) |
This method will calculate word wrappings given a number of lines of text and how wide the text can be printed.
|
static boolean |
isCharCJK(char c) |
Given a character, is this character considered to be a CJK character?
|
static boolean |
isCharDoubleWidth(char c) |
Checks if a character is expected to be taking up two columns if printed to a terminal.
|
static boolean |
isControlCharacter(char c) |
Checks if a particular character is a control character, in Lanterna this currently means it's 0-31 or 127 in the
ascii table.
|
static boolean |
isPrintableCharacter(char c) |
Checks if a particular character is printable.
|
static void |
updateModifiersFromCSICode(String controlSequence,
StyleSet<?> target,
StyleSet<?> original) |
public static String getANSIControlSequenceAt(String string, int index)
string
- String to scan for control sequencesindex
- Index in the string where the control sequence beginsnull
if there was no control sequence starting at the specified index, otherwise the entire
control sequencepublic static int getANSIControlSequenceLength(String string, int index)
string
- String to scan for control sequencesindex
- Index in the string where the control sequence begins0
if there was no control sequence starting at the specified index, otherwise the length
of the entire control sequencepublic static boolean isCharCJK(char c)
c
- Character to testtrue
if the character is a CJK characterpublic static boolean isCharDoubleWidth(char c)
true
for CJK (Chinese, Japanese and Korean) characters.c
- Character to test if it's double-width when printed to a terminaltrue
if this character is expected to be taking up two columns when printed to the terminal,
otherwise false
public static boolean isControlCharacter(char c)
c
- character to testtrue
if the character is a control character, false
otherwisepublic static boolean isPrintableCharacter(char c)
c
- character to testtrue
if the character is considered printable, false
otherwise@Deprecated public static int getTrueWidth(String s)
getColumnWidth(s)
insteads
- String to measurepublic static int getColumnWidth(String s)
s
- String to check lengthpublic static int getColumnIndex(String s, int stringCharacterIndex) throws StringIndexOutOfBoundsException
stringCharacterIndex
, but if there are CJK characters the value will be different due to CJK
characters taking up two columns in width. If the character at the index in the string is a CJK character itself,
the returned value will be the index of the left-side of character. The tab character is counted as four spaces.s
- String to translate the index fromstringCharacterIndex
- Index within the string to get the terminal column index ofstringCharacterIndex
when it has been writted to a
terminalStringIndexOutOfBoundsException
- if the index given is outside the String length or negativepublic static int getColumnIndex(String s, int stringCharacterIndex, TabBehaviour tabBehaviour, int firstCharacterColumnPosition) throws StringIndexOutOfBoundsException
stringCharacterIndex
, but if there are CJK characters the value will be different due to CJK
characters taking up two columns in width. If the character at the index in the string is a CJK character itself,
the returned value will be the index of the left-side of character.s
- String to translate the index fromstringCharacterIndex
- Index within the string to get the terminal column index oftabBehaviour
- The behavior to use when encountering the tab characterfirstCharacterColumnPosition
- Where on the screen the first character in the string would be printed, this
applies only when you have an alignment-based TabBehaviour
stringCharacterIndex
when it has been writted to a
terminalStringIndexOutOfBoundsException
- if the index given is outside the String length or negativepublic static int getStringCharacterIndex(String s, int columnIndex)
columnIndex
, what is the index of that
character in the string. If the string contains no CJK characters, this will always be the same as
columnIndex
. If the index specified is the right column of a CJK character, the index is the same as if
the column was the left column. So calling getStringCharacterIndex("\u82f1", 0)
and
getStringCharacterIndex("\u82f1", 1)
will both return 0.s
- String to translate the index tocolumnIndex
- Column index of the string written to a terminalcolumnIndex
public static String fitString(String string, int availableColumnSpace)
availableColumnSpace
columns. This method does not handle special cases like tab or new-line.
Calling this method is the same as calling fitString(string, 0, availableColumnSpace)
.
string
- The string to fit inside the availableColumnSpaceavailableColumnSpace
- Number of columns to fit the string insidepublic static String fitString(String string, int fromColumn, int availableColumnSpace)
availableColumnSpace
columns. This method does not handle special cases like tab or new-line.
This overload has a fromColumn
parameter that specified where inside the string to start fitting. Please
notice that fromColumn
is not a character index inside the string, but a column index as if the string
has been printed from the left-most side of the terminal. So if the string is "日本語", fromColumn set to 1 will
not starting counting from the second character ("本") in the string but from the CJK filler character belonging
to "日". If you want to count from a particular character index inside the string, please pass in a substring
and use fromColumn set to 0.
string
- The string to fit inside the availableColumnSpacefromColumn
- From what column of the input string to start fitting (see description above!)availableColumnSpace
- Number of columns to fit the string insidepublic static List<String> getWordWrappedText(int maxWidth, String... lines)
maxWidth
- Maximum number of columns that can be used before word-wrapping is applied, if <= 0 then the
lines will be returned unchangedlines
- Input textmaxWidth
; this may contain more rows than the input textCopyright © 2020. All rights reserved.