『Golang』跨平台TUI(基於文字的用戶界面)庫Terbox-Go文檔翻譯


原文

package termbox

import "github.com/nsf/termbox-go"

termbox-go 是一個用於創建跨平台TUI(基於文本的用戶界面)的庫。

索引

包文件

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

返回一個slicetermbox的后台緩存。你可以使用Size方法來獲取后台緩存的大小。如果調用當前方法后,沒有使用ClearFlush方法清理緩存,后台緩存的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)

返回內部緩存的大小(幾乎與終端窗口尺寸同樣大小)。但是當終端的大小被改變后,它並不總是與終端窗口的大小一致,內部后台緩存僅僅在ClearFlush方法調用后才會獲得同步。

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字符,FgBg是前景和背景屬性。

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事件。ModKey以及Ch字段是對Type是否是一個鍵值事件的驗證。WidthHeight字段是對於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有兩種輸入模式:

  1. Esc輸入模式。當ESC在緩沖序列當中,並且它與任何已知的序列匹配。ESC表示KeyEsc(ESC鍵值)。此為默認的輸入模式。
  2. Alt輸入模式。當ESC在緩沖序列當中,並且它與任何已知的序列匹配。ESC為下一個鍵盤事件啟用ModAlt修改器。

這兩個模式都可以與Mouse模式混用。設置Mouse模式將啟用鼠標點擊事件。

如果modeInputCurrent,返回當前的輸入模式。詳見輸入模式與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有四種輸出選項:

  1. OutputNormal => [1..8]
此模式提供8個不同的顏色:
    黑,紅,綠,黃,藍,品紅,藍綠色,白
快捷方式:ColorBlack,ColorRec,……
屬性:AttrBold,AttrUnderline,AttrReverse

示例:
    SetCell(x, y, '@', ColorBlack | AttrBold, ColorRed);
  1. Output256 => [1..256]
此模式你可以使用256色的終端模式:
0x00 - 0x07: 與OutputNormal一致的8個顏色
0x08 - 0x0f: Color* 或 AttrBold
0x10 - 0xe7: 216種不同的顏色
0xe8 - 0xff: 24種灰度

示例:
    SetCell(x, y, '@', 184, 240);
    SetCell(x, y, '@', 0xb8, 0xf0);
  1. Output216 => [1..216]

此種模式僅僅支持256色模式的第三種情況。但是你不需要提供偏移。

  1. OutputGrayscale => [1..24]

這個模式僅僅支持256色模式的第四種情況。但是你不需要提供偏移。在所有模式中0表示默認的顏色。

使用go run _demos/output.go查看它在你終端的響應。

如果modeOutputCurrent它返回當前的輸出模式。

需要注意的是,這將會返回一個不同的OutputMode超過一個請求,當請求模式也許在目標平台上不可用時。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM