Untitled
List<String> result = new ArrayList<>(); // Create the top border of the frame String topBottomBorder = "+" + "*".repeat(width - 2) + "+"; result.add(topBottomBorder); for (String[] message : messages) { String user = message[0]; String text = message[1]; List<String> lines = new ArrayList<>(); // Split the text into words String[] words = text.split(" "); StringBuilder currentLine = new StringBuilder(); for (String word : words) { if (currentLine.length() + word.length() + (currentLine.length() > 0 ? 1 : 0) <= userWidth) { if (currentLine.length() > 0) { currentLine.append(" ").append(word); } else { currentLine.append(word); } } else { lines.add(currentLine.toString()); currentLine = new StringBuilder(word); } } // Add the last line if (currentLine.length() > 0) { lines.add(currentLine.toString()); } for (String line : lines) { String formattedLine; if (user.equals("1")) { // Left-aligned formattedLine = "|" + String.format("%-" + userWidth + "s", line) + " ".repeat(width - userWidth - 2) + "|"; } else { // Right-aligned formattedLine = "|" + " ".repeat(width - userWidth - 1) + String.format("%-" + userWidth + "s", line) + "|"; } result.add(formattedLine); } } // Add the bottom border of the frame result.add(topBottomBorder); return result;
Leave a Comment