console

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2022 License: MIT Imports: 11 Imported by: 3

README

Go Console APIs

releases reference ci

These Go Console APIs provide a level of abstraction over virtual terminals, implement testable io.Writer, and support color schemes.

Example

You can create a new console using os streams:

package main

import (
    "fmt"

    "github.com/heaths/go-console"
)

func main() {
    con := console.System()
    fmt.Fprintln(con.Stdout(), "Hello, world!")
}
Fake

You can also create a new fake console that uses bytes.Buffer you can access from Stdout() as well:

package main

import (
    "fmt"

    "github.com/heaths/go-console"
)

func main() {
    fake := console.Fake()
    fmt.Fprintln(fake.Stdout(), "Hello, fake!")
    fmt.Println(fake.Stdout().String())
}

License

This project is licensed under the MIT license.

Documentation

Overview

Example
stdin := bytes.NewBufferString("31\tred\n32\tgreen\n")

// Set up fake console with stdin, and stdout as TTY.
fake := console.Fake(
	console.WithStdin(stdin),
	console.WithStdoutTTY(true),
)

// Scan color codes and descriptions from fake stdin.
scanner := bufio.NewScanner(fake.Stdin())
for scanner.Scan() {
	var color int
	var desc string

	// Write scanned color codes to fake stdout.
	if _, err := fmt.Sscanf(scanner.Text(), "%d %s", &color, &desc); err == nil {
		fmt.Fprintf(fake.Stdout(), "\x1b[0;%dm%s\x1b[0m", color, desc)
	}
}

// Doubly escape fake stdout and write to real stdout to assert output.
stdout, _, _ := fake.Buffers()
s := strings.ReplaceAll(stdout.String(), "\x1b", `\x1b`)
fmt.Println(s)
Output:

\x1b[0;31mred\x1b[0m\x1b[0;32mgreen\x1b[0m

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Console

type Console interface {
	Stdout() io.Writer
	Stderr() io.Writer
	Stdin() io.Reader
	IsStdoutTTY() bool
	IsStderrTTY() bool
	IsStdinTTY() bool
	Size() (width, height int, err error)

	io.Writer

	ColorScheme() *colorscheme.ColorScheme
	Reset()

	StartProgress(label string, opts ...ProgressOption)
	StopProgress()

	ClearLine()
	ClearLines(rows int)
	ClearScreen()
	StartAlternativeScreenBuffer()
	StopAlternativeScreenBuffer()

	MoveCursor(rows, columns int)
	CursorUp(rows int)
	CursorDown(rows int)
	CursorForward(columns int)
	CursorBack(columns int)
	CursorColumn(column int)
}

func System

func System() Console
Example
// Create console from system streams.
con := console.System()
fmt.Fprintln(con.Stdout(), "Hello, world!")
Output:

Hello, world!

type FakeConsole

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

func Fake

func Fake(opts ...FakeOption) *FakeConsole
Example
// Create fake console from buffers.
fake := console.Fake()
fmt.Fprintf(fake.Stdout(), "Hello, fake!")

stdout, _, _ := fake.Buffers()
fmt.Println(stdout.String())
Output:

Hello, fake!

func (*FakeConsole) Buffers added in v0.6.0

func (f *FakeConsole) Buffers() (stdout, stderr, stdin *bytes.Buffer)

func (FakeConsole) ClearLine added in v0.7.0

func (c FakeConsole) ClearLine()

func (FakeConsole) ClearLines added in v0.7.0

func (c FakeConsole) ClearLines(rows int)

func (FakeConsole) ClearScreen added in v0.7.0

func (c FakeConsole) ClearScreen()

func (FakeConsole) ColorScheme added in v0.6.0

func (c FakeConsole) ColorScheme() *colorscheme.ColorScheme

ColorScheme gets the color scheme for the console i.e., Stdout.

func (FakeConsole) CursorBack added in v0.7.0

func (c FakeConsole) CursorBack(columns int)

func (FakeConsole) CursorColumn added in v0.7.0

func (c FakeConsole) CursorColumn(column int)

func (FakeConsole) CursorDown added in v0.7.0

func (c FakeConsole) CursorDown(rows int)

func (FakeConsole) CursorForward added in v0.7.0

func (c FakeConsole) CursorForward(columns int)

func (FakeConsole) CursorUp added in v0.7.0

func (c FakeConsole) CursorUp(rows int)

func (FakeConsole) IsStderrTTY added in v0.6.0

func (c FakeConsole) IsStderrTTY() bool

func (FakeConsole) IsStdinTTY added in v0.6.0

func (c FakeConsole) IsStdinTTY() bool

func (FakeConsole) IsStdoutTTY added in v0.6.0

func (c FakeConsole) IsStdoutTTY() bool

func (FakeConsole) MoveCursor added in v0.7.0

func (c FakeConsole) MoveCursor(row, column int)

func (FakeConsole) Reset added in v0.7.0

func (c FakeConsole) Reset()

func (FakeConsole) Size added in v0.8.0

func (c FakeConsole) Size() (width, height int, err error)

func (FakeConsole) StartAlternativeScreenBuffer added in v0.7.0

func (c FakeConsole) StartAlternativeScreenBuffer()

func (FakeConsole) StartProgress added in v0.6.0

func (c FakeConsole) StartProgress(label string, opts ...ProgressOption)

func (FakeConsole) Stderr

func (c FakeConsole) Stderr() io.Writer

func (FakeConsole) Stdin

func (c FakeConsole) Stdin() io.Reader

func (FakeConsole) Stdout

func (c FakeConsole) Stdout() io.Writer

func (FakeConsole) StopAlternativeScreenBuffer added in v0.7.0

func (c FakeConsole) StopAlternativeScreenBuffer()

func (FakeConsole) StopProgress added in v0.6.0

func (c FakeConsole) StopProgress()

func (*FakeConsole) Write

func (f *FakeConsole) Write(p []byte) (n int, err error)

type FakeOption

type FakeOption func(*FakeConsole)

func WithColorScheme added in v0.3.0

func WithColorScheme(cs *colorscheme.ColorScheme) FakeOption

func WithSize added in v0.8.0

func WithSize(width, height int) FakeOption

func WithStderr

func WithStderr(stderr *bytes.Buffer) FakeOption

func WithStderrTTY

func WithStderrTTY(tty bool) FakeOption

func WithStdin

func WithStdin(stdin *bytes.Buffer) FakeOption

func WithStdinTTY

func WithStdinTTY(tty bool) FakeOption

func WithStdout

func WithStdout(stdout *bytes.Buffer) FakeOption

func WithStdoutTTY

func WithStdoutTTY(tty bool) FakeOption

type ProgressOption added in v0.4.0

type ProgressOption func(*con, *spinner.Spinner)

func WithMinimum added in v0.7.0

func WithMinimum(d time.Duration) ProgressOption

func WithProgressStyle added in v0.4.0

func WithProgressStyle(style ProgressStyle) ProgressOption

type ProgressStyle added in v0.4.0

type ProgressStyle int
const (
	// https://github.com/briandowns/spinner#available-character-sets
	ProgressStyleBars ProgressStyle = 9
	ProgressStyleDots ProgressStyle = 11
)

Directories

Path Synopsis
examples
internal
pkg

Jump to

Keyboard shortcuts

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