consolein

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package consolein is an abstraction over console input.

We support two methods of getting input, whilst selectively disabling/enabling echo - the use of `termbox' and the use of the `stty` binary.

Index

Constants

This section is empty.

Variables

View Source
var ErrInterrupted error = fmt.Errorf("INTERRUPTED")

ErrInterrupted is returned if the user presses Ctrl-C when in our ReadLine function.

Functions

func Register added in v0.14.0

func Register(name string, obj Constructor)

Register makes a console driver available, by name.

When one needs to be created the constructor can be called to create an instance of it.

Types

type ConsoleIn

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

ConsoleIn holds our state, which is basically just a pointer to the object handling our input

func New

func New(name string) (*ConsoleIn, error)

New is our constructor, it creates an input device which uses the specified driver.

func (*ConsoleIn) BlockForCharacterNoEcho

func (co *ConsoleIn) BlockForCharacterNoEcho() (byte, error)

BlockForCharacterNoEcho proxies into our registered console-input driver.

func (*ConsoleIn) BlockForCharacterWithEcho

func (co *ConsoleIn) BlockForCharacterWithEcho() (byte, error)

BlockForCharacterWithEcho blocks for input and shows that input before it is returned.

This function DOES NOT proxy to our registered console-input driver.

func (*ConsoleIn) GetDriver added in v0.14.0

func (co *ConsoleIn) GetDriver() ConsoleInput

GetDriver allows getting our driver at runtime.

func (*ConsoleIn) GetDrivers added in v0.14.0

func (co *ConsoleIn) GetDrivers() []string

GetDrivers returns all available driver-names.

We hide the internal "file" driver.

func (*ConsoleIn) GetInterruptCount added in v0.8.0

func (co *ConsoleIn) GetInterruptCount() int

GetInterruptCount retrieves the number of consecutive Ctrl-C characters are required to trigger a reboot.

This function DOES NOT proxy to our registered console-input driver.

func (*ConsoleIn) GetName added in v0.14.0

func (co *ConsoleIn) GetName() string

GetName returns the name of our selected driver.

func (*ConsoleIn) GetSystemCommandPrefix added in v0.15.0

func (co *ConsoleIn) GetSystemCommandPrefix() string

GetSystemCommandPrefix returns the value of the system-command prefix.

func (*ConsoleIn) PendingInput added in v0.11.0

func (co *ConsoleIn) PendingInput() bool

PendingInput proxies into our registered console-input driver.

func (*ConsoleIn) ReadLine

func (co *ConsoleIn) ReadLine(max uint8) (string, error)

ReadLine handles the input of a single line of text.

This function DOES NOT proxy to our registered console-input driver.

func (*ConsoleIn) SetInterruptCount added in v0.8.0

func (co *ConsoleIn) SetInterruptCount(val int)

SetInterruptCount sets the number of consecutive Ctrl-C characters are required to trigger a reboot.

This function DOES NOT proxy to our registered console-input driver.

func (*ConsoleIn) SetSystemCommandPrefix added in v0.15.0

func (co *ConsoleIn) SetSystemCommandPrefix(str string)

SetSystemCommandPrefix enables the use of system-commands in our readline function.

func (*ConsoleIn) Setup added in v0.14.0

func (co *ConsoleIn) Setup() error

Setup proxies into our registered console-input driver.

func (*ConsoleIn) StuffInput added in v0.9.0

func (co *ConsoleIn) StuffInput(input string)

StuffInput proxies into our registered console-input driver.

func (*ConsoleIn) TearDown added in v0.14.0

func (co *ConsoleIn) TearDown() error

TearDown proxies into our registered console-input driver.

type ConsoleInput added in v0.14.0

type ConsoleInput interface {

	// Setup performs any specific setup which is required.
	Setup() error

	// TearDown performs any specific cleanup which is required.
	TearDown() error

	// PendingInput returns true if there is pending input available to be read.
	PendingInput() bool

	// BlockForCharacterNoEcho reads a single character from the console, without
	// echoing it.
	BlockForCharacterNoEcho() (byte, error)

	// GetName will return the name of the driver.
	GetName() string
}

ConsoleInput is the interface that must be implemented by anything that wishes to be used as an input driver.

Providing this interface is implemented an object may register itself, by name, via the Register method.

You can compare this interface to the corresponding ConsoleOutput one, that delegates everything to the drivers rather than having some wrapper methods building upon the drivers as we do here.

type Constructor added in v0.14.0

type Constructor func() ConsoleInput

Constructor is the signature of a constructor-function which is used to instantiate an instance of a driver.

type EchoStatus added in v0.14.0

type EchoStatus int

EchoStatus is used to record our current state.

var (
	// Unknown means we don't know the status of echo/noecho
	Unknown EchoStatus = 0

	// Echo means that input will echo characters.
	Echo EchoStatus = 1

	// NoEcho means that input will not echo characters.
	NoEcho EchoStatus = 2
)

type FileInput added in v0.16.0

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

FileInput is an input-driver that returns fake "console input" by reading the content of the file "input.txt".

It is primarily designed for testing and automation. We make a tiny pause between our functions and for every input character that is a "#" character we sleep a single second.

We do this because there are some commands that poll for console input and cancel, or otherwise process it. For example the C compiler will poll for input when linking and if we don't give it some artificial delays we might find our pending input is swallowed at random - depending on the speed of the host.

func (*FileInput) BlockForCharacterNoEcho added in v0.16.0

func (fi *FileInput) BlockForCharacterNoEcho() (byte, error)

BlockForCharacterNoEcho returns the next character from the file we use to fake our input.

func (*FileInput) GetName added in v0.16.0

func (fi *FileInput) GetName() string

GetName is part of the module API, and returns the name of this driver.

func (*FileInput) PendingInput added in v0.16.0

func (fi *FileInput) PendingInput() bool

PendingInput returns true if there is pending input which we can return. This is always true unless we've exhausted the contents of our input-file.

func (*FileInput) Setup added in v0.16.0

func (fi *FileInput) Setup() error

Setup reads the contents of the file specified by the environmental variable $INPUT_FILE, and saves it away as a source of fake console input.

If no filename is chosen "input.txt" will be used as a default.

func (*FileInput) TearDown added in v0.16.0

func (fi *FileInput) TearDown() error

TearDown is a NOP.

type STTYInput added in v0.14.0

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

STTYInput is an input-driver that executes the 'stty' binary to toggle between echoing character input, and disabling the echo.

This is slow, as you can imagine, and non-portable outwith Unix-like systems. To mitigate against the speed-issue we keep track of "echo" versus "noecho" states, to minimise the executions.

func (*STTYInput) BlockForCharacterNoEcho added in v0.14.0

func (si *STTYInput) BlockForCharacterNoEcho() (byte, error)

BlockForCharacterNoEcho returns the next character from the console, blocking until one is available.

NOTE: This function should not echo keystrokes which are entered.

func (*STTYInput) GetName added in v0.14.0

func (si *STTYInput) GetName() string

GetName is part of the module API, and returns the name of this driver.

func (*STTYInput) PendingInput added in v0.14.0

func (si *STTYInput) PendingInput() bool

PendingInput returns true if there is pending input from STDIN..

Note that we have to set RAW mode, without this input is laggy and zork doesn't run.

func (*STTYInput) Setup added in v0.14.0

func (si *STTYInput) Setup() error

Setup is a NOP.

func (*STTYInput) TearDown added in v0.14.0

func (si *STTYInput) TearDown() error

TearDown resets the state of the terminal.

type TermboxInput added in v0.14.0

type TermboxInput struct {

	// Cancel holds a context which can be used to close our polling goroutine
	Cancel context.CancelFunc
	// contains filtered or unexported fields
}

TermboxInput is our input-driver, using termbox

func (*TermboxInput) BlockForCharacterNoEcho added in v0.14.0

func (ti *TermboxInput) BlockForCharacterNoEcho() (byte, error)

BlockForCharacterNoEcho returns the next character from the console, blocking until one is available.

NOTE: This function should not echo keystrokes which are entered.

func (*TermboxInput) GetName added in v0.14.0

func (ti *TermboxInput) GetName() string

GetName is part of the module API, and returns the name of this driver.

func (*TermboxInput) PendingInput added in v0.14.0

func (ti *TermboxInput) PendingInput() bool

PendingInput returns true if there is pending input from STDIN.

func (*TermboxInput) Setup added in v0.14.0

func (ti *TermboxInput) Setup() error

Setup ensures that the termbox init functions are called, and our terminal is set into RAW mode.

func (*TermboxInput) TearDown added in v0.14.0

func (ti *TermboxInput) TearDown() error

TearDown resets the state of the terminal, disables the background polling of characters and generally gets us ready for exit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳