console

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2022 License: MIT Imports: 8 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.
s := strings.ReplaceAll(fake.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 struct {
	// contains filtered or unexported fields
}

func System

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

Hello, world!

func (*Console) ColorScheme added in v0.3.0

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

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

Example
// Set up a console with stdout redirected, but not stderr.
fake := console.Fake(
	console.WithStdoutTTY(false),
	console.WithStderrTTY(true),
)

cs := fake.ColorScheme()

// Console.ColorScheme affects the Console i.e., stdout, so this isn't colored.
fmt.Fprintf(fake.Stderr(), "%s\n", cs.Red("Error: not red!"))

// Clone the ColorScheme to check if stderr is redirected or not.
cs = fake.ColorScheme().Clone(
	colorscheme.WithTTY(fake.IsStderrTTY),
)
fmt.Fprintf(fake.Stderr(), "%s\n", cs.Red("Error: red alert!"))

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

Error: not red!
\x1b[0;31mError: red alert!\x1b[0m

func (*Console) IsStderrTTY

func (c *Console) IsStderrTTY() bool

func (*Console) IsStdinTTY

func (c *Console) IsStdinTTY() bool

func (*Console) IsStdoutTTY

func (c *Console) IsStdoutTTY() bool

func (*Console) StartProgress added in v0.4.0

func (c *Console) StartProgress(label string, opts ...ProgressOption)

func (*Console) Stderr

func (c *Console) Stderr() io.Writer

func (*Console) Stdin

func (c *Console) Stdin() io.Reader

func (*Console) Stdout

func (c *Console) Stdout() io.Writer

func (*Console) StopProgress added in v0.4.0

func (c *Console) StopProgress()

func (*Console) Write

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

Write implements Writer on the console and calls Write on Stdout.

type FakeConsole

type FakeConsole struct {
	*Console
}

func Fake

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

Hello, fake!

func (*FakeConsole) Stderr

func (f *FakeConsole) Stderr() *bytes.Buffer

func (*FakeConsole) Stdin

func (f *FakeConsole) Stdin() *bytes.Buffer

func (*FakeConsole) Stdout

func (f *FakeConsole) Stdout() *bytes.Buffer

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 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(*spinner.Spinner)

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
pkg

Jump to

Keyboard shortcuts

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