minicon

package
v0.0.0-...-9034458 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2016 License: MIT Imports: 6 Imported by: 0

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

View Source
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.

func (*CursorRegion) Write

func (this *CursorRegion) Write(box *TermBox, ch rune) bool

Advance the space of one rune. Returns the previous position of the cursor, and 'true' if the cursor was left in a valid state.

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

func (this *Hardlines) AppendRune(ch rune)

Append a rune to the current word.

func (*Hardlines) AppendWord

func (this *Hardlines) AppendWord(w string)

Add a new word to the current line. Flushes any pending word.

func (*Hardlines) IsCurrentLineEmpy

func (this *Hardlines) IsCurrentLineEmpy() bool

`true` if there are no pending words, and nothing has been written to the current line.

func (*Hardlines) NumLines

func (this *Hardlines) NumLines() int

Total number of explict lines, including the current line ( if non empty ).

func (Hardlines) Reflow

func (this Hardlines) Reflow(width int) []string

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

func NewHistory(capacity int) *History

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

func (hs *History) Back() (string, bool)

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

func (hs *History) Forward() (string, bool)

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 HistoryMarker

type HistoryMarker *ring.Ring

Records a stich 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) Close

func (this *MiniCon) Close()

Restore the user's screen and free resources

func (*MiniCon) Flush

func (this *MiniCon) Flush()

Flush any pending teletype text to the screen

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

func (this *MiniCon) RefreshDisplay() *TermBox

Redraw the screen, reflowing all text to accomdate the window's current dimensions.

func (*MiniCon) Update

func (this *MiniCon) Update() (ret string)

Display pending output and read input from the user. Blocks until the user has entered some text and pressed return.

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.

func (PrintSpeed) NextSpeed

func (this PrintSpeed) NextSpeed() (ret PrintSpeed)

More faster.

func (PrintSpeed) Sleep

func (this PrintSpeed) Sleep()

Block for the Duration().

type Prompt

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

User command managment.

func NewPrompt

func NewPrompt() *Prompt

Each MiniCon has its own prompt.

func (*Prompt) Clear

func (this *Prompt) Clear() string

Clear and return the user's previous input as a string.

func (*Prompt) DeleteRune

func (this *Prompt) DeleteRune(box *TermBox) *TermBox

Remove the most recent rune from the user's input.

func (*Prompt) RefreshCursor

func (this *Prompt) RefreshCursor(box *TermBox) *TermBox

Ensure the termbox's cursor is on the prompt line at the proper point.

func (*Prompt) RefreshPrompt

func (this *Prompt) RefreshPrompt(box *TermBox) *TermBox

Repaint the prompt, input string, and cursor.

func (*Prompt) SetInput

func (this *Prompt) SetInput(box *TermBox, str string) *TermBox

Set the display input to the passed string.

func (*Prompt) WriteRune

func (this *Prompt) WriteRune(box *TermBox, ch rune) *TermBox

Add a rune to the user's input.

type Status

type Status struct {
	Left, Right string
	Pen         Pen
}

Status text

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) Clear

func (this *Teletype) Clear()

Abandon any pending characters.

func (*Teletype) NewLine

func (this *Teletype) NewLine()

Advance to the next line.

func (*Teletype) NextSpeed

func (this *Teletype) NextSpeed()

Advance to the next PrintSpeed.

func (*Teletype) QueueWord

func (this *Teletype) QueueWord(word string)

Queues a new word for printing.

func (*Teletype) RemainingLines

func (this *Teletype) RemainingLines() int

Return the amount of vertical space remaining.

func (*Teletype) Resize

func (this *Teletype) Resize(x, y, left, top, width, height int)

func (*Teletype) TypeNextRune

func (this *Teletype) TypeNextRune(box *TermBox) (ret rune, okay bool)

Prints a single rune from the most recently QueuedWord to the display. Returns the rune printed, and true if the rune there was a rune to print.

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

func (this *TermBox) CheckEvent() (ret termbox.Event, okay bool)

Check for the next event without blocking.

func (*TermBox) Clear

func (this *TermBox) Clear() *TermBox

Clear the screen, hide the cursor.

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

func (this *TermBox) Init() *TermBox

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.

func (*TermBox) SetCell

func (this *TermBox) SetCell(x, y int, ch rune)

Write a rune at the passed x,y coordinates using the current draw pen

func (*TermBox) SetCursor

func (this *TermBox) SetCursor(x, y int)

Place the termbox cursor at the passed x,y coordinates.

func (*TermBox) SetPen

func (this *TermBox) SetPen(pen Pen) (ret Pen)

Set the current draw pen, return the previous draw pen.

func (*TermBox) Size

func (this *TermBox) Size() (width int, height int)

Return the width and height of the terminal screen.

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.

Jump to

Keyboard shortcuts

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