aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/gizak/termui/render.go
blob: d697d0aea84d41ccc37185fddde17e9fbfe411b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.

package termui

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

// Bufferer should be implemented by all renderable components.
type Bufferer interface {
    Buffer() []Point
}

// Init initializes termui library. This function should be called before any others.
// After initialization, the library must be finalized by 'Close' function.
func Init() error {
    Body = NewGrid()
    Body.X = 0
    Body.Y = 0
    Body.BgColor = theme.BodyBg
    defer func() {
        w, _ := tm.Size()
        Body.Width = w
        evtListen()
    }()
    return tm.Init()
}

// Close finalizes termui library,
// should be called after successful initialization when termui's functionality isn't required anymore.
func Close() {
    tm.Close()
}

// TermWidth returns the current terminal's width.
func TermWidth() int {
    tm.Sync()
    w, _ := tm.Size()
    return w
}

// TermHeight returns the current terminal's height.
func TermHeight() int {
    tm.Sync()
    _, h := tm.Size()
    return h
}

// Render renders all Bufferer in the given order from left to right,
// right could overlap on left ones.
func Render(rs ...Bufferer) {
    tm.Clear(tm.ColorDefault, toTmAttr(theme.BodyBg))
    for _, r := range rs {
        buf := r.Buffer()
        for _, v := range buf {
            tm.SetCell(v.X, v.Y, v.Ch, toTmAttr(v.Fg), toTmAttr(v.Bg))
        }
    }
    tm.Flush()
}