README.md
1## Description
2
3This library provides basic building blocks for building advanced console UIs.
4
5Initially created for [Gor](http://github.com/buger/gor).
6
7Full API documentation: http://godoc.org/github.com/buger/goterm
8
9## Basic usage
10
11Full screen console app, printing current time:
12
13```go
14import (
15 tm "github.com/buger/goterm"
16 "time"
17)
18
19func main() {
20 tm.Clear() // Clear current screen
21
22 for {
23 // By moving cursor to top-left position we ensure that console output
24 // will be overwritten each time, instead of adding new.
25 tm.MoveCursor(1,1)
26
27 tm.Println("Current Time:", time.Now().Format(time.RFC1123))
28
29 tm.Flush() // Call it every time at the end of rendering
30
31 time.Sleep(time.Second)
32 }
33}
34```
35
36This can be seen in [examples/time_example.go](examples/time_example.go). To
37run it yourself, go into your `$GOPATH/src/github.com/buger/goterm` directory
38and run `go run ./examples/time_example.go`
39
40
41Print red bold message on white background:
42
43```go
44tm.Println(tm.Background(tm.Color(tm.Bold("Important header"), tm.RED), tm.WHITE))
45```
46
47
48Create box and move it to center of the screen:
49
50```go
51tm.Clear()
52
53// Create Box with 30% width of current screen, and height of 20 lines
54box := tm.NewBox(30|tm.PCT, 20, 0)
55
56// Add some content to the box
57// Note that you can add ANY content, even tables
58fmt.Fprint(box, "Some box content")
59
60// Move Box to approx center of the screen
61tm.Print(tm.MoveTo(box.String(), 40|tm.PCT, 40|tm.PCT))
62
63tm.Flush()
64```
65
66This can be found in [examples/box_example.go](examples/box_example.go).
67
68Draw table:
69
70```go
71// Based on http://golang.org/pkg/text/tabwriter
72totals := tm.NewTable(0, 10, 5, ' ', 0)
73fmt.Fprintf(totals, "Time\tStarted\tActive\tFinished\n")
74fmt.Fprintf(totals, "%s\t%d\t%d\t%d\n", "All", started, started-finished, finished)
75tm.Println(totals)
76tm.Flush()
77```
78
79This can be found in [examples/table_example.go](examples/table_example.go).
80
81## Line charts
82
83Chart example:
84
85![screen shot 2013-07-09 at 5 05 37 pm](https://f.cloud.github.com/assets/14009/767676/e3dd35aa-e887-11e2-9cd2-f6451eb26adc.png)
86
87
88```go
89 import (
90 tm "github.com/buger/goterm"
91 )
92
93 chart := tm.NewLineChart(100, 20)
94
95 data := new(tm.DataTable)
96 data.AddColumn("Time")
97 data.AddColumn("Sin(x)")
98 data.AddColumn("Cos(x+1)")
99
100 for i := 0.1; i < 10; i += 0.1 {
101 data.AddRow(i, math.Sin(i), math.Cos(i+1))
102 }
103
104 tm.Println(chart.Draw(data))
105```
106
107This can be found in [examples/chart_example.go](examples/chart_example.go).
108
109Drawing 2 separate graphs in different scales. Each graph have its own Y axe.
110
111```go
112chart.Flags = tm.DRAW_INDEPENDENT
113```
114
115Drawing graph with relative scale (Grapwh draw starting from min value instead of zero)
116
117```go
118chart.Flags = tm.DRAW_RELATIVE
119```
120