Add grid layout

This commit is contained in:
gizak 2015-03-15 15:56:38 -04:00
parent a1a3d729d5
commit 8ed1b40acf
6 changed files with 181 additions and 0 deletions

View File

@ -7,6 +7,10 @@ __Demo:__
<img src="./example/screencast.gif" alt="demo" width="600">
__Grid layout:(incomplete)__
<img src="./example/grid.gif" alt="grid" width="400">
## Installation
go get github.com/gizak/termui

View File

@ -77,3 +77,23 @@ func (d *Block) Buffer() []Point {
}
return ps
}
func (d Block) GetHeight() int {
return d.Height
}
func (d Block) GetWidth() int {
return d.Width
}
func (d *Block) SetX(x int) {
d.X = x
}
func (d *Block) SetY(y int) {
d.Y = y
}
func (d *Block) SetWidth(w int) {
d.Width = w
}

BIN
example/grid.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

86
example/grid.go Normal file
View File

@ -0,0 +1,86 @@
package main
import ui "github.com/gizak/termui"
import tm "github.com/nsf/termbox-go"
import "math"
import "time"
func main() {
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
sinps := (func() []float64 {
n := 400
ps := make([]float64, n)
for i := range ps {
ps[i] = 1 + math.Sin(float64(i)/5)
}
return ps
})()
sinpsint := (func() []int {
ps := make([]int, len(sinps))
for i, v := range sinps {
ps[i] = int(100*v + 10)
}
return ps
})()
ui.UseTheme("helloworld")
spark := ui.Sparkline{}
spark.Height = 8
spdata := sinpsint
spark.Data = spdata
spark.LineColor = ui.ColorCyan
spark.TitleColor = ui.ColorWhite
sp := ui.NewSparklines(spark)
sp.Height = 11
sp.Border.Label = "Sparkline"
lc := ui.NewLineChart()
lc.Border.Label = "braille-mode Line Chart"
lc.Data = sinps
lc.Height = 11
lc.AxesColor = ui.ColorWhite
lc.LineColor = ui.ColorYellow | ui.AttrBold
ui.Body.Rows = []ui.Row{
ui.NewRow(
ui.NewCol(sp, 6, 0, true),
ui.NewCol(lc, 6, 0, true))}
draw := func(t int) {
sp.Lines[0].Data = spdata[t:]
lc.Data = sinps[2*t:]
ui.Render(ui.Body)
}
evt := make(chan tm.Event)
go func() {
for {
evt <- tm.PollEvent()
}
}()
i := 0
for {
select {
case e := <-evt:
if e.Type == tm.EventKey && e.Ch == 'q' {
return
}
default:
draw(i)
i++
if i == 102 {
return
}
time.Sleep(time.Second / 2)
}
}
}

67
grid.go Normal file
View File

@ -0,0 +1,67 @@
package termui
import tm "github.com/nsf/termbox-go"
type container struct {
//Height int
//Width int
BgColor Attribute
Rows []Row
}
type Row []Col
type Col struct {
ColumnBufferer
Offset int // 0 ~ 11
Span int // 1 ~ 12
Sticky bool
}
type ColumnBufferer interface {
Bufferer
GetHeight() int
GetWidth() int
SetWidth(int)
SetX(int)
SetY(int)
}
func NewRow(cols ...Col) Row {
return cols
}
func NewCol(block ColumnBufferer, span, offset int, sticky bool) Col {
return Col{ColumnBufferer: block, Span: span, Sticky: sticky, Offset: offset}
}
func (c container) Buffer() []Point {
ps := []Point{}
maxw, _ := tm.Size()
y := 0
for _, row := range c.Rows {
x := 0
maxHeight := 0
for _, col := range row {
if h := col.GetHeight(); h > maxHeight {
maxHeight = h
}
w := int(float64(maxw*(col.Span+col.Offset)) / 12.0)
if col.Sticky || col.GetWidth() > w {
col.SetWidth(w)
}
col.SetY(y)
col.SetX(x)
ps = append(ps, col.Buffer()...)
x += w + int(float64(maxw*col.Offset)/12)
}
y += maxHeight
}
return ps
}
var Body container

View File

@ -8,6 +8,10 @@ type Bufferer interface {
}
func Init() error {
Body = container{
BgColor: theme.BodyBg,
Rows: []Row{},
}
return tm.Init()
}