Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.
(a ...any)
| 37 | // Sprint formats using the default formats for its operands and returns the resulting string. |
| 38 | // Spaces are added between operands when neither is a string. |
| 39 | func (p CenterPrinter) Sprint(a ...any) string { |
| 40 | if RawOutput { |
| 41 | return Sprint(a...) |
| 42 | } |
| 43 | |
| 44 | lines := strings.Split(Sprint(a...), "\n") |
| 45 | |
| 46 | var ret strings.Builder |
| 47 | |
| 48 | if p.CenterEachLineSeparately { |
| 49 | for _, line := range lines { |
| 50 | margin := (GetTerminalWidth() - runewidth.StringWidth(RemoveColorFromString(line))) / 2 |
| 51 | if margin >= 1 { |
| 52 | ret.WriteString(strings.Repeat(" ", margin)) |
| 53 | } |
| 54 | ret.WriteString(line) |
| 55 | ret.WriteByte('\n') |
| 56 | } |
| 57 | return ret.String() |
| 58 | } |
| 59 | |
| 60 | var maxLineWidth int |
| 61 | |
| 62 | for _, line := range lines { |
| 63 | lineLength := runewidth.StringWidth(RemoveColorFromString(line)) |
| 64 | if maxLineWidth < lineLength { |
| 65 | maxLineWidth = lineLength |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | indent := GetTerminalWidth() - maxLineWidth |
| 70 | |
| 71 | if indent/2 < 1 { |
| 72 | for _, line := range lines { |
| 73 | ret.WriteString(line) |
| 74 | ret.WriteByte('\n') |
| 75 | } |
| 76 | |
| 77 | return ret.String() |
| 78 | } |
| 79 | |
| 80 | for _, line := range lines { |
| 81 | ret.WriteString(strings.Repeat(" ", indent/2)) |
| 82 | ret.WriteString(line) |
| 83 | ret.WriteByte('\n') |
| 84 | } |
| 85 | |
| 86 | return ret.String() |
| 87 | } |
| 88 | |
| 89 | // Sprintln formats using the default formats for its operands and returns the resulting string. |
| 90 | // Spaces are always added between operands and a newline is appended. |
no test coverage detected