console

package module
v0.0.0-...-d142096 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: GPL-3.0 Imports: 11 Imported by: 0

README

faws-vcs/console

Go Reference

console provides an immediate-mode rendering framework for miniature terminal-based UIs.

In this package, you are provided with a live text area (called a Hud) that you can print to using your custom RendererFunc.

// Open the console
console.Open()

// Print a normal line to the background (always printed above the Hud)
console.Println("Hello, this is normal line")

// Set a Hud renderer function
console.RenderFunc(func(hud *console.Hud) {
    var spin console.Spinner
    // foreground color = BrightBlue, background color = None (0)
    spin.Stylesheet.Sequence[0] = console.Cell{'⡿', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[1] = console.Cell{'⣟', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[2] = console.Cell{'⣯', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[3] = console.Cell{'⣷', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[4] = console.Cell{'⣾', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[5] = console.Cell{'⣽', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[6] = console.Cell{'⣻', console.BrightBlue, 0}
    spin.Stylesheet.Sequence[7] = console.Cell{'⢿', console.BrightBlue, 0}
    spin.Frequency = time.Second/3
    hud.Line(&spin)

    // Start displaying hud elements
    var progress_bar console.ProgressBar

    // | [====>   ]                |
    progress_bar.Stylesheet.Alignment = console.Left
    progress_bar.Stylesheet.Width = 10 // or console.Width() to fill the entire line

    progress_bar.Stylesheet.Sequence[console.PbCaseLeft] = console.Cell{'[', 0, 0}
    progress_bar.Stylesheet.Sequence[console.PbCaseRight] = console.Cell{']', 0, 0}
    progress_bar.Stylesheet.Sequence[console.PbFluid] = console.Cell{'=', 0, 0}
    progress_bar.Stylesheet.Sequence[console.PbVoid] = console.Cell{' ', 0, 0}
    progress_bar.Stylesheet.Sequence[console.PbTail] = console.Cell{'<', 0, 0}
    progress_bar.Stylesheet.Sequence[console.PbHead] = console.Cell{'>', 0, 0}

    progress_bar.Progress = 0.5

    var progress_text console.Text
    // | [====>   ] 1/2
    progress_text.Stylesheet.Alignment = console.Right
    // necessary to put space between text elements
    progress_text.Stylesheet.Margin[console.Left] = 1
    progress_text.Add(fmt.Sprintf("%d/%d", 1, 2), console.BrightGreen, console.None) 
    // render lines to the buffer
    hud.Line(&progress_bar, &progress_text)
})

// run your application here

// ...

// Manually trigger a re-render of the Hud from your app
console.SwapHud()

// Forces SwapHud to be called every second
console.SwapInterval(1 * time.Second)

console.Close()

Documentation

Index

Constants

View Source
const (
	// Stylesheet.Sequence indices for styling progress bars
	PbCaseLeft = iota
	PbCaseRight
	PbFluid
	PbVoid
	PbTail
	PbHead
)
View Source
const (
	Left = iota
	Right
	Center
)
View Source
const (
	// Use no color but the default
	None = iota
	Black
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	LightGray
	Gray
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	White
)

Variables

This section is empty.

Functions

func Close

func Close()

Required: shut down console systems at end of process

func Fatal

func Fatal(args ...any)
func Header(str string)

str ---

func Open

func Open()

Required: start up console systems at end of process

func Println

func Println(args ...any) (n int, err error)

Print generic information to os.Stdout

func Quote

func Quote(args ...any)

func RenderFunc

func RenderFunc(f RendererFunc)

Set a function to render your Hud

func SwapHud

func SwapHud()

Erases the previously printed hud and renders the updated version

func SwapInterval

func SwapInterval(t time.Duration)

Automatically swap at the designated interval

func Width

func Width() int

func WriteText

func WriteText(text []Cell, s string, fg, bg Color) (n int, err error)

Types

type Alignment

type Alignment uint8

type Cell

type Cell struct {
	Rune rune
	// Foreground color
	Fg Color
	// Background color
	Bg Color
}

A cell is a colorable UTF-8 character that takes up 1 width in the terminal (Hud).

type Color

type Color uint8

type Component

type Component interface {
	Render(line []Cell) (n int, err error)
}

type Hud

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

func (*Hud) Exiting

func (h *Hud) Exiting() bool

func (*Hud) Line

func (h *Hud) Line(components ...Component)

render a line

type ProgressBar

type ProgressBar struct {
	Stylesheet Stylesheet
	// Must be in range from 0-1
	Progress float64
}

func (*ProgressBar) Render

func (pb *ProgressBar) Render(line []Cell) (n int, err error)

type RendererFunc

type RendererFunc func(h *Hud)

type Spinner

type Spinner struct {
	Stylesheet Stylesheet
	// How long it takes to move to a new sequence
	Frequency time.Duration
}

func (*Spinner) Render

func (s *Spinner) Render(line []Cell) (n int, err error)

type Stylesheet

type Stylesheet struct {
	Alignment Alignment
	Width     int
	Margin    [2]int
	Sequence  [8]Cell
}

type Text

type Text struct {
	Stylesheet Stylesheet
	Text       []Cell
}

func (*Text) Add

func (t *Text) Add(s string, fg, bg Color) (err error)

func (*Text) Render

func (t *Text) Render(line []Cell) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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