repl

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: GPL-3.0 Imports: 8 Imported by: 2

Documentation

Overview

Package repl provides a REPL implementation and options to expose Sabre features through a read-eval-print-loop.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrMapper

type ErrMapper func(err error) error

ErrMapper should map a custom Input error to nil to indicate error that should be ignored by REPL, EOF to signal end of REPL session and any other error to indicate a irrecoverable failure.

type Input

type Input interface {
	SetPrompt(string)
	Readline() (string, error)
}

Input implementation is used by REPL to read user-input. See WithInput() REPL option to configure an Input.

type NamespacedScope

type NamespacedScope interface {
	CurrentNS() string
}

NamespacedScope can be implemented by sabre.Scope implementations to allow namespace based isolation (similar to Clojure). REPL will call CurrentNS() method to get the current Namespace and display it as part of input prompt.

type Option

type Option func(repl *REPL)

Option implementations can be provided to New() to configure the REPL during initialization.

func WithBanner

func WithBanner(banner string) Option

WithBanner sets the REPL's banner which is displayed once when the REPL starts.

func WithInput

func WithInput(in Input, mapErr ErrMapper) Option

WithInput sets the REPL's input stream. `nil` defaults to bufio.Scanner backed by os.Stdin

func WithOutput

func WithOutput(w io.Writer) Option

WithOutput sets the REPL's output stream.`nil` defaults to stdout.

func WithPrinter added in v0.3.1

func WithPrinter(f func(io.Writer, interface{}) error) Option

WithPrinter sets the print function for the REPL. It is useful for customizing how different types should be rendered into human-readable character streams.

func WithPrompts

func WithPrompts(oneLine, multiLine string) Option

WithPrompts sets the prompt to be displayed when waiting for user input in the REPL.

func WithReaderFactory

func WithReaderFactory(factory ReaderFactory) Option

WithReaderFactory can be used set factory function for initializing sabre Reader. This is useful when you want REPL to use custom reader instance.

type REPL

type REPL struct {
	// contains filtered or unexported fields
}

REPL implements a read-eval-print loop for a generic Runtime.

func New

func New(scope sabre.Scope, opts ...Option) *REPL

New returns a new instance of REPL with given sabre Scope. Option values can be used to configure REPL input, output etc.

func (*REPL) Loop

func (repl *REPL) Loop(ctx context.Context) error

Loop starts the read-eval-print loop. Loop runs until context is cancelled or input stream returns an irrecoverable error (See WithInput()).

func (*REPL) Write

func (repl *REPL) Write(b []byte) (int, error)

type ReaderFactory

type ReaderFactory interface {
	NewReader(r io.Reader) *sabre.Reader
}

ReaderFactory should return an instance of reader when called. This might be called repeatedly. See WithReaderFactory()

type ReaderFactoryFunc

type ReaderFactoryFunc func(r io.Reader) *sabre.Reader

ReaderFactoryFunc implements ReaderFactory using a function value.

func (ReaderFactoryFunc) NewReader

func (factory ReaderFactoryFunc) NewReader(r io.Reader) *sabre.Reader

NewReader simply calls the wrapped function value and returns the result.

Jump to

Keyboard shortcuts

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