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)
| 155 | // Sprint formats using the default formats for its operands and returns the resulting string. |
| 156 | // Spaces are added between operands when neither is a string. |
| 157 | func (p *PrefixPrinter) Sprint(a ...any) string { |
| 158 | m := Sprint(a...) |
| 159 | if p.Debugger && !PrintDebugMessages { |
| 160 | return "" |
| 161 | } |
| 162 | |
| 163 | if RawOutput { |
| 164 | if p.Prefix.Text != "" { |
| 165 | return Sprintf("%s: %s", strings.TrimSpace(p.Prefix.Text), Sprint(a...)) |
| 166 | } else { |
| 167 | return Sprint(a...) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if p.Prefix.Style == nil { |
| 172 | p.Prefix.Style = NewStyle() |
| 173 | } |
| 174 | if p.Scope.Style == nil { |
| 175 | p.Scope.Style = NewStyle() |
| 176 | } |
| 177 | if p.MessageStyle == nil { |
| 178 | p.MessageStyle = NewStyle() |
| 179 | } |
| 180 | |
| 181 | var ret strings.Builder |
| 182 | var newLine bool |
| 183 | |
| 184 | if strings.HasSuffix(m, "\n") { |
| 185 | m = strings.TrimRight(m, "\n") |
| 186 | newLine = true |
| 187 | } |
| 188 | |
| 189 | messageLines := strings.Split(m, "\n") |
| 190 | for i, m := range messageLines { |
| 191 | if i == 0 { |
| 192 | ret.WriteString(p.GetFormattedPrefix()) |
| 193 | ret.WriteByte(' ') |
| 194 | if p.Scope.Text != "" { |
| 195 | ret.WriteString(NewStyle(*p.Scope.Style...).Sprint(" (" + p.Scope.Text + ") ")) |
| 196 | } |
| 197 | ret.WriteString(p.MessageStyle.Sprint(m)) |
| 198 | } else { |
| 199 | ret.WriteByte('\n') |
| 200 | ret.WriteString(p.Prefix.Style.Sprint(strings.Repeat(" ", len([]rune(p.Prefix.Text))+2))) |
| 201 | ret.WriteByte(' ') |
| 202 | ret.WriteString(p.MessageStyle.Sprint(m)) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if p.ShowLineNumber { |
| 207 | _, fileName, line, _ := runtime.Caller(3 + p.LineNumberOffset) |
| 208 | ret.WriteString(FgGray.Sprint("\n└ " + fmt.Sprintf("(%s:%d)\n", fileName, line))) |
| 209 | newLine = false |
| 210 | } |
| 211 | |
| 212 | if newLine { |
| 213 | ret.WriteByte('\n') |
| 214 | } |