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) IsStderrTTY ¶
func (*Console) IsStdinTTY ¶
func (*Console) IsStdoutTTY ¶
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
type FakeOption ¶
type FakeOption func(*FakeConsole)
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
Click to show internal directories.
Click to hide internal directories.