segment

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package segment provides the atomic rendering unit for terminal output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Divide

func Divide(segments []Segment, cuts []int) [][]Segment

Divide divides segments at the given cell positions. Returns len(cuts)+1 slices of segments.

func LastStyle added in v1.1.2

func LastStyle(segments []Segment) *style.Style

LastStyle returns the Style of the last non-control segment, or nil.

func SplitLines

func SplitLines(segments []Segment) [][]Segment

SplitLines splits segments by newlines, returning lines of segments.

func TotalCellLength

func TotalCellLength(segments []Segment) int

TotalCellLength returns the total cell length of a slice of segments.

Types

type Control

type Control struct {
	Codes []ControlCode
}

Control is a renderable that produces terminal control sequences.

func BeginSyncUpdate added in v1.0.8

func BeginSyncUpdate() Control

BeginSyncUpdate creates a control that opens a synchronized output frame (DEC private mode 2026). Terminals that support it buffer everything until EndSyncUpdate and paint it atomically, eliminating flicker. Unsupported terminals ignore the unknown mode.

func CarriageReturn

func CarriageReturn() Control

CarriageReturn creates a control for carriage return.

func CursorBackward

func CursorBackward(n int) Control

CursorBackward creates a control to move the cursor backward n columns.

func CursorDown

func CursorDown(n int) Control

CursorDown creates a control to move the cursor down n lines.

func CursorForward

func CursorForward(n int) Control

CursorForward creates a control to move the cursor forward n columns.

func CursorMoveTo

func CursorMoveTo(x, y int) Control

CursorMoveTo creates a control to move the cursor to (x, y).

func CursorUp

func CursorUp(n int) Control

CursorUp creates a control to move the cursor up n lines.

func EndSyncUpdate added in v1.0.8

func EndSyncUpdate() Control

EndSyncUpdate creates a control that closes a synchronized output frame.

func EraseInDisplay

func EraseInDisplay(mode int) Control

EraseInDisplay creates a control to erase in the display. mode: 0 = cursor to end, 1 = start to cursor, 2 = entire display, 3 = entire display + scrollback

func EraseInLine

func EraseInLine(mode int) Control

EraseInLine creates a control to erase in the current line. mode: 0 = cursor to end, 1 = start to cursor, 2 = entire line

func HideCursor

func HideCursor() Control

HideCursor creates a control to hide the cursor.

func NewControl

func NewControl(codes ...ControlCode) Control

NewControl creates a new Control with the given codes.

func SetTitle

func SetTitle(title string) Control

SetTitle creates a control to set the terminal title.

func ShowCursor

func ShowCursor() Control

ShowCursor creates a control to show the cursor.

func (Control) Render

func (c Control) Render() string

Render produces the ANSI escape sequence for the control codes.

type ControlCode

type ControlCode struct {
	Type   ControlType
	Params []int
	Text   string // For SetTitle/SetWindowTitle
}

ControlCode represents a control operation with optional parameters.

func (ControlCode) Render

func (cc ControlCode) Render() string

Render produces the ANSI escape sequence for this control code.

type ControlType

type ControlType int

ControlType represents a terminal control operation.

const (
	ControlCarriageReturn ControlType = iota
	ControlShowCursor
	ControlHideCursor
	ControlCursorUp
	ControlCursorDown
	ControlCursorForward
	ControlCursorBackward
	ControlCursorMoveTo
	ControlEraseInLine
	ControlEraseInDisplay
	ControlSetTitle
	ControlBeginSyncUpdate
	ControlEndSyncUpdate
)

type Segment

type Segment struct {
	Text    string
	Style   *style.Style
	Control []ControlCode // Non-nil means this is a control segment
}

Segment is the atomic unit of terminal rendering. A segment is either styled text or a control sequence.

func AdjustLineLength

func AdjustLineLength(line []Segment, width int, pad bool) []Segment

AdjustLineLength adjusts a line of segments to the given cell width. If pad is true, shorter lines are padded with spaces. Longer lines are truncated.

func ApplyStyle

func ApplyStyle(segments []Segment, s style.Style) []Segment

ApplyStyle applies a style to all segments.

func NewControlSegment

func NewControlSegment(codes ...ControlCode) Segment

NewControl creates a control segment.

func NewText

func NewText(text string, s *style.Style) Segment

NewText creates a text segment with optional style.

func Simplify

func Simplify(segments []Segment) []Segment

Simplify merges adjacent segments with the same style.

func (Segment) CellLength

func (s Segment) CellLength() int

CellLength returns the number of terminal cells this segment occupies. Control segments have zero cell length.

func (Segment) IsControl

func (s Segment) IsControl() bool

IsControl returns true if this is a control segment.

func (Segment) Render

func (s Segment) Render(colorSystem style.ColorSystem) string

Render produces the ANSI-encoded string for this segment.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL