Merge branch 'master' into jrmiller82-feature-textwrapping

This commit is contained in:
gizak 2016-03-10 15:52:15 -05:00
commit cede030c41
69 changed files with 422 additions and 73 deletions

View File

@ -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:__
<img src="./_example/grid.gif" alt="grid" width="60%">
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

0
_docs/about.md Normal file
View File

32
_docs/components.md Normal file
View File

@ -0,0 +1,32 @@
Overview
---
Bufferer
---
Block
---
BarChart
---
Canvas
---
Gauge
---
LineChart
---
MBarChart
---
Par
---
Sparkline
---
Sparklines
---

14
_docs/events.md Normal file
View File

@ -0,0 +1,14 @@
Event System
---
Keyboard Events
---
Mouse Events
---
Window Events
---
Custom Events
---

BIN
_docs/img/dashboard.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

BIN
_docs/img/demo1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 KiB

15
_docs/index.md Normal file
View File

@ -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.

26
_docs/layouts.md Normal file
View File

@ -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
---

80
_docs/quickstart.md Normal file
View File

@ -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)

1
_docs/recipes.md Normal file
View File

@ -0,0 +1 @@
_Sorry, it is still Work in Progress..._

0
_docs/themes.md Normal file
View File

0
_docs/versions.md Normal file
View File

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.
@ -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)
}

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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.
// +build ignore
package main

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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.
// +build ignore
package main

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 extra
import (

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 (

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 (

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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.
// +build ignore
package termui

26
config Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env perl6
use v6;
my $copyright = '// Copyright 2016 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.
';
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, $_;
}
}
}

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 debug
import (

4
doc.go
View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.
@ -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)
}

View File

@ -1,10 +1,6 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.
//
// 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

View File

@ -1,10 +1,6 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.
//
// 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

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

16
glide.lock generated Normal file
View File

@ -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: []

7
glide.yaml Normal file
View File

@ -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

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

28
mkdocs.yml Normal file
View File

@ -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

2
par.go
View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 "testing"

9
pos.go
View File

@ -1,3 +1,7 @@
// Copyright 2016 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 "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)
}

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 (

View File

@ -1,10 +1,12 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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 (
"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
}

View File

@ -1,12 +1,10 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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 "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++ {

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 main
import (

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 (
@ -35,6 +39,7 @@ var colorMap = map[string]Attribute{
"blue": ColorBlue,
"black": ColorBlack,
"cyan": ColorCyan,
"yellow": ColorYellow,
"white": ColorWhite,
"default": ColorDefault,
"green": ColorGreen,

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 "testing"

View File

@ -1,4 +1,4 @@
// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Copyright 2016 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.

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 "testing"

1
vendor/github.com/mattn/go-runewidth generated vendored Submodule

@ -0,0 +1 @@
Subproject commit e882a96ec18dd43fa283187b66af74497c9101c0

1
vendor/github.com/nsf/termbox-go generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 362329b0aa6447eadd52edd8d660ec1dff470295

1
vendor/golang.org/x/crypto generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 1f22c0103821b9390939b6776727195525381532

1
vendor/golang.org/x/net generated vendored Submodule

@ -0,0 +1 @@
Subproject commit cbbbe2bc0f2efdd2afb318d93f1eadb19350e4a3

1
vendor/golang.org/x/text generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 07b9a78963006a15c538ec5175243979025fa7a8

View File

@ -1,3 +1,7 @@
// Copyright 2016 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 (