Skip to content
Myr

renderer.go

This file

Path

renderer.go

104 lines · 2.6 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package tea23import (4	"fmt"56	"github.com/charmbracelet/colorprofile"7	"github.com/charmbracelet/x/ansi"8)910const (11	// defaultFramerate specifies the maximum interval at which we should12	// update the view.13	defaultFPS = 6014	maxFPS     = 12015)1617// renderer is the interface for Bubble Tea renderers.18type renderer interface {19	// start starts the renderer.20	start()2122	// close closes the renderer and flushes any remaining data.23	close() error2425	// render renders a frame to the output.26	render(View)2728	// flush flushes the renderer's buffer to the output.29	flush(closing bool) error3031	// reset resets the renderer's state to its initial state.32	reset()3334	// insertAbove inserts unmanaged lines above the renderer.35	insertAbove(string) error3637	// setSyncdUpdates sets whether to use synchronized updates.38	setSyncdUpdates(bool)3940	// setWidthMethod sets the method for calculating the width of the terminal.41	setWidthMethod(ansi.Method)4243	// resize notify the renderer of a terminal resize.44	resize(int, int)4546	// setColorProfile sets the color profile.47	setColorProfile(colorprofile.Profile)4849	// clearScreen clears the screen.50	clearScreen()5152	// writeString writes a string to the renderer's output.53	writeString(string) (int, error)5455	// onMouse handles a mouse event.56	onMouse(MouseMsg) Cmd57}5859type printLineMessage struct {60	messageBody string61}6263// Println prints above the Program. This output is unmanaged by the program and64// will persist across renders by the Program.65//66// Unlike fmt.Println (but similar to log.Println) the message will be print on67// its own line.68//69// If the altscreen is active no output will be printed.70func Println(args ...any) Cmd {71	return func() Msg {72		return printLineMessage{73			messageBody: fmt.Sprint(args...),74		}75	}76}7778// Printf prints above the Program. It takes a format template followed by79// values similar to fmt.Printf. This output is unmanaged by the program and80// will persist across renders by the Program.81//82// Unlike fmt.Printf (but similar to log.Printf) the message will be print on83// its own line.84//85// If the altscreen is active no output will be printed.86func Printf(template string, args ...any) Cmd {87	return func() Msg {88		return printLineMessage{89			messageBody: fmt.Sprintf(template, args...),90		}91	}92}9394// encodeCursorStyle returns the integer value for the given cursor style and95// blink state.96func encodeCursorStyle(style CursorShape, blink bool) int {97	// We're using the ANSI escape sequence values for cursor styles.98	// We need to map both [style] and [steady] to the correct value.99	style = (style * 2) + 1 //nolint:mnd100	if !blink {101		style++102	}103	return int(style)104}