termbox

package module
v0.0.0-...-e3b6b9b Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2013 License: MIT Imports: 13 Imported by: 0

README

Termbox

Termbox is a library that provides a minimalistic API which allows the programmer to write text-based user interfaces. The library is crossplatform and has both terminal-based implementations on *nix operating systems and a winapi console based implementation for windows operating systems. The basic idea is an abstraction of the greatest common subset of features available on all major terminals and other terminal-like APIs in a minimalistic fashion. Small API means it is easy to implement, test, maintain and learn it, that's what makes the termbox a distinct library in its area.

Installation

Install and update this go package with go get -u github.com/mbsulliv/termbox-go

Examples

For examples of what can be done take a look at demos in the _demos directory. You can try them with go run: go run _demos/keyboard.go

There are also some interesting projects using termbox-go:

  • godit is an emacsish lightweight text editor written using termbox.
  • gomatrix connects to The Matrix and displays it's data streams in your terminal.

API reference

godoc.org/github.com/mbsulliv/termbox-go

Documentation

Overview

termbox is a library for creating cross-platform text-based interfaces

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Blit

func Blit(x, y, width int, cells []Cell)

Blit copies a 'rectangle' of termbox cells into the termbox buffer at the appropriate location. If the supplied rectangle exceeds backbuffer boundaries, the operation is silently ignored in its entirety.

func Clear

func Clear(fg, bg Attribute) error

Clears the internal back buffer.

func Close

func Close()

Finalizes termbox library, should be called after successful initialization when termbox's functionality isn't required anymore.

func Flush

func Flush() error

Synchronizes the internal back buffer with the terminal.

func HideCursor

func HideCursor()

The shortcut for SetCursor(-1, -1).

func Init

func Init() error

Initializes termbox library. This function should be called before any other functions. After successful initialization, the library must be finalized using 'Close' function.

Example usage:

err := termbox.Init()
if err != nil {
        panic(err)
}
defer termbox.Close()

func PollEvent

func PollEvent(event *Event)

Wait for an event and return it. This is a blocking function call.

func SetCell

func SetCell(x, y int, ch rune, fg, bg Attribute)

Changes cell's parameters in the internal back buffer at the specified position.

func SetCursor

func SetCursor(x, y int)

Sets the position of the cursor. See also HideCursor().

func Size

func Size() (int, int)

Returns the size of the internal back buffer (which is the same as terminal's window size in characters).

Types

type Attribute

type Attribute uint32
const (
	ColorDefault Attribute = iota
	ColorBlack             // 1 is subtracted on use, so really 0
	ColorRed
	ColorGreen
	ColorYellow
	ColorBlue
	ColorMagenta
	ColorCyan
	ColorLightGray
	ColorDarkGray
	ColorLightRed
	ColorLightGreen
	ColorLightYellow
	ColorLightBlue
	ColorLightMagenta
	ColorLightCyan
	ColorWhite
)

Cell attributes, it is possible to use multiple attributes by combining them using bitwise OR ('|'). Although, colors cannot be combined. But you can combine attributes and a single color.

See <http://misc.flogisoft.com/bash/tip_colors_and_formatting> for a pretty good resource on color codes.

const (
	ColorGray100 Attribute = (iota + 232) // 1 is subtracted on use, so really 231
	ColorGray3
	ColorGray7
	ColorGray11
	ColorGray15
	ColorGray19
	ColorGray23
	ColorGray27
	ColorGray30
	ColorGray35
	ColorGray39
	ColorGray42
	ColorGray46
	ColorGray50
	ColorGray54
	ColorGray58
	ColorGray62
	ColorGray66
	ColorGray70
	ColorGray74
	ColorGray78
	ColorGray82
	ColorGray85
	ColorGray89
	ColorGray93
)
const (
	AttrBold Attribute = 1 << (iota + 8)
	AttrUnderline
	AttrReverse
	AttrBlink
)

type Cell

type Cell struct {
	Ch rune
	Fg Attribute
	Bg Attribute
}

A cell, single conceptual entity on the screen. The screen is basically a 2d array of cells. 'Ch' is a unicode character, 'Fg' and 'Bg' are foreground and background attributes respectively.

func CellBuffer

func CellBuffer() []Cell

Returns a slice into the termbox's back buffer. You can get its dimensions using 'Size' function. The slice remains valid as long as no 'Clear' or 'Flush' function calls were made after call to this function.

type Event

type Event struct {
	Type   EventType // one of Event* constants
	Mod    Modifier  // one of Mod* constants or 0
	Key    Key       // one of Key* constants, invalid if 'Ch' is not 0
	Ch     rune      // a unicode character
	Width  int       // width of the screen
	Height int       // height of the screen
	Err    error     // error in case if input failed
}

This type represents a termbox event. The 'Mod', 'Key' and 'Ch' fields are valid if 'Type' is EventKey. The 'Width' and 'Height' fields are valid if 'Type' is EventResize. The 'Err' field is valid if 'Type' is EventError.

type EventType

type EventType uint8
const (
	EventKey EventType = iota
	EventResize
	EventError
)

Event type. See Event.Type field.

type InputMode

type InputMode int
const (
	InputCurrent InputMode = iota
	InputEsc
	InputAlt
)

Input mode. See SetInputMode function.

func SetInputMode

func SetInputMode(mode InputMode) InputMode

Sets termbox input mode. Termbox has two input modes:

1. Esc input mode. When ESC sequence is in the buffer and it doesn't match any known sequence. ESC means KeyEsc.

2. Alt input mode. When ESC sequence is in the buffer and it doesn't match any known sequence. ESC enables ModAlt modifier for the next keyboard event.

If 'mode' is InputCurrent, returns the current input mode. See also Input* constants.

type Key

type Key uint16
const (
	KeyF1 Key = 0xFFFF - iota
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyInsert
	KeyDelete
	KeyHome
	KeyEnd
	KeyPgup
	KeyPgdn
	KeyArrowUp
	KeyArrowDown
	KeyArrowLeft
	KeyArrowRight
)

Key constants, see Event.Key field.

const (
	KeyCtrlTilde      Key = 0x00
	KeyCtrl2          Key = 0x00
	KeyCtrlSpace      Key = 0x00
	KeyCtrlA          Key = 0x01
	KeyCtrlB          Key = 0x02
	KeyCtrlC          Key = 0x03
	KeyCtrlD          Key = 0x04
	KeyCtrlE          Key = 0x05
	KeyCtrlF          Key = 0x06
	KeyCtrlG          Key = 0x07
	KeyBackspace      Key = 0x08
	KeyCtrlH          Key = 0x08
	KeyTab            Key = 0x09
	KeyCtrlI          Key = 0x09
	KeyCtrlJ          Key = 0x0A
	KeyCtrlK          Key = 0x0B
	KeyCtrlL          Key = 0x0C
	KeyEnter          Key = 0x0D
	KeyCtrlM          Key = 0x0D
	KeyCtrlN          Key = 0x0E
	KeyCtrlO          Key = 0x0F
	KeyCtrlP          Key = 0x10
	KeyCtrlQ          Key = 0x11
	KeyCtrlR          Key = 0x12
	KeyCtrlS          Key = 0x13
	KeyCtrlT          Key = 0x14
	KeyCtrlU          Key = 0x15
	KeyCtrlV          Key = 0x16
	KeyCtrlW          Key = 0x17
	KeyCtrlX          Key = 0x18
	KeyCtrlY          Key = 0x19
	KeyCtrlZ          Key = 0x1A
	KeyEsc            Key = 0x1B
	KeyCtrlLsqBracket Key = 0x1B
	KeyCtrl3          Key = 0x1B
	KeyCtrl4          Key = 0x1C
	KeyCtrlBackslash  Key = 0x1C
	KeyCtrl5          Key = 0x1D
	KeyCtrlRsqBracket Key = 0x1D
	KeyCtrl6          Key = 0x1E
	KeyCtrl7          Key = 0x1F
	KeyCtrlSlash      Key = 0x1F
	KeyCtrlUnderscore Key = 0x1F
	KeySpace          Key = 0x20
	KeyBackspace2     Key = 0x7F
	KeyCtrl8          Key = 0x7F
)

type Modifier

type Modifier uint8
const (
	ModAlt Modifier = 0x01
)

Alt modifier constant, see Event.Mod field and SetInputMode function.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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