public class TerminalTextUtils extends Object
Modifier and Type | Method and 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 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 |
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?
Shamelessly stolen from
StackOverflow
where it was contributed by user Rakesh N
|
static boolean |
isCharDoubleWidth(char c)
Checks if a character is expected to be taking up two columns if printed to a terminal.
|
public 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
@Deprecated public static int getTrueWidth(String s)
getColumnWidth(s)
insteadpublic 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.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 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("英", 0)
and
getStringCharacterIndex("英", 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 appliedlines
- Input textmaxWidth
; this may contain more rows than the input textCopyright © 2016. All rights reserved.