stream

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package stream provides NullStream

Packate stream defines input and output streams.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoTerminal = errors.New("ReadPasswordStrict: Stdin is not a terminal")

ErrNoTerminal is returned by ReadPasswordStrict() when stdin is not a terminal

View Source
var Null io.ReadWriteCloser = nullStream{}

Null is an io.ReadWriteCloser.

Reads from it return 0 bytes and io.EOF. Writes and Closes succeed without doing anything.

See also io.Discard.

Functions

func ReadLine added in v0.0.8

func ReadLine(reader io.Reader) (value string, err error)

ReadLine reads the current line from the provided reader.

A line is considered to end when one of the following is encountered: '\r\n', '\n' or EOF or '\r' followed by EOF. Note that only a '\r' is not considered an end-of-line.

The returned line never contains the end-of-line markers, such as '\n' or '\r\n'. A line may be empty, however when only EOF is read, returns "", EOF.

Example
input := strings.NewReader("line1\nline2\r\n\n\r\nline5")

fmt.Println(ReadLine(input))
fmt.Println(ReadLine(input))
fmt.Println(ReadLine(input))
fmt.Println(ReadLine(input))
fmt.Println(ReadLine(input))
fmt.Println(ReadLine(input))
Output:

line1 <nil>
line2 <nil>
 <nil>
 <nil>
line5 <nil>
 EOF

func ReadPassword added in v0.0.8

func ReadPassword(reader io.Reader) (value string, err error)

ReadPassword is like ReadLine, except that it turns off terminal echo. When standard input is not a terminal, behaves like ReadLine()

func ReadPasswordStrict added in v0.0.8

func ReadPasswordStrict(reader io.Reader) (value string, err error)

ReadPasswordSrict is like ReadPassword, except that when stdin is not a terminal, returns ErrNoTerminal.

Types

type IOStream

type IOStream struct {
	Stdin          io.Reader
	Stdout, Stderr io.Writer
	// contains filtered or unexported fields
}

IOStream represents a set of input and output streams commonly associated to a process.

func FromDebug added in v0.0.13

func FromDebug() IOStream

FromDebug returns a stream that behaves like FromNil when debugging is disabled. When debugging is enabled (by building with the "debug" tag), behaves like FromEnv instead.

func FromEnv

func FromEnv() IOStream

FromEnv creates a new IOStream using the environment.

The Stdin, Stdout and Stderr streams are used from the os package.

func FromNil

func FromNil() IOStream

FromNil creates a new IOStream that silences all output and provides no input.

func NewIOStream

func NewIOStream(Stdout, Stderr io.Writer, Stdin io.Reader, wrap int) IOStream

NewIOStream creates a new IOStream with the provided readers and writers. If any of them are set to an empty stream, they are set to util.NullStream. When wrap is set to 0, it is set to a reasonable default.

It furthermore wraps output as set by wrap.

func (IOStream) Die

func (io IOStream) Die(err error) error

Die prints a non-nil err to io.Stderr and returns an error of type Error or nil.

When error is non-nil, this function first turns err into type Error. Then if err.Message is not the empty string, it prints it to io.Stderr with wrapping.

If err is nil, it does nothing and returns nil.

func (IOStream) EPrint added in v0.0.13

func (io IOStream) EPrint(args ...interface{}) (n int, err error)

EPrint is like fmt.Print but prints to io.Stderr.

func (IOStream) EPrintf

func (io IOStream) EPrintf(format string, args ...interface{}) (n int, err error)

EPrintf is like fmt.Printf but prints to io.Stderr.

func (IOStream) EPrintln

func (io IOStream) EPrintln(args ...interface{}) (n int, err error)

EPrintln is like fmt.Println but prints to io.Stderr.

func (IOStream) NonInteractive added in v0.0.12

func (io IOStream) NonInteractive() IOStream

NonInteractive creates a new IOStream with Null as standard input.

func (IOStream) Print added in v0.0.13

func (io IOStream) Print(args ...interface{}) (n int, err error)

Print is like fmt.Print but prints to io.Stdout.

func (IOStream) Printf

func (io IOStream) Printf(format string, args ...interface{}) (n int, err error)

Printf is like fmt.Printf but prints to io.Stdout.

func (IOStream) Println

func (io IOStream) Println(args ...interface{}) (n int, err error)

Println is like fmt.Println but prints to io.Stdout.

func (IOStream) ReadLine added in v0.0.8

func (io IOStream) ReadLine() (string, error)

ReadLine is like ReadLine(io.Stdin)

func (IOStream) ReadPassword added in v0.0.8

func (io IOStream) ReadPassword() (string, error)

ReadPassword is like ReadPassword(io.Stdin)

func (IOStream) ReadPasswordStrict added in v0.0.8

func (io IOStream) ReadPasswordStrict() (string, error)

ReadPasswordStrict is like ReadPasswordStrict(io.Stdin)

func (IOStream) StderrWriteWrap

func (io IOStream) StderrWriteWrap(s string) (int, error)

StderrWriteWrap is like

io.Stdout.Write([]byte(s + "\n"))

but wrapped at length Wrap.

func (IOStream) StdinIsATerminal added in v0.0.9

func (io IOStream) StdinIsATerminal() bool

StdinIsATerminal checks if standard input is a terminal

func (IOStream) StdoutWriteWrap

func (io IOStream) StdoutWriteWrap(s string) (int, error)

StdoutWriteWrap is like

io.Stdout.Write([]byte(s + "\n"))

but wrapped at a reasonable length

func (IOStream) Streams added in v0.0.12

func (io IOStream) Streams(Stdout, Stderr io.Writer, Stdin io.Reader, wrap int) IOStream

Streams creates a new IOStream with the provided streams and wrap. If any parameter is the zero value, copies the values from io.

Jump to

Keyboard shortcuts

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