這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
原文
package termbox
import "github.com/nsf/termbox-go"
termbox-go 是一個用於建立跨平台TUI(基於文本的使用者介面)的庫。
索引
- Variables
- func CellBuffer() []Cell
- func Clear(fg, bg Attribute) error
- func Close()
- func Flush() error
- func HideCursor()
- func Init() error
- func Interrupt()
- func SetCell(x, y int, ch rune, fg, bg Attribute)
- func SetCursor(x, y int)
- func Size() (int, int)
- func Sync() error
- type Attribute
- type Cell
- type Event
-
func PollEvent() Event
-
type InputMode
- func SetInputMode(mode InputMode) InputMode
- type Key
- type Modifier
-
type OutputMode
- func SetOutputMode(mode OutputMode) OutputMode
包檔案
api.go
api_common.go
syscalls_linux.go
termbox.go
termbox_common.go
terminfo.go
terminfo_builtin.go
變數
var ( IsInit bool = false)
查看termbox是否已經被初始化。
func CellBuffer
func CellBuffer() []Cell
返回一個slice到termbox的後台緩衝。你可以使用Size方法來擷取後台緩衝的大小。如果調用當前方法後,沒有使用Clear或Flush方法清理緩衝,後台緩衝的Slice將會一直存在。
func Clear
func Clear(fg, bg Attribute) error
清理內部後台緩衝。
func Close
func Close()
當termbox已經被成功初始化且termbox的方法不再被需要的時候,調用這個方法來終止termbox庫。
func Flush
func Flush() error
與終端同步內部後台緩衝。
func HideCursor
func HideCursor()
設定SetCursor(-1,-1)的快速鍵。
func Init
func Init() error
初始化termbox庫。這個方法需要在其他方法之前被調用。在成功過初始化後,庫必須使用Close方法結束。
樣本:
err := termbox.Init()if err != nil { panic(err)}defer termbox.Close()
func Interrupt
func Interrupt()
通過返回一個EventInterrupt來終止一個進行中中的到PollEvent的調用。需要注意的是,這個方法在PollEvent方法被成功中斷前,將會被阻塞。
func SetCell
func SetCell(x, y int, ch rune, fg, bg Attribute)
在指定的位置改變內部後台緩衝中儲存格的參數。
func SetCursor
func SetCursor(x, y int)
設定游標的位置。參見HideCursor()
func Size
func Size() (int, int)
返回內部緩衝的大小(幾乎與終端視窗尺寸同樣大小)。但是當終端的大小被改變後,它並不總是與終端視窗的大小一致,內部後台緩衝僅僅在Clear或Flush方法調用後才會獲得同步。
func Sync
func Sync() error
當有事務引起termbox對於終端緩衝和實際情況的解析不同步時,立即同步。
type Attribute
type Attribute uint16
const ( ColorDefault Attribute = iota ColorBlack ColorRed ColorGreen ColorYellow ColorBlue ColorMagenta ColorCyan ColorWhite)
儲存格顏色,你可以通過使用bitwise或|混合多個屬性。
const ( AttrBold Attribute = 1 << (iota + 9) AttrUnderline AttrReverse)
儲存格屬性,通過使用bitwise或|來混合多個屬性。雖然顏色不能被混合,但是你可以混合多個屬性和一個獨立的顏色。
值得一提的是,一些平台不支援某些的屬性。例如Windows Console不支援底線屬性。在一些終端上,應用AttrBold到背景,可能會引起文字的閃爍。小心的使用他們,並且在不同的終端上測試你的代碼。
type Cell
type Cell struct { Ch rune Fg Attribute Bg Attribute}
一個儲存格,在螢幕上的獨立概念實體。螢幕是基於儲存格的一個2d數組。Ch是一個unicode字元,Fg和Bg是前景和背景屬性。
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 MouseX int // x coord of mouse MouseY int // y coord of mouse}
此類型描述一個termbox事件。Mod、Key以及Ch欄位是對Type是否是一個索引值事件的驗證。Width和Height欄位是對於Type是否是重設重設的驗證。Err欄位是對於Type是否是錯誤事件的驗證。
func PollEvent
func PollEvent() Event
等待一個事件,並返回它。這事一個阻塞方法調用。
type EventType
type EventType uint8
const ( EventKey EventType = iota EventResize EventMouse EventError EventInterrupt)
指示事件類型,祥見Event.Type欄位。
type InputMode
type InputMode int
const ( InputEsc InputMode = 1 << iota InputAlt InputMouse InputCurrent InputMode = 0)
輸入模式,詳見SetInputMode方法。
func SetInputMode
func SetInputMode(mode InputMode) InputMode
設定termbox輸入模式。Termbox有兩種輸入模式:
Esc輸入模式。當ESC在緩衝序列當中,並且它與任何已知的序列匹配。ESC表示KeyEsc(ESC索引值)。此為預設的輸入模式。
Alt輸入模式。當ESC在緩衝序列當中,並且它與任何已知的序列匹配。ESC為下一個鍵盤事件啟用ModAlt修改器。
這兩個模式都可以與Mouse模式混用。設定Mouse模式將啟用滑鼠點擊事件。
如果mode是InputCurrent,返回當前的輸入模式。詳見輸入模式與Input*常量。
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 MouseLeft MouseMiddle MouseRight)
索引值常量,詳見Event.Key欄位。
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修改常量,祥見Event.Mod欄位與SetInputMode方法。
type OutputMode
type OutputMode int
const ( OutputCurrent OutputMode = iota OutputNormal Output256 Output216 OutputGrayscale)
輸出模式。詳見SetOutputMode方法。
func SetOutputMode
func SetOutputMode(mode OutputMode) OutputMode
設定termbox輸出模式。Termbox有四種輸出選項:
- OutputNormal => [1..8]
此模式提供8個不同的顏色: 黑,紅,綠,黃,藍,品紅,藍綠色,白捷徑:ColorBlack,ColorRec,……屬性:AttrBold,AttrUnderline,AttrReverse樣本: SetCell(x, y, '@', ColorBlack | AttrBold, ColorRed);
- Output256 => [1..256]
此模式你可以使用256色的終端模式:0x00 - 0x07: 與OutputNormal一致的8個顏色0x08 - 0x0f: Color* 或 AttrBold0x10 - 0xe7: 216種不同的顏色0xe8 - 0xff: 24種灰階樣本: SetCell(x, y, '@', 184, 240); SetCell(x, y, '@', 0xb8, 0xf0);
- Output216 => [1..216]
此種模式僅僅支援256色模式的第三種情況。但是你不需要提供位移。
- OutputGrayscale => [1..24]
這個模式僅僅支援256色模式的第四種情況。但是你不需要提供位移。在所有模式中0表示預設的顏色。
使用go run _demos/output.go查看它在你終端的響應。
如果mode是OutputCurrent它返回當前的輸出模式。
需要注意的是,這將會返回一個不同的OutputMode超過一個請求,當請求模式也許在目標平台上不可用時。