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

Method Sprint

header_printer.go:66–123  ·  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

64// Sprint formats using the default formats for its operands and returns the resulting string.
65// Spaces are added between operands when neither is a string.
66func (p HeaderPrinter) Sprint(a ...any) string {
67 if RawOutput {
68 return Sprint(a...)
69 }
70
71 if p.TextStyle == nil {
72 p.TextStyle = NewStyle()
73 }
74 if p.BackgroundStyle == nil {
75 p.BackgroundStyle = NewStyle()
76 }
77
78 text := Sprint(a...)
79
80 var blankLine string
81
82 longestLine := internal.ReturnLongestLine(text, "\n")
83 longestLineLen := runewidth.StringWidth(RemoveColorFromString(longestLine)) + p.Margin*2
84
85 if p.FullWidth {
86 text = splitText(text, GetTerminalWidth()-p.Margin*2)
87 blankLine = strings.Repeat(" ", GetTerminalWidth())
88 } else {
89 if longestLineLen > GetTerminalWidth() {
90 text = splitText(text, GetTerminalWidth()-p.Margin*2)
91 blankLine = strings.Repeat(" ", GetTerminalWidth())
92 } else {
93 text = splitText(text, longestLineLen-p.Margin*2)
94 blankLine = strings.Repeat(" ", longestLineLen)
95 }
96 }
97
98 var marginString string
99 var ret strings.Builder
100
101 if p.FullWidth {
102 longestLineLen = runewidth.StringWidth(RemoveColorFromString(internal.ReturnLongestLine(text, "\n")))
103 marginString = strings.Repeat(" ", (GetTerminalWidth()-longestLineLen)/2)
104 } else {
105 marginString = strings.Repeat(" ", p.Margin)
106 }
107
108 ret.WriteString(p.BackgroundStyle.Sprint(blankLine))
109 ret.WriteByte('\n')
110 for _, line := range strings.Split(text, "\n") {
111 line = strings.ReplaceAll(line, "\n", "")
112 line = marginString + line + marginString
113 if runewidth.StringWidth(line) < runewidth.StringWidth(blankLine) {
114 line += strings.Repeat(" ", runewidth.StringWidth(blankLine)-runewidth.StringWidth(line))
115 }
116 ret.WriteString(p.BackgroundStyle.Sprint(p.TextStyle.Sprint(line)))
117 ret.WriteByte('\n')
118 }
119 ret.WriteString(p.BackgroundStyle.Sprint(blankLine))
120 ret.WriteByte('\n')
121
122 return ret.String()
123}

Callers 4

SprintlnMethod · 0.95
SprintfMethod · 0.95
PrintMethod · 0.95

Calls 8

ReturnLongestLineFunction · 0.92
SprintFunction · 0.85
NewStyleFunction · 0.85
RemoveColorFromStringFunction · 0.85
splitTextFunction · 0.85
GetTerminalWidthFunction · 0.85
SprintMethod · 0.65
StringMethod · 0.45

Tested by 1