console

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 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.
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

	io.Writer

	ColorScheme() *colorscheme.ColorScheme

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

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) 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) 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) 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) 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 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