Documentation
¶
Index ¶
- Constants
- Variables
- func AcquireBuilder() *strings.Builder
- func ApplyBackgroundToLine(line string, width int, bgFn func(string) string) string
- func BuildWidthExceedErrorMsg(lineIndex int, lineWidth int, termWidth int, crashLogPath string) string
- func ExtractAnsiCode(s string, pos int) (code string, length int, ok bool)
- func ExtractSegments(line string, beforeEnd int, afterStart int, afterLen int, strictAfter bool) (string, int, string, int)
- func GetCrashLogPath() string
- func GetCursorMarker() string
- func GetSegmentReset() string
- func GetSegmenter() any
- func GraphemeWidth(s string) int
- func IsASCII(s string) bool
- func IsPrintableASCII(s string) bool
- func IsPunctuationChar(char string) bool
- func IsWhitespaceChar(char string) bool
- func LogCrashInfo(width int, lineIndex int, line string, newLines []string)
- func ReleaseBuilder(b *strings.Builder)
- func SliceByColumn(line string, startCol int, length int, strict bool) string
- func StripAnsi(s string) string
- func TruncateToWidth(text string, maxWidth int, ellipsis string, pad bool) string
- func VisibleWidth(s string) int
- func WrapAnsiText(text string, width int) []string
- func WriteCrashLog(path string, data string)
- func WriteDebugLog(...)
- type AnsiCodeTracker
- type Component
- type Container
- func (c *Container) AddChild(component Component)
- func (c *Container) Clear()
- func (c *Container) GetChildren() []Component
- func (c *Container) HandleInput(data string)
- func (c *Container) InsertChildAt(index int, component Component)
- func (c *Container) Invalidate()
- func (c *Container) RemoveChild(component Component)
- func (c *Container) RemoveChildAt(index int)
- func (c *Container) Render(width int) []string
- func (c *Container) WantsKeyRelease() bool
- type Focusable
- type FullRenderer
- type SliceResult
- type TUI
- func (t *TUI) ForceRender()
- func (t *TUI) GetFullRedraws() int
- func (t *TUI) GetShowHardwareCursor() bool
- func (t *TUI) HandleInput(data string)
- func (t *TUI) QueryCellSize()
- func (t *TUI) SetClearOnShrink(enabled bool)
- func (t *TUI) SetFocus(component Component)
- func (t *TUI) SetLogDir(dir string)
- func (t *TUI) SetShowHardwareCursor(enabled bool)
- func (t *TUI) Start()
- func (t *TUI) Stop()
- func (t *TUI) TriggerRender()
- type Terminal
Constants ¶
const ( CursorMarker = "\x1b_pi:c\x07" SegmentReset = "\x1b[0m\x1b]8;;\x07" )
Variables ¶
var ( SyncOutputBegin = "\x1b[?2026h" SyncOutputEnd = "\x1b[?2026l" )
var SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07"
Functions ¶
func AcquireBuilder ¶ added in v0.10.0
AcquireBuilder returns a pooled strings.Builder. Call ReleaseBuilder when done.
func ApplyBackgroundToLine ¶
func BuildWidthExceedErrorMsg ¶
func BuildWidthExceedErrorMsg(lineIndex int, lineWidth int, termWidth int, crashLogPath string) string
BuildWidthExceedErrorMsg builds an error message for when a rendered line exceeds the terminal width.
func ExtractAnsiCode ¶
ExtractAnsiCode extracts an ANSI escape sequence starting at the given position. It supports three types of sequences:
- CSI (Control Sequence Introducer): ESC [ ... terminator (e.g., ESC[31m for red text)
- OSC (Operating System Command): ESC ] ... BEL or ESC \ (e.g., ESC]8;;url for hyperlinks)
- APC (Application Program Command): ESC _ ... BEL or ESC \ (e.g., ESC_pi:c for cursor marker)
Returns the complete escape sequence, its length, and whether extraction succeeded.
func ExtractSegments ¶
func GetCrashLogPath ¶
func GetCrashLogPath() string
GetCrashLogPath returns the path to the crash log file.
func GetCursorMarker ¶
func GetCursorMarker() string
func GetSegmentReset ¶
func GetSegmentReset() string
func GetSegmenter ¶
func GetSegmenter() any
func GraphemeWidth ¶
GraphemeWidth calculates the display width of a grapheme cluster It handles: - Zero-width characters (zero-width joiners, zero-width spaces) - Combining marks (counted with their base character) - Emoji (typically width 2) - East Asian characters (width 2 for fullwidth, 1 for halfwidth) - Regular ASCII (width 1)
func IsPrintableASCII ¶ added in v0.10.0
IsPrintableASCII reports whether s contains only printable ASCII (0x20-0x7e).
func IsPunctuationChar ¶
func IsWhitespaceChar ¶
func LogCrashInfo ¶
LogCrashInfo logs detailed crash information including terminal width, line index, and all rendered lines.
func ReleaseBuilder ¶ added in v0.10.0
ReleaseBuilder returns a builder from AcquireBuilder to the pool.
func StripAnsi ¶ added in v0.3.2
StripAnsi removes every escape sequence recognized by ExtractAnsiCode from s. Lone ESC bytes and incomplete or unsupported sequences are left unchanged.
func TruncateToWidth ¶
TruncateToWidth truncates text to fit within a maximum visible width, adding ellipsis if needed. Optionally pad with spaces to reach exactly maxWidth. Properly handles ANSI escape codes (they don't count toward width).
Parameters:
- text: Text to truncate (may contain ANSI codes)
- maxWidth: Maximum visible width
- ellipsis: Ellipsis string to append when truncating (default: "...")
- pad: If true, pad result with spaces to exactly maxWidth
Returns: Truncated text, optionally padded to exactly maxWidth
func VisibleWidth ¶
func WrapAnsiText ¶ added in v0.2.1
WrapAnsiText wraps text to the given width,
preserving ANSI escape codes (colors, bold, etc.).
func WriteCrashLog ¶
WriteCrashLog writes crash data to the specified path.
func WriteDebugLog ¶
func WriteDebugLog(firstChanged, viewportTop, finalCursorRow, hardwareCursorRow, renderEnd, cursorRow, cursorCol, height int, tCursorRow int, newLines, previousLines []string)
WriteDebugLog writes detailed debug information about the rendering process. With FASTTUI_DEBUG=1 the render loop dumps automatically; this helper is for explicit dumps.
Types ¶
type AnsiCodeTracker ¶
type AnsiCodeTracker struct {
// contains filtered or unexported fields
}
AnsiCodeTracker tracks the current state of ANSI escape codes in a text stream. It maintains the active text formatting attributes (bold, italic, colors, etc.) and can reconstruct the ANSI codes needed to continue formatting on a new line.
Example usage:
tracker := NewAnsiCodeTracker()
tracker.Process("\x1b[1;31m") // bold + red foreground
codes := tracker.GetActiveCodes() // returns "\x1b[1;31m"
func NewAnsiCodeTracker ¶
func NewAnsiCodeTracker() *AnsiCodeTracker
NewAnsiCodeTracker creates a new ANSI code tracker with all formatting disabled.
Example:
tracker := NewAnsiCodeTracker()
func (*AnsiCodeTracker) Clear ¶
func (t *AnsiCodeTracker) Clear()
Clear is an alias for Reset. It clears all active formatting attributes.
Example:
tracker.Clear() // same as tracker.Reset()
func (*AnsiCodeTracker) GetActiveCodes ¶
func (t *AnsiCodeTracker) GetActiveCodes() string
GetActiveCodes returns an ANSI escape sequence that represents all currently active formatting attributes. This is useful for continuing formatting on a new line. Returns an empty string if no formatting is active.
Example:
tracker.Process("\x1b[1;31m") // bold + red
codes := tracker.GetActiveCodes() // returns "\x1b[1;31m"
// Use case: wrapping text while preserving formatting
line1 := "\x1b[1;31mHello"
tracker.Process("\x1b[1;31m")
line2 := tracker.GetActiveCodes() + "World\x1b[0m"
func (*AnsiCodeTracker) GetLineEndReset ¶
func (t *AnsiCodeTracker) GetLineEndReset() string
GetLineEndReset returns an ANSI code to reset underline formatting at the end of a line. This is useful because underline formatting can extend beyond the text content. Returns "\x1b[24m" (turn off underline) if underline is active, empty string otherwise.
Example:
tracker.Process("\x1b[4m") // underline
reset := tracker.GetLineEndReset() // returns "\x1b[24m"
line := "text" + reset // prevents underline from extending
func (*AnsiCodeTracker) HasActiveCodes ¶
func (t *AnsiCodeTracker) HasActiveCodes() bool
HasActiveCodes returns true if any formatting attributes are currently active.
Example:
tracker := NewAnsiCodeTracker()
tracker.HasActiveCodes() // returns false
tracker.Process("\x1b[1m") // bold
tracker.HasActiveCodes() // returns true
func (*AnsiCodeTracker) Process ¶
func (t *AnsiCodeTracker) Process(ansiCode string)
Process parses an ANSI escape code and updates the tracker's internal state. It handles SGR (Select Graphic Rendition) codes for text formatting and colors.
Supported codes:
- 0: Reset all attributes
- 1: Bold, 2: Dim, 3: Italic, 4: Underline, 5: Blink
- 7: Inverse, 8: Hidden, 9: Strikethrough
- 21-29: Turn off corresponding attributes
- 30-37, 90-97: Foreground colors
- 40-47, 100-107: Background colors
- 38;5;n: 256-color foreground
- 48;5;n: 256-color background
- 38;2;r;g;b: RGB foreground
- 48;2;r;g;b: RGB background
Example:
tracker.Process("\x1b[1;31m") // bold + red
tracker.Process("\x1b[38;5;208m") // 256-color orange
tracker.Process("\x1b[0m") // reset all
func (*AnsiCodeTracker) Reset ¶
func (t *AnsiCodeTracker) Reset()
Reset clears all active formatting attributes, returning the tracker to its initial state.
Example:
tracker.Process("\x1b[1;31m") // bold + red
tracker.Reset() // clear all formatting
tracker.HasActiveCodes() // returns false
type Component ¶
type Component interface {
// Render returns terminal lines for the given width.
Render(width int) []string
// HandleInput handles raw input when focused.
HandleInput(data string)
// WantsKeyRelease: receive key-up events.
WantsKeyRelease() bool
// Invalidate clears cached render state.
Invalidate()
}
Component: render + keyboard input.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
func NewContainer ¶
func NewContainer() *Container
func (*Container) GetChildren ¶
func (*Container) HandleInput ¶
func (*Container) InsertChildAt ¶
func (*Container) Invalidate ¶
func (c *Container) Invalidate()
func (*Container) RemoveChild ¶
func (*Container) RemoveChildAt ¶
func (*Container) WantsKeyRelease ¶
type FullRenderer ¶ added in v0.2.0
type FullRenderer struct {
// contains filtered or unexported fields
}
func (FullRenderer) Render ¶ added in v0.2.0
func (f FullRenderer) Render(clear bool)
type SliceResult ¶
type SliceResult struct {
// contains filtered or unexported fields
}
func SliceWithWidth ¶
func SliceWithWidth(line string, startCol int, length int, strict bool) SliceResult
type TUI ¶
type TUI struct {
Container
// contains filtered or unexported fields
}
func (*TUI) ForceRender ¶
func (t *TUI) ForceRender()
func (*TUI) GetFullRedraws ¶
func (*TUI) GetShowHardwareCursor ¶
func (*TUI) HandleInput ¶
func (*TUI) QueryCellSize ¶
func (t *TUI) QueryCellSize()
func (*TUI) SetClearOnShrink ¶
func (*TUI) SetFocus ¶
SetFocus sets the component that currently receives keyboard input. In a TUI, only one interactive component (editor, selector, list, etc.) can receive input at a time. This method switches the "input focus": first unfocus the old component, then focus the new one.
func (*TUI) SetLogDir ¶ added in v0.14.0
SetLogDir sets the directory for debug and crash logs (fasttui-debug.log, fasttui-crash.log). Call before Start. Empty restores the default (FASTTUI_LOG_DIR or ~/.fasttui).
func (*TUI) SetShowHardwareCursor ¶
func (*TUI) TriggerRender ¶
func (t *TUI) TriggerRender()