Skip to content

Commit

Permalink
Add word wrapping method with default max length
Browse files Browse the repository at this point in the history
Introduced a new `wordWrap` method that defaults to a max length of 100 characters. Also updated parameter naming for clarity in the word wrapping logic.
  • Loading branch information
IllianiCBT committed Aug 5, 2024
1 parent cce83da commit 0be9ce5
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 0be9ce5

Please sign in to comment.