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 ¶
- type Console
- type FakeConsole
- func (f *FakeConsole) Buffers() (stdout, stderr, stdin *bytes.Buffer)
- func (c FakeConsole) ClearLine()
- func (c FakeConsole) ClearLines(rows int)
- func (c FakeConsole) ClearScreen()
- func (c FakeConsole) ColorScheme() *colorscheme.ColorScheme
- func (c FakeConsole) CursorBack(columns int)
- func (c FakeConsole) CursorColumn(column int)
- func (c FakeConsole) CursorDown(rows int)
- func (c FakeConsole) CursorForward(columns int)
- func (c FakeConsole) CursorUp(rows int)
- func (c FakeConsole) IsStderrTTY() bool
- func (c FakeConsole) IsStdinTTY() bool
- func (c FakeConsole) IsStdoutTTY() bool
- func (c FakeConsole) MoveCursor(row, column int)
- func (c FakeConsole) Reset()
- func (c FakeConsole) Size() (width, height int, err error)
- func (c FakeConsole) StartAlternativeScreenBuffer()
- func (c FakeConsole) StartProgress(label string, opts ...ProgressOption)
- func (c FakeConsole) Stderr() io.Writer
- func (c FakeConsole) Stdin() io.Reader
- func (c FakeConsole) Stdout() io.Writer
- func (c FakeConsole) StopAlternativeScreenBuffer()
- func (c FakeConsole) StopProgress()
- func (f *FakeConsole) Write(p []byte) (n int, err error)
- type FakeOption
- func WithColorScheme(cs *colorscheme.ColorScheme) FakeOption
- func WithSize(width, height int) 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 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) }
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) 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) 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) 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) StopAlternativeScreenBuffer ¶ added in v0.7.0
func (c FakeConsole) StopAlternativeScreenBuffer()
func (FakeConsole) StopProgress ¶ added in v0.6.0
func (c FakeConsole) StopProgress()
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
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 )
Click to show internal directories.
Click to hide internal directories.