MCPcopy Index your code
hub / github.com/box-cli-maker/box-cli-maker / Render

Method Render

box.go:248–413  ·  view source on GitHub ↗

Render generates the box with the given title and content. It returns an error if: - the BoxStyle is invalid, - the TitlePosition is invalid, - the wrapping limit is negative, - padding is negative, - a multiline title is used with a non-Inside TitlePosition, or - any configured colors are invalid.

(title, content string)

Source from the content-addressed store, hash-verified

246// - a multiline title is used with a non-Inside TitlePosition, or
247// - any configured colors are invalid.
248func (b *Box) Render(title, content string) (string, error) {
249 if b.styleSet {
250 if _, ok := boxes[b.config.style]; !ok {
251 return "", fmt.Errorf("invalid Box style %s", b.config.style)
252 }
253 }
254
255 var content_ []string
256
257 // Allow wrapping according to the user
258 if b.allowWrapping {
259 if b.wrappingLimit < 0 {
260 return "", fmt.Errorf("wrapping limit cannot be negative")
261 }
262 // If limit not provided then use 2*TermWidth/3 as limit else
263 // use the one provided
264 if b.wrappingLimit != 0 {
265 content = ansi.Wrap(content, b.wrappingLimit, "")
266 } else {
267 if !isTTY(os.Stdout.Fd()) {
268 return "", fmt.Errorf("cannot determine terminal width; use WrapLimit to set an explicit wrap limit when wrapping on non-TTY outputs")
269 }
270 width, _, err := term.GetSize(os.Stdout.Fd())
271 if err != nil {
272 return "", fmt.Errorf("cannot determine terminal width: %v", err)
273 }
274 // Use 2/3 of terminal width as default wrapping limit
275 wrapWidth := max(2*width/defaultWrapDivisor, minWrapWidth)
276 content = ansi.Wrap(content, wrapWidth, "")
277 }
278 }
279
280 title, err := applyColor(title, b.titleColor)
281 if err != nil {
282 return "", err
283 }
284 content, err = applyColor(content, b.contentColor)
285 if err != nil {
286 return "", err
287 }
288
289 if b.titlePos == "" {
290 b.titlePos = Inside
291 }
292
293 if title != "" {
294 if b.titlePos != Inside && strings.Contains(title, "\n") {
295 return "", fmt.Errorf("multiline titles are only supported Inside title position only")
296 }
297 if b.titlePos == Inside {
298 content_ = append(content_, strings.Split(title, "\n")...)
299 content_ = append(content_, []string{""}...) // for empty line between title and content
300 }
301 }
302 content_ = append(content_, strings.Split(content, "\n")...)
303
304 titleLen := 0
305 if title != "" {

Calls 8

applyColorBarMethod · 0.95
addVertPaddingMethod · 0.95
formatLineMethod · 0.95
applyColorFunction · 0.85
longestLineFunction · 0.85
charWidthFunction · 0.85
buildPlainBarFunction · 0.85
buildTitledBarFunction · 0.85