diff --git a/README.md b/README.md index 8d6c1b6..63ad8af 100644 --- a/README.md +++ b/README.md @@ -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) + +par + +#### List +[demo code](https://github.com/gizak/termui/blob/master/example/list.go) + +list + +#### Gauge +[Demo code](https://github.com/gizak/termui/blob/master/example/gauge.go) + +gauge + +#### Line Chart +[Demo code](https://github.com/gizak/termui/blob/master/example/linechart.go) + +linechart + +#### Bar Chart +[Demo code](https://github.com/gizak/termui/blob/master/example/barchart.go) + +barchart + +#### Sparklines +[Demo code](https://github.com/gizak/termui/blob/master/example/sparklines.go) + +sparklines + ## 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) diff --git a/example/barchart.go b/example/barchart.go new file mode 100644 index 0000000..2e9829b --- /dev/null +++ b/example/barchart.go @@ -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() +} diff --git a/example/barchart.png b/example/barchart.png new file mode 100644 index 0000000..a37912f Binary files /dev/null and b/example/barchart.png differ diff --git a/example/gauge.go b/example/gauge.go new file mode 100644 index 0000000..a4cef73 --- /dev/null +++ b/example/gauge.go @@ -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() +} diff --git a/example/gauge.png b/example/gauge.png new file mode 100644 index 0000000..17eb3ea Binary files /dev/null and b/example/gauge.png differ diff --git a/example/linechart.go b/example/linechart.go new file mode 100644 index 0000000..9796f33 --- /dev/null +++ b/example/linechart.go @@ -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() +} diff --git a/example/linechart.png b/example/linechart.png new file mode 100644 index 0000000..655ef43 Binary files /dev/null and b/example/linechart.png differ diff --git a/example/list.go b/example/list.go new file mode 100644 index 0000000..18d53a5 --- /dev/null +++ b/example/list.go @@ -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() +} diff --git a/example/list.png b/example/list.png new file mode 100644 index 0000000..8ca08c0 Binary files /dev/null and b/example/list.png differ diff --git a/example/par.go b/example/par.go new file mode 100644 index 0000000..a2d0ada --- /dev/null +++ b/example/par.go @@ -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() +} diff --git a/example/par.png b/example/par.png new file mode 100644 index 0000000..a85e644 Binary files /dev/null and b/example/par.png differ diff --git a/example/sparklines.go b/example/sparklines.go new file mode 100644 index 0000000..4ef2e1b --- /dev/null +++ b/example/sparklines.go @@ -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() +} diff --git a/example/sparklines.png b/example/sparklines.png new file mode 100644 index 0000000..9dd7c82 Binary files /dev/null and b/example/sparklines.png differ diff --git a/helper.go b/helper.go index 2952cf9..32551d7 100644 --- a/helper.go +++ b/helper.go @@ -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, "")) } } diff --git a/p.go b/p.go index c5dd936..6264955 100644 --- a/p.go +++ b/p.go @@ -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++ } diff --git a/sparkline.go b/sparkline.go index e8e8a6b..42873fe 100644 --- a/sparkline.go +++ b/sparkline.go @@ -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 {