Add widgets demos

Fix trim bug in helper.go
Add todos
This commit is contained in:
gizak 2015-03-13 13:20:17 -04:00
parent dd3b43990c
commit d6ab2b3d3c
16 changed files with 333 additions and 7 deletions

View File

@ -72,11 +72,47 @@ The `helloworld` color scheme drops in some colors!
## Widgets
_APIs are subject to change, docs will be added after 1 or 2 commits_
#### Par
[Demo code](https://github.com/gizak/termui/blob/master/example/par.go)
<img src="./example/par.png" alt="par" type="image/png" width="300">
#### List
[demo code](https://github.com/gizak/termui/blob/master/example/list.go)
<img src="./example/list.png" alt="list" type="image/png" width="200">
#### Gauge
[Demo code](https://github.com/gizak/termui/blob/master/example/gauge.go)
<img src="./example/gauge.png" alt="gauge" type="image/png" width="300">
#### Line Chart
[Demo code](https://github.com/gizak/termui/blob/master/example/linechart.go)
<img src="./example/linechart.png" alt="linechart" type="image/png" width="400">
#### Bar Chart
[Demo code](https://github.com/gizak/termui/blob/master/example/barchart.go)
<img src="./example/barchart.png" alt="barchart" type="image/png" width="120">
#### Sparklines
[Demo code](https://github.com/gizak/termui/blob/master/example/sparklines.go)
<img src="./example/sparklines.png" alt="sparklines" type="image/png" width="300">
## GoDoc
[godoc](https://godoc.org/github.com/gizak/termui).
## TODO
[1] Float layout
[2] Event system
## License
This library is under the [MIT License](http://opensource.org/licenses/MIT)

30
example/barchart.go Normal file
View File

@ -0,0 +1,30 @@
package main
import "github.com/gizak/termui"
import "github.com/nsf/termbox-go"
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.UseTheme("helloworld")
bc := termui.NewBarChart()
data := []int{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
bc.Border.Label = "Bar Chart"
bc.Data = data
bc.Width = 26
bc.Height = 10
bc.DataLabels = bclabels
bc.TextColor = termui.ColorGreen
bc.BarColor = termui.ColorRed
bc.NumColor = termui.ColorYellow
termui.Render(bc)
termbox.PollEvent()
}

BIN
example/barchart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

48
example/gauge.go Normal file
View File

@ -0,0 +1,48 @@
package main
import "github.com/gizak/termui"
import "github.com/nsf/termbox-go"
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.UseTheme("helloworld")
g0 := termui.NewGauge()
g0.Percent = 40
g0.Width = 50
g0.Height = 3
g0.Border.Label = "Slim Gauge"
g0.BarColor = termui.ColorRed
g0.Border.FgColor = termui.ColorWhite
g0.Border.LabelFgColor = termui.ColorCyan
g2 := termui.NewGauge()
g2.Percent = 60
g2.Width = 50
g2.Height = 3
g2.PercentColor = termui.ColorBlue
g2.Y = 3
g2.Border.Label = "Slim Gauge"
g2.BarColor = termui.ColorYellow
g2.Border.FgColor = termui.ColorWhite
g1 := termui.NewGauge()
g1.Percent = 30
g1.Width = 50
g1.Height = 5
g1.Y = 6
g1.Border.Label = "Big Gauge"
g1.PercentColor = termui.ColorYellow
g1.BarColor = termui.ColorGreen
g1.Border.FgColor = termui.ColorWhite
g1.Border.LabelFgColor = termui.ColorMagenta
termui.Render(g0, g1, g2)
termbox.PollEvent()
}

BIN
example/gauge.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

63
example/linechart.go Normal file
View File

@ -0,0 +1,63 @@
package main
import (
"math"
"github.com/gizak/termui"
)
import "github.com/nsf/termbox-go"
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.UseTheme("helloworld")
sinps := (func() []float64 {
n := 220
ps := make([]float64, n)
for i := range ps {
ps[i] = 1 + math.Sin(float64(i)/5)
}
return ps
})()
lc0 := termui.NewLineChart()
lc0.Border.Label = "braille-mode Line Chart"
lc0.Data = sinps
lc0.Width = 50
lc0.Height = 12
lc0.X = 0
lc0.Y = 0
lc0.AxesColor = termui.ColorWhite
lc0.LineColor = termui.ColorGreen | termui.AttrBold
lc1 := termui.NewLineChart()
lc1.Border.Label = "dot-mode Line Chart"
lc1.Mode = "dot"
lc1.Data = sinps
lc1.Width = 26
lc1.Height = 12
lc1.X = 51
lc1.DotStyle = '+'
lc1.AxesColor = termui.ColorWhite
lc1.LineColor = termui.ColorYellow | termui.AttrBold
lc2 := termui.NewLineChart()
lc2.Border.Label = "dot-mode Line Chart"
lc2.Mode = "dot"
lc2.Data = sinps[4:]
lc2.Width = 77
lc2.Height = 16
lc2.X = 0
lc2.Y = 12
lc2.AxesColor = termui.ColorWhite
lc2.LineColor = termui.ColorCyan | termui.AttrBold
termui.Render(lc0, lc1, lc2)
termbox.PollEvent()
}

BIN
example/linechart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

36
example/list.go Normal file
View File

@ -0,0 +1,36 @@
package main
import "github.com/gizak/termui"
import "github.com/nsf/termbox-go"
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.UseTheme("helloworld")
strs := []string{
"[0] github.com/gizak/termui",
"[1] editbox.go",
"[2] iterrupt.go",
"[3] keyboard.go",
"[4] output.go",
"[5] random_out.go",
"[6] dashboard.go",
"[7] nsf/termbox-go"}
ls := termui.NewList()
ls.Items = strs
ls.ItemFgColor = termui.ColorYellow
ls.Border.Label = "List"
ls.Height = 7
ls.Width = 25
ls.Y = 0
termui.Render(ls)
termbox.PollEvent()
}

BIN
example/list.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

43
example/par.go Normal file
View File

@ -0,0 +1,43 @@
package main
import "github.com/gizak/termui"
import "github.com/nsf/termbox-go"
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.UseTheme("helloworld")
par0 := termui.NewPar("Borderless Text")
par0.Height = 1
par0.Width = 20
par0.Y = 1
par0.HasBorder = false
par1 := termui.NewPar("Simple Text")
par1.Height = 3
par1.Width = 17
par1.X = 20
par1.Border.Label = "Label"
par2 := termui.NewPar("Simple text\nwith label. It can be multilined with \\n or break automatically")
par2.Height = 5
par2.Width = 37
par2.Y = 4
par2.Border.Label = "Multiline"
par2.Border.FgColor = termui.ColorYellow
par3 := termui.NewPar("Long text with label and it is auto trimmed.")
par3.Height = 3
par3.Width = 37
par3.Y = 9
par3.Border.Label = "Auto Trim"
termui.Render(par0, par1, par2, par3)
termbox.PollEvent()
}

BIN
example/par.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

60
example/sparklines.go Normal file
View File

@ -0,0 +1,60 @@
package main
import "github.com/gizak/termui"
import "github.com/nsf/termbox-go"
func main() {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
termui.UseTheme("helloworld")
data := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
spl0 := termui.NewSparkline()
spl0.Data = data[3:]
spl0.Title = "Sparkline 0"
spl0.LineColor = termui.ColorGreen
// single
spls0 := termui.NewSparklines(spl0)
spls0.Height = 2
spls0.Width = 20
spls0.HasBorder = false
spl1 := termui.NewSparkline()
spl1.Data = data
spl1.Title = "Sparkline 1"
spl1.LineColor = termui.ColorRed
spl2 := termui.NewSparkline()
spl2.Data = data[5:]
spl2.Title = "Sparkline 2"
spl2.LineColor = termui.ColorMagenta
// group
spls1 := termui.NewSparklines(spl0, spl1, spl2)
spls1.Height = 8
spls1.Width = 20
spls1.Y = 3
spls1.Border.Label = "Group Sparklines"
spl3 := termui.NewSparkline()
spl3.Data = data
spl3.Title = "Enlarged Sparkline"
spl3.Height = 8
spl3.LineColor = termui.ColorYellow
spls2 := termui.NewSparklines(spl3)
spls2.Height = 11
spls2.Width = 30
spls2.Border.FgColor = termui.ColorCyan
spls2.X = 21
spls2.Border.Label = "Tweeked Sparkline"
termui.Render(spls0, spls1, spls2)
termbox.PollEvent()
}

BIN
example/sparklines.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

View File

@ -46,8 +46,8 @@ func trimStr2Runes(s string, w int) []rune {
}
sw := rw.StringWidth(s)
if sw+dotw >= w {
return []rune(rw.Truncate(s, w, ""))
} else {
return []rune(rw.Truncate(s, w, "…"))
} else {
return []rune(rw.Truncate(s, w, ""))
}
}

10
p.go
View File

@ -27,6 +27,15 @@ func (p *Par) Buffer() []Point {
if rs[k] == '\n' {
k++
}
if i >= p.innerHeight {
ps = append(ps, newPointWithAttrs('…',
p.innerX+p.innerWidth-1,
p.innerY+p.innerHeight-1,
p.TextFgColor, p.TextBgColor))
break
}
continue
}
pi := Point{}
@ -38,6 +47,7 @@ func (p *Par) Buffer() []Point {
pi.Fg = p.TextFgColor
ps = append(ps, pi)
k++
j++
}

View File

@ -102,16 +102,16 @@ func (sl *Sparklines) Buffer() []Point {
}
for j, v := range data {
h := int(float32(v) * l.scale)
h := int(float32(v)*l.scale + 0.5)
barCnt := h / 8
barMod := h % 8
for jj := 0; jj < barCnt; jj++ {
p := Point{}
p.X = sl.innerX + j
p.Y = sl.innerY + oftY + l.Height - jj
p.Ch = sparks[7]
p.Fg = l.LineColor
p.Bg = sl.BgColor
p.Ch = ' ' //sparks[7]
p.Bg = l.LineColor
//p.Bg = sl.BgColor
ps = append(ps, p)
}
if barMod != 0 {