diff --git a/README.md b/README.md index f3571f0..5f327b4 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ To use `termui`, the very first thing you may want to know is how to manage layo __Absolute layout__ -Each widget has an underlying block structure which basically is a box model. It has border, label and padding properties. A border of a widget can be chosen to hide or display (with its border label), you can pick a different front/back colour for the border as well. To display such a widget at a specific location in terminal window, you need to assign `.X`, `.Y`, `.Height`, `.Width` values for each widget before send it to `.Render`. Let's demonstrate these by a code snippet: +Each widget has an underlying block structure which basically is a box model. It has border, label and padding properties. A border of a widget can be chosen to hide or display (with its border label), you can pick a different front/back colour for the border as well. To display such a widget at a specific location in terminal window, you need to assign `.X`, `.Y`, `.Height`, `.Width` values for each widget before sending it to `.Render`. Let's demonstrate these by a code snippet: `````go import ui "github.com/gizak/termui" // <- ui shortcut, optional @@ -48,7 +48,7 @@ Each widget has an underlying block structure which basically is a box model. It g.Width = 50 g.Height = 3 g.Y = 11 - g.Border.Label = "Gauge" + g.BorderLabel = "Gauge" g.BarColor = ui.ColorRed g.BorderFg = ui.ColorWhite g.BorderLabelFg = ui.ColorCyan @@ -65,7 +65,7 @@ __Grid layout:__ grid -Grid layout uses [12 columns grid system](http://www.w3schools.com/bootstrap/bootstrap_grid_system.asp) with expressive syntax. To use `Grid`, all we need to do is build a widget tree consisting of `Row`s and Cols (Actually a Col is also a `Row` but with a widget endpoint attached). +Grid layout uses [12 columns grid system](http://www.w3schools.com/bootstrap/bootstrap_grid_system.asp) with expressive syntax. To use `Grid`, all we need to do is build a widget tree consisting of `Row`s and `Col`s (Actually a `Col` is also a `Row` but with a widget endpoint attached). ```go import ui "github.com/gizak/termui" @@ -93,22 +93,22 @@ Grid layout uses [12 columns grid system](http://www.w3schools.com/bootstrap/boo ```go // handle key q pressing - termui.Handle("/sys/kbd/q", func(termui.Event) { + ui.Handle("/sys/kbd/q", func(ui.Event) { // press q to quit - termui.StopLoop() + ui.StopLoop() }) - termui.Handle("/sys/kbd/C-x", func(termui.Event) { + ui.Handle("/sys/kbd/C-x", func(ui.Event) { // handle Ctrl + x combination }) - termui.Handle("/sys/kbd", func(termui.Event) { + ui.Handle("/sys/kbd", func(ui.Event) { // handle all other key pressing }) // handle a 1s timer - termui.Handle("/timer/1s", func(e ui.Event) { - t := e.Data.(termui.EvtTimer) + ui.Handle("/timer/1s", func(e ui.Event) { + t := e.Data.(ui.EvtTimer) // t is a EvtTimer if t.Count%2 ==0 { // do something diff --git a/_docs/about.md b/_docs/about.md new file mode 100644 index 0000000..e69de29 diff --git a/_docs/components.md b/_docs/components.md new file mode 100644 index 0000000..4e0239f --- /dev/null +++ b/_docs/components.md @@ -0,0 +1,32 @@ +Overview +--- + +Bufferer +--- + +Block +--- + +BarChart +--- + +Canvas +--- + +Gauge +--- + +LineChart +--- + +MBarChart +--- + +Par +--- + +Sparkline +--- + +Sparklines +--- diff --git a/_docs/events.md b/_docs/events.md new file mode 100644 index 0000000..45aa04c --- /dev/null +++ b/_docs/events.md @@ -0,0 +1,14 @@ +Event System +--- + +Keyboard Events +--- + +Mouse Events +--- + +Window Events +--- + +Custom Events +--- diff --git a/_docs/img/dashboard.gif b/_docs/img/dashboard.gif new file mode 100644 index 0000000..4bab470 Binary files /dev/null and b/_docs/img/dashboard.gif differ diff --git a/_docs/img/demo1.png b/_docs/img/demo1.png new file mode 100644 index 0000000..cc35f49 Binary files /dev/null and b/_docs/img/demo1.png differ diff --git a/_docs/index.md b/_docs/index.md new file mode 100644 index 0000000..2769067 --- /dev/null +++ b/_docs/index.md @@ -0,0 +1,15 @@ +[termui]() is a cross-platform, easy-to-compile, and fully-customizable terminal dashboard. It aims to provide a terminal front end for your applications with less struggle: + +> ![dashboard](img/dashboard.gif) +> +> _cast under osx 10.10; Terminal.app; Menlo Regular 12pt._ + +This guide describes the essential parts used to build a interface, which includes: + +- Installation & Usage +- Layout System +- Event System +- Theming +- Components + +[Quickstart](quickstart.md) is the way to go for starters and [Recipes](recipes.md) contains some practical resolutions you might need. diff --git a/_docs/layouts.md b/_docs/layouts.md new file mode 100644 index 0000000..b260736 --- /dev/null +++ b/_docs/layouts.md @@ -0,0 +1,26 @@ +Overview +--- + +termui offers two layout system: [Absolute]() and [Grid](). The two concept actually spawned from Web: + +- The __Absolute layout__ is a plain coordination system, like [CSS position property](https://developer.mozilla.org/en/docs/Web/CSS/position) `position: absolute`. You will need manually assign `.X`, `.Y`, `.Width` and `.Height` to a component. +- The __Grid system__ actually is a simplified version of [the 12 columns CSS grid system](http://www.w3schools.com/bootstrap/bootstrap_grid_system.asp) on terminal. You do not need to bother setting positions and width properties, these values will be synced up according to their containers. + +!!! note + `Align` property can help you set your component position based on terminal window. Find more at [Magic Variables](#magic-variables) + +__Cons and pros:__ + +- Use of Absolute layout gives you maximum control over how to arrange your components, while you have +to put a little more effort to set things up. Fortunately there are some "magic variables" may help you out. +- Grid layout can save you some time, it adjusts components location and size based on it's container. But note that you do need to set `.Height` property to each components because termui can not decide it for you. + + +Absolute Layout +--- + +Grid Layout +--- + +Magic Variables +--- diff --git a/_docs/quickstart.md b/_docs/quickstart.md new file mode 100644 index 0000000..207019f --- /dev/null +++ b/_docs/quickstart.md @@ -0,0 +1,80 @@ +Installation +--- + +Since [termui](https://github.com/gizak/termui) is a Go lib, we will need a working Go environment to begin with. If you have not set it up, there is a great intro you can follow up: [How to write Go code](https://golang.org/doc/code.html). + +Once you have the environment set up, you can proceed to install termui by the following command: + +`go get github.com/gizak/termui` + +The current version of termui is v2. If you are working with the old version of termui or the new version does not seem right to you, you can always go back to v1 version by: + +`go get gopkg.in/gizak/termui.v1` + +!!! note + v2 has many features implemented which you can not find in v1, such as new event system and asynchronous rendering. To find more about versions difference in section [Versions](versions.md). + + +Usage +--- + +Let's throw an simple example to get our feet wet: + +```go +package main + +import ui "github.com/gizak/termui" // use ui as an alias + +func main() { + err := ui.Init() + if err != nil { + panic(err) + } + defer ui.Close() + + p := ui.NewPar(":PRESS q TO QUIT DEMO") + p.Height = 3 + p.Width = 50 + p.TextFgColor = ui.ColorWhite + p.BorderLabel = "Text Box" + p.BorderFg = ui.ColorCyan + + ui.Render(p) // feel free to call Render, it's async and non-block + + ui.Handle("/sys/kbd/q",func(e ui.Event){ + ui.StopLoop() + }) + + ui.Loop() +} +``` +There are only around 20 lines for the main function. Break this down into 4 parts: + +1. __Init termui__: + `ui.Init()` initializes the termui. From this point, termui will take over your terminal display. + `ui.Close()` closes resources and cleans up your terminal content. Make sure it is called before exit or you will end up with a messed up looking terminal. + +2. __Build your component__: + `ui.NewPar(:PRESS q TO QUIT DEMO)` returns a structure representing a paragraph component. You can assign position, size, text colour, border and many other properties to a component. + +3. __Draw your component on display__: + `ui.Render(p)` renders p onto terminal display. + +4. __Handle events__: + `ui.Handle("/sys/kbd/q", func(e Event))` registers an event handler for event: key q is pressed. + `ui.StopLoop()` exits the event listening loop invoked by `ui.Loop()`. + `ui.Loop()` makes the program stops at here and start listening & handling events. Call + `ui.StopLoop()` to leave the circle. + +The example code gives us: + +> ![example screenshot](img/demo1.png) + +Now you can press q to quit the program. + +After knowing of some basics, next we can discover more about: + +1. how to set component location in [Layouts](layouts.md) +2. how to capture and handle events in [Events](events.md) +3. the different [components](components.md) +4. check out some real world examples in [recipes](recipes.md) diff --git a/_docs/recipes.md b/_docs/recipes.md new file mode 100644 index 0000000..3a16965 --- /dev/null +++ b/_docs/recipes.md @@ -0,0 +1 @@ +_Sorry, it is still Work in Progress..._ diff --git a/_docs/themes.md b/_docs/themes.md new file mode 100644 index 0000000..e69de29 diff --git a/_docs/versions.md b/_docs/versions.md new file mode 100644 index 0000000..e69de29 diff --git a/_example/barchart.go b/_example/barchart.go index 30993d3..6a94091 100644 --- a/_example/barchart.go +++ b/_example/barchart.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/dashboard.go b/_example/dashboard.go index ecf8921..95a23e9 100644 --- a/_example/dashboard.go +++ b/_example/dashboard.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. @@ -126,8 +126,8 @@ func main() { list.Items = strs[t%9:] sp.Lines[0].Data = spdata[:30+t%50] sp.Lines[1].Data = spdata[:35+t%50] - lc.Data = sinps[t/2:] - lc1.Data = sinps[2*t:] + lc.Data = sinps[t/2%220:] + lc1.Data = sinps[2*t%220:] bc.Data = bcdata[t/2%10:] ui.Render(p, list, g, sp, lc, bc, lc1, p1) } diff --git a/_example/gauge.go b/_example/gauge.go index ce2c22f..dcce7b0 100644 --- a/_example/gauge.go +++ b/_example/gauge.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/grid.go b/_example/grid.go index 0c97ab9..d4078c5 100644 --- a/_example/grid.go +++ b/_example/grid.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/linechart.go b/_example/linechart.go index 1749e7b..68573c8 100644 --- a/_example/linechart.go +++ b/_example/linechart.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/list.go b/_example/list.go index e1914c6..2520b98 100644 --- a/_example/list.go +++ b/_example/list.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/mbarchart.go b/_example/mbarchart.go index 0fed643..c65fc59 100644 --- a/_example/mbarchart.go +++ b/_example/mbarchart.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/par.go b/_example/par.go index f8539fe..827b31b 100644 --- a/_example/par.go +++ b/_example/par.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/sparklines.go b/_example/sparklines.go index 4b3a5b6..dc486f4 100644 --- a/_example/sparklines.go +++ b/_example/sparklines.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/tabs.go b/_example/tabs.go index ae397c4..1bfe3d5 100644 --- a/_example/tabs.go +++ b/_example/tabs.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + // +build ignore package main diff --git a/_example/theme.go b/_example/theme.go index 30c51a3..0c36cec 100644 --- a/_example/theme.go +++ b/_example/theme.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/_example/ttop.go b/_example/ttop.go index a640c3b..73c9b85 100644 --- a/_example/ttop.go +++ b/_example/ttop.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + // +build ignore package main diff --git a/_extra/tabpane.go b/_extra/tabpane.go index dbfefe6..7647238 100644 --- a/_extra/tabpane.go +++ b/_extra/tabpane.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + package extra import ( diff --git a/barchart.go b/barchart.go index ed59184..980e958 100644 --- a/barchart.go +++ b/barchart.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/block.go b/block.go index fabd098..418738c 100644 --- a/block.go +++ b/block.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/block_common.go b/block_common.go index ed0f6a6..0f972cb 100644 --- a/block_common.go +++ b/block_common.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/block_test.go b/block_test.go index 9be8aa7..5b0ce92 100644 --- a/block_test.go +++ b/block_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 ( diff --git a/block_windows.go b/block_windows.go index dd39019..4dbc017 100644 --- a/block_windows.go +++ b/block_windows.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/buffer.go b/buffer.go index 6eabc02..60e7786 100644 --- a/buffer.go +++ b/buffer.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/buffer_test.go b/buffer_test.go index 8fbf812..5786157 100644 --- a/buffer_test.go +++ b/buffer_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 ( diff --git a/canvas.go b/canvas.go index 9422f5e..4173780 100644 --- a/canvas.go +++ b/canvas.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/canvas_test.go b/canvas_test.go index a955587..d059174 100644 --- a/canvas_test.go +++ b/canvas_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + // +build ignore package termui diff --git a/config b/config new file mode 100755 index 0000000..18fd6a4 --- /dev/null +++ b/config @@ -0,0 +1,26 @@ +#!/usr/bin/env perl6 + +use v6; + +my $copyright = '// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + +'; + +sub MAIN('update-docstr', Str $srcp) { + if $srcp.IO.f { + $_ = $srcp.IO.slurp; + if m/^ \/\/\s Copyright .+? \n\n/ { + unless ~$/ eq $copyright { + s/^ \/\/\s Copyright .+? \n\n /$copyright/; + spurt $srcp, $_; + say "[updated] doc string for:"~$srcp; + } + } else { + say "[added] doc string for "~$srcp~" (no match found)"; + $_ = $copyright ~ $_; + spurt $srcp, $_; + } + } +} diff --git a/debug/debuger.go b/debug/debuger.go index ac86226..f723b96 100644 --- a/debug/debuger.go +++ b/debug/debuger.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + package debug import ( diff --git a/doc.go b/doc.go index 43f886f..af24578 100644 --- a/doc.go +++ b/doc.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. @@ -19,7 +19,7 @@ A simplest example: g := ui.NewGauge() g.Percent = 50 g.Width = 50 - g.Border.Label = "Gauge" + g.BorderLabel = "Gauge" ui.Render(g) } diff --git a/events.go b/events.go index 22e7193..177bbb4 100644 --- a/events.go +++ b/events.go @@ -1,10 +1,6 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. -// -// Portions of this file uses [termbox-go](https://github.com/nsf/termbox-go/blob/54b74d087b7c397c402d0e3b66d2ccb6eaf5c2b4/api_common.go) -// by [authors](https://github.com/nsf/termbox-go/blob/master/AUTHORS) -// under [license](https://github.com/nsf/termbox-go/blob/master/LICENSE) package termui diff --git a/events_test.go b/events_test.go index c85634a..888559a 100644 --- a/events_test.go +++ b/events_test.go @@ -1,10 +1,6 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. -// -// Portions of this file uses [termbox-go](https://github.com/nsf/termbox-go/blob/54b74d087b7c397c402d0e3b66d2ccb6eaf5c2b4/api_common.go) -// by [authors](https://github.com/nsf/termbox-go/blob/master/AUTHORS) -// under [license](https://github.com/nsf/termbox-go/blob/master/LICENSE) package termui diff --git a/gauge.go b/gauge.go index f2753fe..244d299 100644 --- a/gauge.go +++ b/gauge.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/glide.lock b/glide.lock new file mode 100644 index 0000000..ad54acd --- /dev/null +++ b/glide.lock @@ -0,0 +1,16 @@ +hash: caa6fb85a72e1b1874b8d7261d8389c4a27a5c4ad85888eb647fae248bbb9862 +updated: 2016-02-15T11:19:55.10760639-05:00 +imports: +- name: github.com/mattn/go-runewidth + version: e882a96ec18dd43fa283187b66af74497c9101c0 +- name: github.com/nsf/termbox-go + version: 362329b0aa6447eadd52edd8d660ec1dff470295 +- name: golang.org/x/crypto + version: 1f22c0103821b9390939b6776727195525381532 +- name: golang.org/x/net + version: cbbbe2bc0f2efdd2afb318d93f1eadb19350e4a3 + subpackages: + - /websocket +- name: golang.org/x/text + version: 07b9a78963006a15c538ec5175243979025fa7a8 +devImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..1572662 --- /dev/null +++ b/glide.yaml @@ -0,0 +1,7 @@ +package: github.com/gizak/termui +import: +- package: github.com/mattn/go-runewidth +- package: github.com/nsf/termbox-go +- package: golang.org/x/net + subpackages: + - /websocket diff --git a/grid.go b/grid.go index 264b760..364442e 100644 --- a/grid.go +++ b/grid.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/grid_test.go b/grid_test.go index 9829586..07358b0 100644 --- a/grid_test.go +++ b/grid_test.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/helper.go b/helper.go index e563888..308f6c1 100644 --- a/helper.go +++ b/helper.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/helper_test.go b/helper_test.go index 5d277de..bce952c 100644 --- a/helper_test.go +++ b/helper_test.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/linechart.go b/linechart.go index 0689487..f282914 100644 --- a/linechart.go +++ b/linechart.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/linechart_others.go b/linechart_others.go index 8911873..7e2e66b 100644 --- a/linechart_others.go +++ b/linechart_others.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/linechart_windows.go b/linechart_windows.go index 9f9a5e9..1478b5c 100644 --- a/linechart_windows.go +++ b/linechart_windows.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/list.go b/list.go index 50361f2..670015f 100644 --- a/list.go +++ b/list.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/mbarchart.go b/mbarchart.go index c9d0c44..231de27 100644 --- a/mbarchart.go +++ b/mbarchart.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..2ab45f0 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,28 @@ +pages: +- Home: 'index.md' +- Quickstart: 'quickstart.md' +- Recipes: 'recipes.md' +- References: + - Layouts: 'layouts.md' + - Components: 'components.md' + - Events: 'events.md' + - Themes: 'themes.md' +- Versions: 'versions.md' +- About: 'about.md' + +site_name: termui +repo_url: https://github.com/gizak/termui/ +site_description: 'termui user guide' +site_author: gizak + +docs_dir: '_docs' + +theme: readthedocs + +markdown_extensions: + - smarty + - admonition + - toc + +extra: + version: 1.0 diff --git a/par.go b/par.go index 354519d..beaafbb 100644 --- a/par.go +++ b/par.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/par_test.go b/par_test.go index e689273..6ecb36d 100644 --- a/par_test.go +++ b/par_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 "testing" diff --git a/pos.go b/pos.go index b26fd11..2046dce 100644 --- a/pos.go +++ b/pos.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 "image" @@ -66,6 +70,9 @@ func MoveArea(a image.Rectangle, dx, dy int) image.Rectangle { return a } +var termWidth int +var termHeight int + func TermRect() image.Rectangle { - return image.Rect(0, 0, TermWidth(), TermHeight()) + return image.Rect(0, 0, termWidth, termHeight) } diff --git a/pos_test.go b/pos_test.go index 0454345..a54522a 100644 --- a/pos_test.go +++ b/pos_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 ( diff --git a/render.go b/render.go index 0cf9b88..36544f0 100644 --- a/render.go +++ b/render.go @@ -1,10 +1,12 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . 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 ( + "image" + "sync" "time" tm "github.com/nsf/termbox-go" @@ -26,11 +28,7 @@ func Init() error { go hookTermboxEvt() renderJobs = make(chan []Bufferer) - go func() { - for bs := range renderJobs { - render(bs...) - } - }() + //renderLock = new(sync.RWMutex) Body = NewGrid() Body.X = 0 @@ -51,6 +49,13 @@ func Init() error { DefaultWgtMgr = NewWgtMgr() DefaultEvtStream.Hook(DefaultWgtMgr.WgtHandlersHook()) + + go func() { + for bs := range renderJobs { + render(bs...) + } + }() + return nil } @@ -60,40 +65,71 @@ func Close() { tm.Close() } +var renderLock sync.Mutex + +func termSync() { + renderLock.Lock() + tm.Sync() + termWidth, termHeight = tm.Size() + renderLock.Unlock() +} + // TermWidth returns the current terminal's width. func TermWidth() int { - tm.Sync() - w, _ := tm.Size() - return w + termSync() + return termWidth } // TermHeight returns the current terminal's height. func TermHeight() int { - tm.Sync() - _, h := tm.Size() - return h + termSync() + return termHeight } // Render renders all Bufferer in the given order from left to right, // right could overlap on left ones. func render(bs ...Bufferer) { - // set tm bg - tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg"))) + for _, b := range bs { + buf := b.Buffer() // set cels in buf for p, c := range buf.CellMap { if p.In(buf.Area) { + tm.SetCell(p.X, p.Y, c.Ch, toTmAttr(c.Fg), toTmAttr(c.Bg)) + } } + } + + renderLock.Lock() // render tm.Flush() + renderLock.Unlock() +} + +func Clear() { + tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg"))) +} + +func clearArea(r image.Rectangle, bg Attribute) { + for i := r.Min.X; i < r.Max.X; i++ { + for j := r.Min.Y; j < r.Max.Y; j++ { + tm.SetCell(i, j, ' ', tm.ColorDefault, toTmAttr(bg)) + } + } +} + +func ClearArea(r image.Rectangle, bg Attribute) { + clearArea(r, bg) + tm.Flush() } var renderJobs chan []Bufferer func Render(bs ...Bufferer) { - go func() { renderJobs <- bs }() + //go func() { renderJobs <- bs }() + renderJobs <- bs } diff --git a/sparkline.go b/sparkline.go index 02a1034..312ad95 100644 --- a/sparkline.go +++ b/sparkline.go @@ -1,12 +1,10 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . 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 "math" - -// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃ +// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers. /* 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} spl := termui.NewSparkline() @@ -84,14 +82,18 @@ func (sl *Sparklines) update() { for i := 0; i < sl.displayLines; i++ { data := sl.Lines[i].Data - max := math.MinInt32 + max := 0 for _, v := range data { if max < v { max = v } } sl.Lines[i].max = max - sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max) + if max != 0 { + sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max) + } else { // when all negative + sl.Lines[i].scale = 0 + } } } @@ -127,7 +129,12 @@ func (sl *Sparklines) Buffer() Buffer { } for j, v := range data { + // display height of the data point, zero when data is negative h := int(float32(v)*l.scale + 0.5) + if v < 0 { + h = 0 + } + barCnt := h / 8 barMod := h % 8 for jj := 0; jj < barCnt; jj++ { diff --git a/test/runtest.go b/test/runtest.go index 97caf83..99794c4 100644 --- a/test/runtest.go +++ b/test/runtest.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + package main import ( diff --git a/textbuilder.go b/textbuilder.go index 2627098..06a019b 100644 --- a/textbuilder.go +++ b/textbuilder.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 ( @@ -35,6 +39,7 @@ var colorMap = map[string]Attribute{ "blue": ColorBlue, "black": ColorBlack, "cyan": ColorCyan, + "yellow": ColorYellow, "white": ColorWhite, "default": ColorDefault, "green": ColorGreen, diff --git a/textbuilder_test.go b/textbuilder_test.go index 93aa62e..71574e1 100644 --- a/textbuilder_test.go +++ b/textbuilder_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 "testing" diff --git a/theme.go b/theme.go index 7ee1fbb..c3ccda5 100644 --- a/theme.go +++ b/theme.go @@ -1,4 +1,4 @@ -// Copyright 2015 Zack Guo . All rights reserved. +// Copyright 2016 Zack Guo . All rights reserved. // Use of this source code is governed by a MIT license that can // be found in the LICENSE file. diff --git a/theme_test.go b/theme_test.go index b488a09..a6a99b3 100644 --- a/theme_test.go +++ b/theme_test.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 "testing" diff --git a/vendor/github.com/mattn/go-runewidth b/vendor/github.com/mattn/go-runewidth new file mode 160000 index 0000000..e882a96 --- /dev/null +++ b/vendor/github.com/mattn/go-runewidth @@ -0,0 +1 @@ +Subproject commit e882a96ec18dd43fa283187b66af74497c9101c0 diff --git a/vendor/github.com/nsf/termbox-go b/vendor/github.com/nsf/termbox-go new file mode 160000 index 0000000..362329b --- /dev/null +++ b/vendor/github.com/nsf/termbox-go @@ -0,0 +1 @@ +Subproject commit 362329b0aa6447eadd52edd8d660ec1dff470295 diff --git a/vendor/golang.org/x/crypto b/vendor/golang.org/x/crypto new file mode 160000 index 0000000..1f22c01 --- /dev/null +++ b/vendor/golang.org/x/crypto @@ -0,0 +1 @@ +Subproject commit 1f22c0103821b9390939b6776727195525381532 diff --git a/vendor/golang.org/x/net b/vendor/golang.org/x/net new file mode 160000 index 0000000..cbbbe2b --- /dev/null +++ b/vendor/golang.org/x/net @@ -0,0 +1 @@ +Subproject commit cbbbe2bc0f2efdd2afb318d93f1eadb19350e4a3 diff --git a/vendor/golang.org/x/text b/vendor/golang.org/x/text new file mode 160000 index 0000000..07b9a78 --- /dev/null +++ b/vendor/golang.org/x/text @@ -0,0 +1 @@ +Subproject commit 07b9a78963006a15c538ec5175243979025fa7a8 diff --git a/widget.go b/widget.go index df15b5d..35cf143 100644 --- a/widget.go +++ b/widget.go @@ -1,3 +1,7 @@ +// Copyright 2016 Zack Guo . 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 (