wrap terminal alert lines after a line break in the text at the full width
What changed, and why it matters
This commit fixes a text-wrapping bug in the terminal version of Sparrow Wallet. Previously, if an alert message contained explicit line breaks (newlines), the wrapping logic counted the entire chunk after a newline as part of the current line length, causing later lines to be wrapped too early. The fix resets the line-length counter after each explicit newline so subsequent text uses the full available width. There is no security relevance in the commit or supplied references.
No security action required; this is a UI formatting fix.
Security signals we found
No strong security signals were identified.
Evidence from the diff
TerminalInteractionServices.formatLines() now treats embedded ‘\n’ characters as hard line breaks. It computes word length only up to the last newline when deciding whether to wrap, appends the word, then resets lineLength to the length of the remainder after that newline. A unit test verifies that text after a double newline wraps at the full maxLength. The change is purely presentational.
Changed components
src/main/java/com/sparrowwallet/sparrow/terminal/TerminalInteractionServices.javaInspect captured patch +38 / −3
### src/main/java/com/sparrowwallet/sparrow/terminal/TerminalInteractionServices.java
@@ -55,7 +55,7 @@ private static boolean isCustomButton(ButtonType buttonType) {
&& !ButtonType.NO.equals(buttonType) && !ButtonType.CLOSE.equals(buttonType);
}
- private String formatLines(String input, int maxLength) {
+ static String formatLines(String input, int maxLength) {
StringBuilder builder = new StringBuilder();
BreakIterator boundary = BreakIterator.getLineInstance(Locale.ROOT);
boundary.setText(input);
@@ -65,12 +65,18 @@ private String formatLines(String input, int maxLength) {
while(end != BreakIterator.DONE) {
String word = input.substring(start,end);
- lineLength = lineLength + word.length();
+ //A line break in the text ends the line, so only what comes before it counts towards this line and only what follows towards the next
+ int lineBreak = word.lastIndexOf('\n');
+ int wordLength = lineBreak < 0 ? word.length() : lineBreak;
+ lineLength = lineLength + wordLength;
if (lineLength >= maxLength) {
builder.append("\n");
- lineLength = word.length();
+ lineLength = wordLength;
}
builder.append(word);
+ if(lineBreak >= 0) {
+ lineLength = word.length() - lineBreak - 1;
+ }
start = end;
end = boundary.next();
}
### src/test/java/com/sparrowwallet/sparrow/terminal/TerminalInteractionServicesTest.java
@@ -0,0 +1,29 @@
+package com.sparrowwallet.sparrow.terminal;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class TerminalInteractionServicesTest {
+ @Test
+ public void linesAfterALineBreakWrapAtTheFullWidth() {
+ String paragraph = "Consider using a node on this computer or your local network, connecting over a Tor onion address.";
+ String formatted = TerminalInteractionServices.formatLines("A first line of forty characters or so.\n\n" + paragraph, 50);
+
+ String[] lines = formatted.split("\n");
+ assertEquals("A first line of forty characters or so.", lines[0]);
+ assertEquals("", lines[1]);
+ assertEquals("Consider using a node on this computer or your ", lines[2]);
+ assertTrue(Arrays.stream(lines).allMatch(line -> line.length() < 50));
+ assertEquals(5, lines.length);
+ }
+
+ @Test
+ public void wrapsTextWithoutLineBreaksAtTheWidth() {
+ String formatted = TerminalInteractionServices.formatLines("one two three four five six seven eight nine ten eleven twelve", 20);
+
+ assertArrayEquals(new String[] {"one two three four ", "five six seven ", "eight nine ten ", "eleven twelve"}, formatted.split("\n"));
+ }
+}Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.