term

package
v0.0.0-...-3f99bb8 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

The term package provides a way to capture stdout and stderr and display the output in a browser. The package also provides a way to display charts in the browser. If you want to display charts, you can use the DataFrame type from the df package. Or you can leverage the Chart interface to display any chart in the go-echarts library.

The Escape* functions are used to wrap the content in HTML tags so that it can be displayed in a browser.

Index

Constants

View Source
const (
	// HtmlTag is a special tag used to wrap HTML content in the buffer.
	// None html content will be wrapped in <pre> tag.
	HtmlTag       = "==========76ADCBF0-980B-4C05-951F-63340F35E9C=========="
	MaxBuffersize = 1024 * 1024 * 1024 // 1GB
)
View Source
const BlockStyle = `` /* 718-byte string literal not displayed */

Block div for html content such as charts and plots. For x-axis, it will take up the full width of the parent element, center it's content if it's smaller than the parent element, and overflow on the x-axis if it's larger. For y-axis, it will either use a fixed height or dynamically adjust to the content. Input: (x, y) Block: (width: 100%, height: auto|y) Child: (width: 100%|x, height: 100%)

View Source
const BodyStyle = `` /* 221-byte string literal not displayed */
View Source
const IframeStyle = `` /* 230-byte string literal not displayed */
View Source
const ScrollScript = `` /* 1286-byte string literal not displayed */
View Source
const TextStyle = `` /* 1003-byte string literal not displayed */

Variables

This section is empty.

Functions

func BindPort

func BindPort(port int) func(t *Term)

BindPort will start a web server to serve the terminal output on the specified port.

func Block

func Block(e BlockElement, ops ...BlockOption)

func BlockSize

func BlockSize(e BlockElement, width, height int, ops ...BlockOption)

func Close

func Close()

Close closes the terminal. This function should be called at the end of the program.

func Detach

func Detach() func(t *Term)

Detach from the stdout/stderr of the current process.

func EscapeIframe

func EscapeIframe(pageHtml string, klass string) string

EscapeIframe wraps the given HTML content in an iframe tag and escapes it for srcdoc attribute. If the pageHtml starts with "http", it will be used as the source url of the iframe.

func Format

func Format(format OutputFormat) func(t *Term)

Format sets the format of the terminal output. The default is FormatRaw.

func HTML

func HTML(page bool) iter.Seq[string]

HTML returns a sequence of strings for the HTML content. If page is true, the HTML content is a full page. Otherwise, it is a fragment. One should only call this function when the format option is set to Custom.

func NewThreadSafeWriter

func NewThreadSafeWriter(w io.Writer) *threadSafeWriter

func Open

func Open(options ...TermOption)

Open opens the terminal. This function should be called at the beginning of the program.

func PrintBlock

func PrintBlock(html string, ops ...BlockOption)

func PrintBlockSize

func PrintBlockSize(html string, width, height int, ops ...BlockOption)

PrintBlockSize supports HTML Page, Iframe, and other HTML elements.

func PrintHtml

func PrintHtml(html string)

PrintHtml prints the given HTML content to the terminal.

Types

type BlockElement

type BlockElement interface {
	HTML() string // Returns the HTML content of the block
}

BlockElement represents a horizontally centered or scrollable block of HTML content. There are two types of block elements: one can have auto size (such as an img), and the other can not (such as an iframe). For elements which can not have auto size, the box element should have a 100% width and auto overflow-x. So that it can take the responsibility for horizontal scrolling from the row element.

func ImageData

func ImageData(mime string, data []byte) BlockElement

type BlockOption

type BlockOption func(*blockConfig)

func BackgroundOption

func BackgroundOption(c color.Color) BlockOption

func ColorOption

func ColorOption(c color.Color) BlockOption

func OpacityOption

func OpacityOption(x float64) BlockOption

func SizeOption

func SizeOption(width, height int) BlockOption

type BlockWithOption

type BlockWithOption interface {
	BlockElement
	Options() []BlockOption
}

type Buffer

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

Buffer is a simple in-memory buffer that can be used as an io.Reader or io.Writer. It's like bytes.Buffer, but it reads and writes to a channel instead of a byte slice. So the read and write operations can block until data is available. One of the NewBuffer* functions should be used to create a new buffer. The Close method should be called to notify readers that no more data will be written.

func NewBuffer

func NewBuffer() *Buffer

func NewBufferChan

func NewBufferChan(ch chan string) *Buffer

func NewBufferSize

func NewBufferSize(size int) *Buffer

func NewBufferString

func NewBufferString(s string) *Buffer

func (*Buffer) Close

func (b *Buffer) Close() error

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Read reads data from the channel and returns it in p. It will block until data is available or the channel is closed.

func (*Buffer) String

func (b *Buffer) String() string

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

type Image

type Image string

func (Image) HTML

func (url Image) HTML() string

type OutputFormat

type OutputFormat int
const (
	HTMLWindow  OutputFormat = iota // Open in browser
	HTMLPage                        // Print HTML page
	HTMLContent                     // Print HTML content
	Raw                             // Print raw text, useful for debugging
	Custom                          // Print nothing, user is expected to call the HTML function
)

type Term

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

Term captures stdout and stderr and provides methods to display the output in a browser.

func NewTerm

func NewTerm() *Term

NewTerm creates a new Term and copies stdout and stderr to a internal buffer. The output can be displayed in a browser when you use the Open method with the default HTMLWindow format. See the Format options for other ways to display the output.

func (*Term) Close

func (t *Term) Close()

Close stops capturing stdout and stderr and restores the original stdout and stderr.

func (*Term) HTML

func (t *Term) HTML(fullPage bool) iter.Seq[string]

HTML returns a sequence of strings that represent the terminal output in HTML format. If fullPage is true, the output will be wrapped in a full HTML page with styles. Otherwise, the output will be some HTML content that can be embedded in a page.

func (*Term) Open

func (t *Term) Open(options ...TermOption)

type TermOption

type TermOption func(*Term)

Jump to

Keyboard shortcuts

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