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\nin 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
Title alignment is controlled with TitleAlign and content alignment with ContentAlign; both use the AlignType constants:
box.Left box.Center box.Right
An Inside title defaults to Center; a Top/Bottom title defaults to Left on the border. Content defaults to Left.
Padding ¶
Padding adds space inside the box borders between the content and the edges. Horizontal padding adds spaces on the left and right of each line. Vertical padding adds blank lines above and below the content. Use Padding to set both at once, or HPadding / VPadding to set each independently.
The argument order is horizontal first: Padding(2, 1) means 2 columns of side padding and 1 blank row above and below — the reverse of CSS's "padding: vertical horizontal" shorthand. Padding defaults to 0; negative padding causes Render to return an error.
Margin ¶
Margin adds space outside the box borders. Horizontal margin prepends spaces to every rendered line. Vertical margin adds blank lines above and below the box. Use Margin to set both at once, or HMargin / VMargin to set each independently. The argument order matches Padding: horizontal first. Margin defaults to 0; negative margin causes Render to return an error.
Wrapping ¶
WrapContent enables or disables automatic wrapping of the content. By default, when wrapping is enabled, the wrap limit is two‑thirds of the available terminal width. If a horizontal margin is set, it is subtracted from the terminal width first so the rendered box stays within the terminal. WrapLimit can be used to set an explicit maximum width. Tabs are expanded at 8-column stops before wrapping, so the limit is honored for tab-heavy content, and Windows \r\n line endings are normalized to \n.
Styled content ¶
Content and titles may already contain ANSI styling — colors, bold, underline, and OSC 8 hyperlinks — and can be passed in as-is: user styling never bleeds into the borders or padding, styling survives wrapping, and this composes with Color and ContentColor.
Every rendered row is made self-contained: styles or hyperlinks that would span a line break (from wrapping or the content's own newlines) are closed at the end of each row and re-opened on the next.
Colors ¶
TitleColor, ContentColor, and Color accept either one of the 16 ANSI color name constants — the 8 base names (e.g. box.Green), each also with a Bright prefix (box.BrightRed) or its Hi alias (box.HiRed) — or a #RGB / #RRGGBB / rgb:RRRR/GGGG/BBBB / rgba:RRRR/GGGG/BBBB/AAAA value. Invalid colors cause Render to return an error.
Colors automatically adapt to what the terminal supports (TrueColor, 256-color, or 16-color). When the output cannot show color at all — NO_COLOR set, TERM=dumb, or output piped to a file — colors are dropped entirely, so captured output stays free of escape sequences.
Errors ¶
Render returns an error if the style or title position is invalid, the wrap limit is not positive, padding or margin 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) HMargin(mx int) *Box
- func (b *Box) HPadding(px int) *Box
- func (b *Box) Horizontal(glyph string) *Box
- func (b *Box) Margin(mx, my int) *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) TitleAlign(align AlignType) *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) VMargin(my int) *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.
Colors are automatically converted to the terminal's detected color profile, and suppressed entirely when the output does not support color (NO_COLOR set, TERM=dumb, or a non-TTY stdout).
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.
Colors are automatically converted to the terminal's detected color profile, and suppressed entirely when the output does not support color (NO_COLOR set, TERM=dumb, or a non-TTY stdout).
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) Margin ¶ added in v3.1.0
Margin sets horizontal (mx) and vertical (my) outer margin around the box.
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 wrap limit set by WrapLimit is not positive,
- 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) TitleAlign ¶ added in v3.1.0
TitleAlign sets the horizontal alignment of the title.
When TitlePosition is Inside, it aligns the title lines inside the box. When TitlePosition is Top or Bottom, it aligns the title on the border.
Supported values are box.Left, box.Center, and box.Right.
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.
Colors are automatically converted to the terminal's detected color profile, and suppressed entirely when the output does not support color (NO_COLOR set, TERM=dumb, or a non-TTY stdout).
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) VMargin ¶ added in v3.1.0
VMargin sets vertical outer margin (blank lines above and below).
func (*Box) WrapContent ¶
WrapContent enables or disables automatic wrapping of content.
When enabled, content is wrapped to fit roughly two-thirds of the available terminal width by default. If a horizontal margin is set, it is subtracted from the terminal width before computing the wrap limit, so the rendered box stays within the terminal. 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 (space) edges and visible "+" // corner markers indicating the box's extent. // // For a fully invisible box, override the corners with spaces: // // box.NewBox().Style(box.Hidden).TopLeft(" ").TopRight(" ").BottomLeft(" ").BottomRight(" ") 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
|
|
|
spacing
command
|
|
|
titles
command
|
|
|
scripts
|
|
|
screenshots/showcase
command
Command showcase renders one README showcase box to stdout, named by argument (a style, title position/alignment, or content alignment).
|
Command showcase renders one README showcase box to stdout, named by argument (a style, title position/alignment, or content alignment). |









