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 ¶
- Variables
- func Register(name string, obj Constructor)
- type ConsoleIn
- func (co *ConsoleIn) BlockForCharacterNoEcho() (byte, error)
- func (co *ConsoleIn) BlockForCharacterWithEcho() (byte, error)
- func (co *ConsoleIn) GetDriver() ConsoleInput
- func (co *ConsoleIn) GetDrivers() []string
- func (co *ConsoleIn) GetInterruptCount() int
- func (co *ConsoleIn) GetName() string
- func (co *ConsoleIn) GetSystemCommandPrefix() string
- func (co *ConsoleIn) PendingInput() bool
- func (co *ConsoleIn) ReadLine(max uint8) (string, error)
- func (co *ConsoleIn) SetInterruptCount(val int)
- func (co *ConsoleIn) SetSystemCommandPrefix(str string)
- func (co *ConsoleIn) Setup() error
- func (co *ConsoleIn) StuffInput(input string)
- func (co *ConsoleIn) TearDown() error
- type ConsoleInput
- type Constructor
- type EchoStatus
- type ErrorInput
- type FileInput
- type STTYInput
- type TermboxInput
Constants ¶
This section is empty.
Variables ¶
var ErrInterrupted error = fmt.Errorf("INTERRUPTED")
ErrInterrupted is returned if the user presses Ctrl-C when in our ReadLine function.
var ErrShowOutput error = fmt.Errorf("SHOW_OUTPUT")
ErrShowOutput is a special sentinel value that is used to signal that the ReadLine function needs to write some output to the console. As that would be a layering violation it is handled indirectly.
var (
// ErrorInputName contains the name of this driver.
ErrorInputName = "error"
)
var (
// FileInputName contains the name of this driver
FileInputName = "file"
)
var (
// STTYInputName contains the name of this driver.
STTYInputName = "stty"
)
var (
// TermboxInputName contains the name of this driver
TermboxInputName = "term"
)
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 (*ConsoleIn) BlockForCharacterNoEcho ¶
BlockForCharacterNoEcho proxies into our registered console-input driver.
func (*ConsoleIn) BlockForCharacterWithEcho ¶
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
GetDrivers returns all available driver-names.
We hide the internal "file" driver.
func (*ConsoleIn) GetInterruptCount ¶ added in v0.8.0
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) GetSystemCommandPrefix ¶ added in v0.15.0
GetSystemCommandPrefix returns the value of the system-command prefix.
func (*ConsoleIn) PendingInput ¶ added in v0.11.0
PendingInput proxies into our registered console-input driver.
func (*ConsoleIn) ReadLine ¶
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
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
SetSystemCommandPrefix enables the use of system-commands in our readline function.
func (*ConsoleIn) StuffInput ¶ added in v0.9.0
StuffInput 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 ErrorInput ¶ added in v0.18.0
type ErrorInput struct {
}
ErrorInput is an input-driver that only returns errors, and is used for testing.
func (*ErrorInput) BlockForCharacterNoEcho ¶ added in v0.18.0
func (ei *ErrorInput) BlockForCharacterNoEcho() (byte, error)
BlockForCharacterNoEcho always returns an error when invoked to read pending input.
func (*ErrorInput) GetName ¶ added in v0.18.0
func (ei *ErrorInput) GetName() string
GetName returns the name of this driver, "error".
func (*ErrorInput) PendingInput ¶ added in v0.18.0
func (ei *ErrorInput) PendingInput() bool
PendingInput always pretends input is pending.
However when input is polled for, via BlockForCharacterNoEcho, an error will always be returned.
func (*ErrorInput) TearDown ¶ added in v0.18.0
func (ei *ErrorInput) TearDown() error
TearDown is a NOP.
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 and returning the contents of a file ("input.txt" by default, but this may be changed).
The input-driver is primarily designed for testing and automation. We make a tiny pause between our functions and for every input we allow some conditional support for changing the line-endings which are used when replaying the various test-cases.
func (*FileInput) BlockForCharacterNoEcho ¶ added in v0.16.0
BlockForCharacterNoEcho returns the next character from the file we use to fake our input.
func (*FileInput) GetName ¶ added in v0.16.0
GetName is part of the module API, and returns the name of this driver.
func (*FileInput) PendingInput ¶ added in v0.16.0
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.
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
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
GetName is part of the module API, and returns the name of this driver.
func (*STTYInput) PendingInput ¶ added in v0.14.0
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.
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.