Added margin options to line wrapping
This commit is contained in:
@@ -796,21 +796,54 @@ public final class StringUtils {
|
||||
@NotNull
|
||||
public static List<String> lineWrap(@NotNull final List<String> input,
|
||||
final int lineLength) {
|
||||
return lineWrap(input, lineLength, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Line wrap a list of strings while preserving formatting.
|
||||
*
|
||||
* @param input The input list.
|
||||
* @param lineLength The length of each line.
|
||||
* @param preserveMargin If the string has a margin, add it to the next line.
|
||||
* @return The wrapped list.
|
||||
*/
|
||||
@NotNull
|
||||
public static List<String> lineWrap(@NotNull final List<String> input,
|
||||
final int lineLength,
|
||||
final boolean preserveMargin) {
|
||||
return input.stream()
|
||||
.flatMap(line -> lineWrap(line, lineLength).stream())
|
||||
.flatMap(line -> lineWrap(line, lineLength, preserveMargin).stream())
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Line wrap a string while preserving formatting.
|
||||
*
|
||||
* @param input The input string.
|
||||
* @param input The input list.
|
||||
* @param lineLength The length of each line.
|
||||
* @return The wrapped string.
|
||||
* @return The wrapped list.
|
||||
*/
|
||||
@NotNull
|
||||
public static List<String> lineWrap(@NotNull final String input,
|
||||
final int lineLength) {
|
||||
return lineWrap(input, lineLength, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Line wrap a string while preserving formatting.
|
||||
*
|
||||
* @param input The input string.
|
||||
* @param lineLength The length of each line.
|
||||
* @param preserveMargin If the string has a margin, add it to the start of each line.
|
||||
* @return The wrapped string.
|
||||
*/
|
||||
@NotNull
|
||||
public static List<String> lineWrap(@NotNull final String input,
|
||||
final int lineLength,
|
||||
final boolean preserveMargin) {
|
||||
int margin = preserveMargin ? getMargin(input) : 0;
|
||||
TextComponent space = Component.text(" ");
|
||||
|
||||
Component asComponent = toComponent(input);
|
||||
|
||||
// The component contains the text as its children, so the child components
|
||||
@@ -835,12 +868,23 @@ public final class StringUtils {
|
||||
|
||||
List<Component> lines = new ArrayList<>();
|
||||
List<TextComponent> currentLine = new ArrayList<>();
|
||||
boolean isFirstLine = true;
|
||||
|
||||
for (TextComponent letter : letters) {
|
||||
if (currentLine.size() > lineLength && letter.content().isBlank()) {
|
||||
lines.add(Component.join(JoinConfiguration.noSeparators(), currentLine));
|
||||
currentLine.clear();
|
||||
isFirstLine = false;
|
||||
} else {
|
||||
// Add margin if starting a new line.
|
||||
if (currentLine.isEmpty() && !isFirstLine) {
|
||||
if (preserveMargin) {
|
||||
for (int i = 0; i < margin; i++) {
|
||||
currentLine.add(space);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentLine.add(letter);
|
||||
}
|
||||
}
|
||||
@@ -853,6 +897,16 @@ public final class StringUtils {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string's margin.
|
||||
*
|
||||
* @param input The input string.
|
||||
* @return The margin.
|
||||
*/
|
||||
public static int getMargin(@NotNull final String input) {
|
||||
return input.indexOf(input.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for formatting.
|
||||
*/
|
||||
|
||||
@@ -69,3 +69,15 @@ fun Any?.toNiceString(): String =
|
||||
/** @see StringUtils.replaceQuickly */
|
||||
fun String.replaceQuickly(target: String, replacement: String): String =
|
||||
StringUtils.replaceQuickly(this, target, replacement)
|
||||
|
||||
/** @see StringUtils.lineWrap */
|
||||
fun String.lineWrap(width: Int, preserveMargin: Boolean = true): List<String> =
|
||||
StringUtils.lineWrap(this, width, preserveMargin)
|
||||
|
||||
/** @see StringUtils.lineWrap */
|
||||
fun List<String>.lineWrap(width: Int, preserveMargin: Boolean = true): List<String> =
|
||||
StringUtils.lineWrap(this, width, preserveMargin)
|
||||
|
||||
/** @see StringUtils.getMargin */
|
||||
val String.margin: Int
|
||||
get() = StringUtils.getMargin(this)
|
||||
|
||||
Reference in New Issue
Block a user