diff --git a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java index d5653e88..3883d766 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java @@ -796,21 +796,54 @@ public final class StringUtils { @NotNull public static List lineWrap(@NotNull final List 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 lineWrap(@NotNull final List 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 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 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 lines = new ArrayList<>(); List 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. */ diff --git a/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt b/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt index 1a3005fe..4025dd13 100644 --- a/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt +++ b/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt @@ -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 = + StringUtils.lineWrap(this, width, preserveMargin) + +/** @see StringUtils.lineWrap */ +fun List.lineWrap(width: Int, preserveMargin: Boolean = true): List = + StringUtils.lineWrap(this, width, preserveMargin) + +/** @see StringUtils.getMargin */ +val String.margin: Int + get() = StringUtils.getMargin(this)