Documentation
¶
Overview ¶
Package tabwrap provides tab-aware, grapheme-cluster-aware display width operations for terminal/fixed-width output.
It wraps clipperhouse/displaywidth to add tab-stop handling, line wrapping, truncation, and padding — the common building blocks for CLI table renderers and TUI applications.
Key differences from mattn/go-runewidth:
- Grapheme-cluster-aware (emoji, combining characters) via displaywidth.
- Built-in tab-stop expansion in every operation.
Key additions over clipperhouse/displaywidth:
- Tab-aware StringWidth, ExpandTab, Wrap, Truncate, FillLeft, FillRight.
Index ¶
- func FillLeft(s string, width int) string
- func FillRight(s string, width int) string
- func StringWidth(s string) int
- func Truncate(s string, maxWidth int, tail string) string
- type Condition
- func (c *Condition) ExpandTab(s string) string
- func (c *Condition) FillLeft(s string, width int) string
- func (c *Condition) FillRight(s string, width int) string
- func (c *Condition) StringWidth(s string) int
- func (c *Condition) Truncate(s string, maxWidth int, tail string) string
- func (c *Condition) Wrap(s string, width int) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StringWidth ¶
StringWidth returns the display width of s using default settings.
Types ¶
type Condition ¶
type Condition struct {
// TabWidth is the number of columns per tab stop. Zero or negative defaults to 4.
TabWidth int
// EastAsianWidth treats ambiguous East Asian characters as width 2 when true.
EastAsianWidth bool
// ControlSequences treats 7-bit ANSI escape sequences (CSI, OSC, etc.)
// as zero-width when true. This allows correct width measurement of
// strings containing terminal color codes and other SGR sequences.
ControlSequences bool
}
Condition configures display width behaviour.
func NewCondition ¶
func NewCondition() *Condition
NewCondition returns a Condition with default settings (TabWidth = 4).
func (*Condition) ExpandTab ¶
ExpandTab replaces every tab with spaces according to tab stops. Columns reset at each newline.
func (*Condition) FillLeft ¶
FillLeft pads s on the left with spaces to reach width display columns. If s is already at least width columns wide it is returned unchanged.
func (*Condition) FillRight ¶
FillRight pads s on the right with spaces to reach width display columns. If s is already at least width columns wide it is returned unchanged.
func (*Condition) StringWidth ¶
StringWidth returns the display width of s, handling tabs and newlines. For multi-line strings it returns the width of the widest line.
func (*Condition) Truncate ¶
Truncate truncates s to fit within maxWidth display columns, appending tail if truncation occurs. Tabs are expanded before measuring.
func (*Condition) Wrap ¶
Wrap wraps s to fit within width display columns.
Tabs are indivisible tokens: if a tab does not fit on the current line the entire tab moves to the next line. Tabs in the output are expanded to spaces so the result is render-ready.
Existing newlines are preserved. When width <= 0 the string is returned with tabs expanded but no wrapping applied.
When ControlSequences is true, SGR (Select Graphic Rendition) state is carried across line breaks: a reset is emitted before each newline and the active SGR sequences are replayed after it. This ensures each output line is independently styled.