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 ¶
- type Console
- func (c *Console) ColorScheme() *colorscheme.ColorScheme
- func (c *Console) IsStderrTTY() bool
- func (c *Console) IsStdinTTY() bool
- func (c *Console) IsStdoutTTY() bool
- func (c *Console) StartProgress(label string, opts ...ProgressOption)
- func (c *Console) Stderr() io.Writer
- func (c *Console) Stdin() io.Reader
- func (c *Console) Stdout() io.Writer
- func (c *Console) StopProgress()
- func (c *Console) Write(p []byte) (n int, err error)
- type FakeConsole
- type FakeOption
- func WithColorScheme(cs *colorscheme.ColorScheme) FakeOption
- func WithStderr(stderr *bytes.Buffer) FakeOption
- func WithStderrTTY(tty bool) FakeOption
- func WithStdin(stdin *bytes.Buffer) FakeOption
- func WithStdinTTY(tty bool) FakeOption
- func WithStdout(stdout *bytes.Buffer) FakeOption
- func WithStdoutTTY(tty bool) FakeOption
- type ProgressOption
- type ProgressStyle
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 (*Console) IsStdinTTY ¶
func (*Console) IsStdoutTTY ¶
func (*Console) StartProgress ¶ added in v0.4.0
func (c *Console) StartProgress(label string, opts ...ProgressOption)
func (*Console) StopProgress ¶ added in v0.4.0
func (c *Console) StopProgress()
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 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
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 )
Click to show internal directories.
Click to hide internal directories.