Documentation
¶
Overview ¶
Package box renders styled boxes around text for terminal applications.
The core type is Box, constructed with NewBox and configured via a fluent API. Boxes support multiple built‑in styles, title positions, alignment, wrapping, and ANSI/truecolor output.
Basic example:
b := box.NewBox().
Style(box.Single).
Padding(2, 1).
TitlePosition(box.Top).
ContentAlign(box.Center).
Color(box.Cyan).
TitleColor(box.BrightYellow).
out, err := b.Render("Box CLI Maker", "Render highly customizable boxes\n in the terminal")
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
Construction ¶
It is recommended to create boxes using NewBox, which returns a Box with sensible defaults. The returned Box can then be configured using methods such as Style, Padding, ContentAlign, Color, TitleColor, and TitlePosition.
The zero value of Box is not intended to be used directly with Render. If you construct a Box manually (e.g. with &box.Box{} or new(box.Box)), you must fully initialize all required fields (style, padding, glyphs, colors, etc.) yourself before calling Render.
Styles ¶
Box styles are selected with Style and the BoxStyle constants:
box.Single box.Double box.Round box.Bold box.SingleDouble box.DoubleSingle box.Classic box.Hidden box.Block
You can further customize any style by overriding the corner and edge glyphs using TopRight, TopLeft, BottomRight, BottomLeft, Horizontal, and Vertical.
Titles and alignment ¶
Titles can be placed inside the box, on the top border, or on the bottom border using TitlePosition with the TitlePosition constants:
box.Inside box.Top box.Bottom
Content alignment is controlled with ContentAlign and the AlignType constants:
box.Left box.Center box.Right
Wrapping ¶
WrapContent enables or disables automatic wrapping of the content. By default, when wrapping is enabled, the box width is based on two‑thirds of the terminal width. WrapLimit can be used to set an explicit maximum width.
Colors ¶
TitleColor, ContentColor, and Color accept either one of the first 16 ANSI color name constants (e.g. box.Green, box.BrightRed) or a #RGB / #RRGGBB / rgb:RRRR/GGGG/BBBB / rgba:RRRR/GGGG/BBBB/AAAA value. Invalid colors cause Render to return an error.
Errors ¶
Render returns an error if the style or title position is invalid, the wrap limit or padding is negative, a multiline title is used with a non‑Inside title position, any configured colors are invalid, or the terminal width cannot be determined. MustRender is a convenience wrapper that panics on error.
Copying ¶
Copy returns a shallow copy of a Box so you can define a base style and derive variants without mutating the original:
base := box.NewBox().Style(box.Single).Padding(2, 1) info := base.Copy().Color(box.Green) warn := base.Copy().Color(box.Yellow)
Each Copy can then be customized and rendered independently.
Index ¶
- Constants
- type AlignType
- type Box
- func (b *Box) BottomLeft(glyph string) *Box
- func (b *Box) BottomRight(glyph string) *Box
- func (b *Box) Color(color string) *Box
- func (b *Box) ContentAlign(align AlignType) *Box
- func (b *Box) ContentColor(color string) *Box
- func (b *Box) Copy() *Box
- func (b *Box) HPadding(px int) *Box
- func (b *Box) Horizontal(glyph string) *Box
- func (b *Box) MustRender(title, content string) string
- func (b *Box) Padding(px, py int) *Box
- func (b *Box) Render(title, content string) (string, error)
- func (b *Box) Style(box BoxStyle) *Box
- func (b *Box) TitleColor(color string) *Box
- func (b *Box) TitlePosition(pos TitlePosition) *Box
- func (b *Box) TopLeft(glyph string) *Box
- func (b *Box) TopRight(glyph string) *Box
- func (b *Box) VPadding(py int) *Box
- func (b *Box) Vertical(glyph string) *Box
- func (b *Box) WrapContent(allow bool) *Box
- func (b *Box) WrapLimit(limit int) *Box
- type BoxStyle
- type TitlePosition
Constants ¶
const ( Black = "Black" Red = "Red" Green = "Green" Yellow = "Yellow" Blue = "Blue" Magenta = "Magenta" Cyan = "Cyan" White = "White" BrightBlack = "BrightBlack" HiBlack = "HiBlack" BrightRed = "BrightRed" HiRed = "HiRed" BrightGreen = "BrightGreen" HiGreen = "HiGreen" BrightYellow = "BrightYellow" HiYellow = "HiYellow" BrightBlue = "BrightBlue" HiBlue = "HiBlue" BrightMagenta = "BrightMagenta" HiMagenta = "HiMagenta" BrightCyan = "BrightCyan" HiCyan = "HiCyan" BrightWhite = "BrightWhite" HiWhite = "HiWhite" )
Standard and bright ANSI color name constants usable with Color, TitleColor, and ContentColor.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AlignType ¶
type AlignType string
AlignType represents the horizontal alignment of content inside the box.
type Box ¶
type Box struct {
// contains filtered or unexported fields
}
Box renders styled borders around text content.
func (*Box) BottomLeft ¶
BottomLeft sets the glyph used in the lower-left corner.
func (*Box) BottomRight ¶
BottomRight sets the glyph used in the lower-right corner.
func (*Box) Color ¶
Color sets the color used for the box border (chrome).
Accepts one of the first 16 ANSI color name constants (e.g. box.Green, box.BrightRed) or a #RGB / #RRGGBB / rgb:RRRR/GGGG/BBBB / rgba:RRRR/GGGG/BBBB/AAAA value.
Invalid colors cause Render to return an error.
func (*Box) ContentAlign ¶
ContentAlign sets the horizontal alignment of content inside the box.
Supported values are box.Left, box.Center, and box.Right.
func (*Box) ContentColor ¶
ContentColor sets the color used for the content text.
Accepts one of the first 16 ANSI color name constants (e.g. box.Green, box.BrightRed) or a #RGB / #RRGGBB / rgb:RRRR/GGGG/BBBB / rgba:RRRR/GGGG/BBBB/AAAA value.
Invalid colors cause Render to return an error.
func (*Box) Copy ¶
Copy returns a shallow copy of the Box so further mutations do not affect the original.
Useful for creating base styles and deriving multiple boxes from them.
func (*Box) Horizontal ¶
Horizontal sets the glyph used for the horizontal edges.
func (*Box) MustRender ¶
MustRender is like Render but panics if an error occurs.
Use MustRender in examples or CLIs where failures should abort execution instead of being handled explicitly.
func (*Box) Render ¶
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.
func (*Box) Style ¶
Style selects one of the built-in BoxStyle presets.
Common styles include box.Single, box.Double, box.Round, box.Bold, box.SingleDouble, box.DoubleSingle, box.Classic, box.Hidden, and box.Block.
To make custom styles, call TopRight, TopLeft, BottomRight, BottomLeft, Horizontal, and Vertical after Style to override individual glyphs.
Example:
b := box.NewBox()
b.TopRight("+").TopLeft("+").BottomRight("+").BottomLeft("_").Horizontal("-").Vertical("|")
func (*Box) TitleColor ¶
TitleColor sets the color used for the title text.
Accepts one of the first 16 ANSI color name constants (e.g. box.Green, box.BrightRed) or a #RGB / #RRGGBB / rgb:RRRR/GGGG/BBBB / rgba:RRRR/GGGG/BBBB/AAAA value.
Invalid colors cause Render to return an error.
func (*Box) TitlePosition ¶
func (b *Box) TitlePosition(pos TitlePosition) *Box
TitlePosition sets where the title is rendered relative to the box.
Valid positions are box.Inside, box.Top, and box.Bottom.
func (*Box) WrapContent ¶
WrapContent enables or disables automatic wrapping of content.
When enabled, content is wrapped to fit roughly two-thirds of the terminal width by default. For custom limits or non-TTY outputs, use WrapLimit instead.
type BoxStyle ¶
type BoxStyle string
BoxStyle defines a built‑in border style for a Box.
const ( // Single is a box style with single-line borders. Single BoxStyle = "Single" // Double is a box style with double-line borders. Double BoxStyle = "Double" // Round is a box style with rounded corners. Round BoxStyle = "Round" // Bold is a box style with heavy lines. Bold BoxStyle = "Bold" // SingleDouble is a box style with single horizontal and double vertical lines. SingleDouble BoxStyle = "SingleDouble" // DoubleSingle is a box style with double horizontal and single vertical lines. DoubleSingle BoxStyle = "DoubleSingle" // Classic is a box style using plus and minus characters. Classic BoxStyle = "Classic" // Hidden is a box style with invisible borders. Hidden BoxStyle = "Hidden" // Block is a box style with solid block characters. Block BoxStyle = "Block" )
type TitlePosition ¶
type TitlePosition string
TitlePosition represents the position of the title relative to the box.
const ( // Inside places the title as the first lines inside the box. Inside TitlePosition = "Inside" // Top places the title on the top border of the box. Top TitlePosition = "Top" // Bottom places the title on the bottom border of the box. Bottom TitlePosition = "Bottom" )
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
ansi_art
command
|
|
|
ansi_styles_and_links
command
|
|
|
box_styles
command
|
|
|
colors_and_unicode
command
|
|
|
content_align
command
|
|
|
content_wrap
command
|
|
|
custom_box
command
|
|
|
ksctl
command
|
|
|
lolcat
command
|
|
|
readme
command
|
|
|
shared_styles
command
|
|
|
simple_box
command
|
|
|
title_positions
command
|