Skip to content

Fix double newline in Println with trailing newlines#279

Open
veeceey wants to merge 1 commit intofatih:mainfrom
veeceey:fix/issue-241-double-newline
Open

Fix double newline in Println with trailing newlines#279
veeceey wants to merge 1 commit intofatih:mainfrom
veeceey:fix/issue-241-double-newline

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 13, 2026

Fixes #241

Problem

When using Println() or SprintlnFunc() with strings that already contain trailing newlines, the output would have double newlines:

c := color.New(color.FgRed)
c.Println("Hello\n")  // Outputs: "Hello\n\n" (double newline)

This happened because:

  1. The input string has \n
  2. fmt.Sprintln() adds another \n
  3. TrimSuffix only removes the one added by Sprintln, leaving the original
  4. Println adds yet another \n via fmt.Fprintln

Solution

Changed sprintln() helper to use TrimRight instead of TrimSuffix. This removes ALL trailing newlines from the formatted string, ensuring Println functions always add exactly one newline regardless of input.

Changes

  • Modified sprintln() to use strings.TrimRight() to strip all trailing newlines
  • Updated TestIssue218 to reflect the new normalized behavior
  • Added TestTrailingNewline with comprehensive test cases

Testing

All existing tests pass. Manual verification:

c := color.New(color.FgRed)

// Before: double newline
// After: single newline
c.Println("Hello\n")

// SprintlnFunc also fixed
fn := c.SprintlnFunc()
result := fn("World\n")
// Before: "World\n\n"
// After: "World\n"

The behavior now matches user expectations - Println always produces exactly one trailing newline, making it more predictable when working with dynamic strings.

When using Println or SprintlnFunc with strings that already contain
trailing newlines, the output would have double newlines - one from
the input and one added by Println.

This changes sprintln() to use TrimRight instead of TrimSuffix, which
removes all trailing newlines from the formatted string before the
Println functions add their own newline. This ensures consistent
single-newline output regardless of whether the input has trailing
newlines.

Updated TestIssue218 to reflect the new normalized behavior.
Added TestTrailingNewline to verify the fix.

Fixes fatih#241
@veeceey
Copy link
Author

veeceey commented Feb 13, 2026

Manual Test Results

Ran comprehensive tests to verify the fix works correctly:

=== Test 1: Println with string ending in \n ===
Hello
--- End of Test 1 ---

=== Test 2: Println with normal string ===
Hello
--- End of Test 2 ---

=== Test 3: SprintlnFunc with string ending in \n ===
Result: "World\n"
World
--- End of Test 3 ---

=== Test 4: SprintFunc with string ending in \n ===
Result: "Test\n"
Test
--- End of Test 4 ---

Test 1 and 3 show the fix working - no more double newlines. The output is now consistent regardless of whether the input has trailing newlines.

All package tests pass:

$ go test ./...
ok  	github.com/fatih/color	0.236s

@veeceey
Copy link
Author

veeceey commented Mar 10, 2026

gentle ping — still hoping to get this one in. let me know if you'd like any changes

@veeceey
Copy link
Author

veeceey commented Mar 12, 2026

any feedback on this? let me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Print string ended by symbol new line '\n'

1 participant