MCPcopy Index your code
hub / github.com/pterm/pterm / renderColorful

Method renderColorful

logger.go:303–377  ·  view source on GitHub ↗
(level LogLevel, msg string, args []LoggerArgument)

Source from the content-addressed store, hash-verified

301}
302
303func (l Logger) renderColorful(level LogLevel, msg string, args []LoggerArgument) (result string) {
304 if l.ShowTime {
305 result += Gray(time.Now().Format(l.TimeFormat)) + " "
306 }
307
308 if GetTerminalWidth() > 0 && GetTerminalWidth() < l.MaxWidth {
309 l.MaxWidth = GetTerminalWidth()
310 }
311
312 var argumentsInNewLine bool
313
314 result += level.Style().Sprintf("%-5s", level.String()) + " "
315
316 // if msg is too long, wrap it to multiple lines with the same length
317 remainingWidth := l.MaxWidth - internal.GetStringMaxWidth(result)
318 if internal.GetStringMaxWidth(msg) > remainingWidth {
319 argumentsInNewLine = true
320 msg = DefaultParagraph.WithMaxWidth(remainingWidth).Sprint(msg)
321 padding := len(time.Now().Format(l.TimeFormat) + " ")
322 msg = strings.ReplaceAll(msg, "\n", "\n"+strings.Repeat(" ", padding)+" │ ")
323 }
324
325 result += msg
326
327 if l.ShowCaller {
328 path, line := l.getCallerInfo()
329 args = append(args, LoggerArgument{
330 Key: "caller",
331 Value: FgGray.Sprintf("%s:%d", path, line),
332 })
333 }
334
335 arguments := make([]string, len(args))
336
337 // add arguments
338 if len(args) > 0 {
339 for i, arg := range args {
340 if style, ok := l.KeyStyles[arg.Key]; ok {
341 arguments[i] = style.Sprintf("%s: ", arg.Key)
342 } else {
343 arguments[i] = level.Style().Sprintf("%s: ", arg.Key)
344 }
345
346 arguments[i] += Sprintf("%s", Sprint(arg.Value))
347 }
348 }
349
350 fullLine := result + " " + strings.Join(arguments, " ")
351
352 // if the full line is too long, wrap the arguments to multiple lines
353 if internal.GetStringMaxWidth(fullLine) > l.MaxWidth {
354 argumentsInNewLine = true
355 }
356
357 if !argumentsInNewLine {
358 result = fullLine
359 } else {
360 padding := 4

Callers 1

printMethod · 0.95

Calls 10

getCallerInfoMethod · 0.95
GetStringMaxWidthFunction · 0.92
GetTerminalWidthFunction · 0.85
SprintfFunction · 0.85
SprintFunction · 0.85
StyleMethod · 0.80
SprintfMethod · 0.65
SprintMethod · 0.65
StringMethod · 0.45
WithMaxWidthMethod · 0.45

Tested by

no test coverage detected