Documentation ¶
Overview ¶
Simple interactive fiction console based on http://github.com/nsf/termbox-go. Example usage: con := NewMiniCon() // create console defer con.Close() // defer cleanup // print some example text con.Status.Left, con.Status.Right = "Left!", "Right!" con.Println("Type `q` to quit; press `esc` to speed up text.") // loop till done for { // read user input userInput := con.Update() if userInput == "q" { break } // echo to the display d.Println(userInput) }
Index ¶
- Constants
- type CursorOutput
- type CursorRegion
- type CursorWidth
- type Hardlines
- type History
- type HistoryMarker
- type InputHandler
- type MiniCon
- type Pen
- type PendingWords
- type PrintSpeed
- type Prompt
- func (this *Prompt) Clear() string
- func (this *Prompt) DeleteRune(box *TermBox) *TermBox
- func (this *Prompt) RefreshCursor(box *TermBox) *TermBox
- func (this *Prompt) RefreshPrompt(box *TermBox) *TermBox
- func (this *Prompt) SetInput(box *TermBox, str string) *TermBox
- func (this *Prompt) WriteRune(box *TermBox, ch rune) *TermBox
- type Status
- type Teletype
- func (this *Teletype) Clear()
- func (this *Teletype) NewLine()
- func (this *Teletype) NextSpeed()
- func (this *Teletype) QueueWord(word string)
- func (this *Teletype) RemainingLines() int
- func (this *Teletype) Resize(x, y, left, top, width, height int)
- func (this *Teletype) TypeNextRune(box *TermBox) (ret rune, okay bool)
- type TermBox
- func (this *TermBox) CheckEvent() (ret termbox.Event, okay bool)
- func (this *TermBox) Clear() *TermBox
- func (this *TermBox) Close()
- func (this *TermBox) Flush()
- func (this *TermBox) Init() *TermBox
- func (this *TermBox) PollEvent() (evt termbox.Event)
- func (this *TermBox) SetCell(x, y int, ch rune)
- func (this *TermBox) SetCursor(x, y int)
- func (this *TermBox) SetPen(pen Pen) (ret Pen)
- func (this *TermBox) Size() (width int, height int)
- type TermChannel
Constants ¶
const ( LineRune = '\n' SpaceRune = ' ' )
Useful runes.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CursorOutput ¶
type CursorOutput struct { CursorWidth // contains filtered or unexported fields }
Reflow, hard line break helper.
func NewCursorOutput ¶
func NewCursorOutput(width int) CursorOutput
Return all lines as an array of lines.
func (*CursorOutput) AddLine ¶
func (this *CursorOutput) AddLine()
Finish the pending line, and start a new line.
func (*CursorOutput) AddRune ¶
func (this *CursorOutput) AddRune(ch rune)
Write a char, possibly leaving the cursor at the start of a new, empty line.
func (*CursorOutput) AddSpace ¶
func (this *CursorOutput) AddSpace()
Inject a space between two words
func (*CursorOutput) AddWord ¶
func (this *CursorOutput) AddWord(word string)
Write a single word, breaking on lines as necessary.
func (*CursorOutput) AddWords ¶
func (this *CursorOutput) AddWords(words []string)
Write space separated words, breaking on lines as necessary.
func (*CursorOutput) Flush ¶
func (this *CursorOutput) Flush() []string
Return all lines as an array of lines.
type CursorRegion ¶
type CursorRegion struct {
// contains filtered or unexported fields
}
Helper to write left-to-right, bottom-to-top within a specified bounds.
func (*CursorRegion) NewLine ¶
func (this *CursorRegion) NewLine()
Move to the start of the next line.
func (*CursorRegion) Reset ¶
func (this *CursorRegion) Reset(x, y, left, top, width, height int)
Define a new valid region for a cursor passing starting x, starting y, and the region bounds.
type CursorWidth ¶
type CursorWidth struct {
// contains filtered or unexported fields
}
Tracks the screen position of where text will next output.
func (CursorWidth) IsStart ¶
func (this CursorWidth) IsStart() bool
Is the cursor at the start of its range?
func (CursorWidth) IsStepInLimits ¶
func (this CursorWidth) IsStepInLimits(step int) bool
If the cursor were advanced by the passed number of steps, would the output position be within the current bounds?
func (CursorWidth) IsValid ¶
func (this CursorWidth) IsValid() bool
Number of valid positions remaining in the range.
func (CursorWidth) RemainingSteps ¶
func (this CursorWidth) RemainingSteps() int
Number of valid positions remaining in the range.
func (*CursorWidth) ResetCursor ¶
func (this *CursorWidth) ResetCursor()
Move the cursor back to the start of its range.
func (*CursorWidth) StepCursor ¶
func (this *CursorWidth) StepCursor(step int) bool
Move the cursor, return true if the new position IsValid()
type Hardlines ¶
type Hardlines struct {
// contains filtered or unexported fields
}
Track displayed words and any explicit newlines. The interface works character by character to support teletyping.
func (*Hardlines) AppendRune ¶
Append a rune to the current word.
func (*Hardlines) AppendWord ¶
Add a new word to the current line. Flushes any pending word.
func (*Hardlines) IsCurrentLineEmpy ¶
`true` if there are no pending words, and nothing has been written to the current line.
func (*Hardlines) NumLines ¶
Total number of explict lines, including the current line ( if non empty ).
func (Hardlines) Reflow ¶
Return lines with a max length of width, broken at explict new lines favoring word boundries.
func (*Hardlines) StartNewLine ¶
func (this *Hardlines) StartNewLine()
Terminate the current line, and start a new one. Flushes any pending word.
func (*Hardlines) StartNewWord ¶
func (this *Hardlines) StartNewWord()
Begin a new word, flushes any pending word.
type History ¶
type History struct {
// contains filtered or unexported fields
}
Keep a bunch of strings (eg. a user command buffer.)
func NewHistory ¶
Create a new history buffer with the passed capacity.
func (*History) Add ¶
func (hs *History) Add(s string, mark HistoryMarker) HistoryMarker
Add a new item to the current point in history. Takes an optional point in time which is first Restore()d Most people want to create a new most recent item, not overwrite something back in time.
func (*History) Back ¶
Move backwards in time, returning the most recent item, then the next most recent, and so on. Returns `true` if there was any history to return.
func (*History) Forward ¶
Move forwards in time, returning the item one step closer to the most recent item. Returns `true` if not already at the most recent item.
func (*History) Mark ¶
func (hs *History) Mark() HistoryMarker
Remember the current point in time. See: Restore()
func (*History) Restore ¶
func (hs *History) Restore(mark HistoryMarker)
Restore a previously remembered point in time.
type InputHandler ¶
type InputHandler struct {
// contains filtered or unexported fields
}
Helper to processes input events to various useful ends.
func NewInputHandler ¶
func NewInputHandler( input *Prompt, box *TermBox, teletype *Teletype, history *History, ) *InputHandler
Every unqiue user entry needs its own unique InputHandler().
func (*InputHandler) HandleTermEvent ¶
func (this *InputHandler) HandleTermEvent(evt termbox.Event, ) (userInput string, refresh bool, )
Process a single terminal event: read user input, add characters to the prompt, cycle through history. Returns the user's command if they typed text and pressed enter; empty string otherwise. Returns `true` if the terminal screen needs a refresh ( ex. a resize event was detected. )
func (*InputHandler) RestoreHistory ¶
func (this *InputHandler) RestoreHistory()
Return history to most recent moment in time. ( In case, via user input, the user cycled through old commands. )
type MiniCon ¶
type MiniCon struct { Status Status // status line at the top of the window // contains filtered or unexported fields }
A simple text window with status line and user input prompt.
func NewMiniCon ¶
func NewMiniCon() *MiniCon
NOTE: Users should call Close() on the console when they are done
func (*MiniCon) ClearScreen ¶
func (this *MiniCon) ClearScreen()
Clear all displayed and printed text; but not status and not history.
func (*MiniCon) Print ¶
func (this *MiniCon) Print(args ...interface{})
Add the passed arguements as space separated words as pending output. NOTE: the output is not displayed until Update()
func (*MiniCon) Println ¶
func (this *MiniCon) Println(args ...interface{})
Same as Print() with a new line appended.
func (*MiniCon) RefreshDisplay ¶
Redraw the screen, reflowing all text to accomdate the window's current dimensions.
type Pen ¶
type Pen struct {
// contains filtered or unexported fields
}
Current foreground and background colors
type PendingWords ¶
type PendingWords struct {
// contains filtered or unexported fields
}
A queue of pending words to display on the MiniCon WARNING: collapses multiple spaces and embedded newlines into single spaces; and, the hardlines expects that behavior.
type PrintSpeed ¶
type PrintSpeed int
Preset delays between characters displayed by the Teletype.
const ( NormalPrinting PrintSpeed = iota QuickPrinting SkipPrinting )
func (PrintSpeed) Duration ¶
func (this PrintSpeed) Duration() time.Duration
Duration between letters that are printed by the teletype.
type Prompt ¶
type Prompt struct {
// contains filtered or unexported fields
}
User command managment.
func (*Prompt) DeleteRune ¶
Remove the most recent rune from the user's input.
func (*Prompt) RefreshCursor ¶
Ensure the termbox's cursor is on the prompt line at the proper point.
func (*Prompt) RefreshPrompt ¶
Repaint the prompt, input string, and cursor.
type Teletype ¶
type Teletype struct { PrintSpeed PrintSpeed // contains filtered or unexported fields }
Prints words one character at a time while word wrapping to the current region.
func (*Teletype) RemainingLines ¶
Return the amount of vertical space remaining.
type TermBox ¶
type TermBox struct {
// contains filtered or unexported fields
}
Helper to make termbox a little more object oriented.
func NewTermBox ¶
func NewTermBox() (this *TermBox)
After creating a new term box object, the caller must call Init().
func (*TermBox) CheckEvent ¶
Check for the next event without blocking.
func (*TermBox) Close ¶
func (this *TermBox) Close()
Kill the terminal are return control of the screen to the OS.
func (*TermBox) Flush ¶
func (this *TermBox) Flush()
Flush any SetCell() changes to the screen so the user can see those changes.
func (*TermBox) Init ¶
Initialize the display. Users should eventually call Close() to return control of the screen to the OS.
func (*TermBox) PollEvent ¶
func (this *TermBox) PollEvent() (evt termbox.Event)
Blocking waiting for a new terminal event.
type TermChannel ¶
type TermChannel chan termbox.Event
Helper for communicating with termbox in a non blocking way.
func (TermChannel) SendEvents ¶
func (this TermChannel) SendEvents(box *TermBox)
Pushes all events, one at a time, into the passed channel. Closes the channel and returns only if termbox.Interrupt() was called.