Skip to content

Commit

Permalink
Merge pull request #5862 from IllianiCBT/wordWrap_utilityMethod
Browse files Browse the repository at this point in the history
Overloaded wordWrap Method with Default Max Length
  • Loading branch information
IllianiCBT authored Aug 7, 2024
2 parents 68c2324 + afdf8aa commit ab85ae0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions megamek/src/megamek/client/ui/WrapLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,24 @@ private void addRow(Dimension dim, int rowWidth, int rowHeight) {
dim.height += rowHeight;
}

/**
* Inserts line breaks into a given input string to ensure that no line exceeds a maximum length of 100.
*
* @param input The input string to be wrapped.
* @return The string with line breaks inserted.
*/
public static String wordWrap(String input) {
return wordWrap(input, 100);
}

/**
* Inserts line breaks into a given input string to ensure that no line exceeds a maximum length.
*
* @param input The input string to be wrapped.
* @param maxLineLength The maximum length of each line.
* @param maximumCharacters The maximum number of characters (including whitespaces) on each line.
* @return The string with line breaks inserted.
*/
public static String wordWrap(String input, int maxLineLength) {
public static String wordWrap(String input, int maximumCharacters) {
StringTokenizer token = new StringTokenizer(input, " ");
StringBuilder output = new StringBuilder(input.length());

Expand All @@ -207,7 +217,7 @@ public static String wordWrap(String input, int maxLineLength) {
while (token.hasMoreTokens()) {
String word = token.nextToken();

if (lineLen + word.length() > maxLineLength) {
if (lineLen + word.length() > maximumCharacters) {
output.append('\n');
lineLen = 0;
}
Expand Down

0 comments on commit ab85ae0

Please sign in to comment.