mattak's blog

人生を1ミリ進める

termbox-go 試してみた

termbox-goとは?

nsf/termbox-go · GitHub

golangからterminalのAPIを叩けるlibrary. pecoの内部でも利用している。

基本

importする

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

初期化 & エラー処理

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

終了処理

    defer termbox.Close()

WindowやKey入力のEvent Wait

for {
    switch ev := termbox.PollEvent(); ev.Type {
    case termbox.EventKey:
        switch ev.Key {
        case termbox.KeyEsc:
            return
        }
    default:
                    draw()
    }
}

全体

Show current time using termbox-go

...

f:id:mattaclj:20150104003239g:plain