MCPcopy
hub / github.com/pterm/pterm / Sprint

Method Sprint

center_printer.go:39–87  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
39func (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.

Callers 3

SprintlnMethod · 0.95
SprintfMethod · 0.95
PrintMethod · 0.95

Calls 4

SprintFunction · 0.85
GetTerminalWidthFunction · 0.85
RemoveColorFromStringFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected