console

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: 12 Imported by: 0

Documentation

Overview

Package console provides the terminal rendering engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Console

type Console struct {
	// contains filtered or unexported fields
}

Console handles terminal output with colors and styles.

func New

func New(opts ...Option) *Console

New creates a new Console.

func (*Console) ColorSystem

func (c *Console) ColorSystem() style.ColorSystem

ColorSystem returns the detected color system.

func (*Console) Height

func (c *Console) Height() int

Height returns the console height in cells.

func (*Console) IsTerminal

func (c *Console) IsTerminal() bool

IsTerminal returns true if output is a terminal.

func (*Console) Log

func (c *Console) Log(args ...any)

Log prints with a timestamp prefix (like Rich's console.log).

func (*Console) Options

func (c *Console) Options() Options

Options returns the default rendering options for this console.

func (*Console) PopRenderHook

func (c *Console) PopRenderHook() RenderHook

PopRenderHook removes the most recently added render hook.

func (*Console) Print

func (c *Console) Print(args ...any)

Print prints Rich-style markup to the console without a trailing newline.

Example:

console.Print("[bold]Hello[/] [red]World[/]")

func (*Console) PrintMarkup

func (c *Console) PrintMarkup(text markup.Text)

PrintMarkup prints pre-parsed markup segments.

func (*Console) PrintSegments

func (c *Console) PrintSegments(segments []segment.Segment)

PrintSegments writes segments directly to the console.

func (*Console) Printf

func (c *Console) Printf(format string, args ...any)

Printf prints formatted Rich-style markup to the console without a trailing newline.

Example:

console.Printf("[bold]Count:[/] %d\n", count)

func (*Console) Println added in v1.1.0

func (c *Console) Println(args ...any)

Println prints Rich-style markup to the console with a trailing newline.

Example:

console.Println("[bold]Hello[/] [red]World[/]")
console.Println("[italic green]Success![/]")

func (*Console) PushRenderHook

func (c *Console) PushRenderHook(hook RenderHook)

PushRenderHook adds a render hook.

func (*Console) Render

func (c *Console) Render(r Renderable)

Render renders a Renderable to the console.

func (*Console) RenderLines added in v1.0.2

func (c *Console) RenderLines(r Renderable, opts Options) [][]segment.Segment

RenderLines renders a Renderable into lines of segments. This is used internally by table rendering to get cell content as lines.

func (*Console) Rule

func (c *Console) Rule(title string, opts ...RuleOption)

Rule prints a horizontal rule with optional title.

func (*Console) Size

func (c *Console) Size() Dimensions

Size returns the console dimensions.

func (*Console) Sprint added in v1.0.10

func (c *Console) Sprint(args ...any) string

Sprint renders Rich-style markup args and returns the resulting ANSI string. Args are joined with spaces, like fmt.Sprint's space rules for non-strings.

Example:

s := console.Sprint("[bold]Hello[/] [red]World[/]")

func (*Console) Sprintf added in v1.0.10

func (c *Console) Sprintf(format string, args ...any) string

Sprintf renders formatted Rich-style markup and returns the resulting ANSI string.

Example:

s := console.Sprintf("[bold]Count:[/] %d", count)

func (*Console) Width

func (c *Console) Width() int

Width returns the console width in cells.

func (*Console) Write

func (c *Console) Write(p []byte) (n int, err error)

Write writes raw bytes to the console (implements io.Writer).

func (*Console) WriteControl

func (c *Console) WriteControl(ctrl segment.Control)

WriteControl writes a control sequence to the console.

func (*Console) WriteString

func (c *Console) WriteString(s string) (n int, err error)

WriteString writes a raw string to the console.

type ControlRenderable

type ControlRenderable struct {
	Control segment.Control
}

ControlRenderable wraps a Control as a Renderable.

func (ControlRenderable) Render

func (cr ControlRenderable) Render(c *Console, opts Options) []segment.Segment

Render implements Renderable.

type Dimensions

type Dimensions struct {
	Width  int
	Height int
}

Dimensions represents terminal dimensions in cells.

type Justify

type Justify int

Justify determines text justification.

const (
	JustifyDefault Justify = iota
	JustifyLeft
	JustifyCenter
	JustifyRight
	JustifyFull
)

type Measurable

type Measurable interface {
	Measure(c *Console, opts Options) Measurement
}

Measurable is implemented by renderables that can report their width.

type Measurement

type Measurement struct {
	Minimum int
	Maximum int
}

Measurement represents the min and max width of a renderable.

func NewMeasurement

func NewMeasurement(min, max int) Measurement

NewMeasurement creates a measurement with the given min and max.

func (Measurement) Clamp

func (m Measurement) Clamp(width int) int

Clamp clamps a width to this measurement's bounds.

func (Measurement) Span

func (m Measurement) Span() int

Span returns max - min.

type Option

type Option func(*Console)

Option configures a Console.

func WithColorSystem

func WithColorSystem(cs style.ColorSystem) Option

WithColorSystem sets the color system.

func WithEnviron

func WithEnviron(env map[string]string) Option

WithEnviron sets a custom environment mapping for testing. This allows overriding environment variables without modifying the actual environment. Matches Python Rich's _environ parameter pattern.

func WithForceTerminal

func WithForceTerminal(force bool) Option

WithForceTerminal forces terminal mode on or off.

func WithNoColor

func WithNoColor(noColor bool) Option

WithNoColor disables all color output.

func WithWidth

func WithWidth(width int) Option

WithWidth sets a fixed width (overrides terminal detection).

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter sets the output writer.

type Options

type Options struct {
	Size        Dimensions
	MinWidth    int
	MaxWidth    int
	MaxHeight   int
	IsTerminal  bool
	ASCIIOnly   bool
	ColorSystem style.ColorSystem
	NoWrap      bool
	Overflow    Overflow
	Justify     Justify
}

Options carries rendering constraints passed to every Renderable.

func (Options) WithMaxWidth

func (o Options) WithMaxWidth(width int) Options

WithMaxWidth returns a copy with the given max width.

func (Options) WithNoWrap

func (o Options) WithNoWrap(noWrap bool) Options

WithNoWrap returns a copy with no-wrap set.

type Overflow

type Overflow int

Overflow determines how text overflow is handled.

const (
	OverflowFold     Overflow = iota // Wrap at width
	OverflowCrop                     // Truncate at width
	OverflowEllipsis                 // Truncate with ellipsis
)

type RenderHook

type RenderHook interface {
	ProcessRenderables(renderables []Renderable) []Renderable
}

RenderHook allows interception of the console's print pipeline. Used by Live to inject cursor-repositioning codes.

type Renderable

type Renderable interface {
	Render(c *Console, opts Options) []segment.Segment
}

Renderable is the core protocol for anything that can be rendered.

type RuleOption added in v1.0.1

type RuleOption func(*ruleConfig)

RuleOption configures Rule rendering.

func WithRuleStyle added in v1.0.1

func WithRuleStyle(s string) RuleOption

WithRuleStyle sets the style for the rule line characters.

func WithTitleStyle added in v1.0.1

func WithTitleStyle(s string) RuleOption

WithTitleStyle sets the style for the title text.

type Segments

type Segments []segment.Segment

Segments is a Renderable that wraps pre-built segments.

func (Segments) Render

func (s Segments) Render(c *Console, opts Options) []segment.Segment

Render implements Renderable.

type Text

type Text struct {
	Content string
	Style   *style.Style
}

Text is a simple Renderable that wraps a styled string.

func NewText

func NewText(content string, s *style.Style) Text

NewText creates a new Text renderable.

func (Text) Measure

func (t Text) Measure(c *Console, opts Options) Measurement

Measure implements Measurable.

func (Text) Render

func (t Text) Render(c *Console, opts Options) []segment.Segment

Render implements Renderable.

Jump to

Keyboard shortcuts

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