ITADN

Fix double newline in Println with trailing newlines

#279Pull Requestveeceey 创建于 2026-02-13
V
veeceeycommented
Fixes #241 ## Problem When using `Println()` or `SprintlnFunc()` with strings that already contain trailing newlines, the output would have double newlines: ```go 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: ```go 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.
合并状态:未合并 5 条评论