Categorygithub.com/buger/goterm
modulepackage
1.0.4
Repository: https://github.com/buger/goterm.git
Documentation: pkg.go.dev

# README

Description

This library provides basic building blocks for building advanced console UIs.

Initially created for Gor.

Full API documentation: http://godoc.org/github.com/buger/goterm

Basic usage

Full screen console app, printing current time:

import (
    tm "github.com/buger/goterm"
    "time"
)

func main() {
    tm.Clear() // Clear current screen

    for {
        // By moving cursor to top-left position we ensure that console output
        // will be overwritten each time, instead of adding new.
        tm.MoveCursor(1,1)

        tm.Println("Current Time:", time.Now().Format(time.RFC1123))

        tm.Flush() // Call it every time at the end of rendering

        time.Sleep(time.Second)
    }
}

This can be seen in examples/time_example.go. To run it yourself, go into your $GOPATH/src/github.com/buger/goterm directory and run go run ./examples/time_example.go

Print red bold message on white background:

tm.Println(tm.Background(tm.Color(tm.Bold("Important header"), tm.RED), tm.WHITE))

Create box and move it to center of the screen:

tm.Clear()

// Create Box with 30% width of current screen, and height of 20 lines
box := tm.NewBox(30|tm.PCT, 20, 0)

// Add some content to the box
// Note that you can add ANY content, even tables
fmt.Fprint(box, "Some box content")

// Move Box to approx center of the screen
tm.Print(tm.MoveTo(box.String(), 40|tm.PCT, 40|tm.PCT))

tm.Flush()

This can be found in examples/box_example.go.

Draw table:

// Based on http://golang.org/pkg/text/tabwriter
totals := tm.NewTable(0, 10, 5, ' ', 0)
fmt.Fprintf(totals, "Time\tStarted\tActive\tFinished\n")
fmt.Fprintf(totals, "%s\t%d\t%d\t%d\n", "All", started, started-finished, finished)
tm.Println(totals)
tm.Flush()

This can be found in examples/table_example.go.

Line charts

Chart example:

screen shot 2013-07-09 at 5 05 37 pm

    import (
        tm "github.com/buger/goterm"
    )

    chart := tm.NewLineChart(100, 20)
    
    data := new(tm.DataTable)
    data.AddColumn("Time")
    data.AddColumn("Sin(x)")
    data.AddColumn("Cos(x+1)")

    for i := 0.1; i < 10; i += 0.1 {
	data.AddRow(i, math.Sin(i), math.Cos(i+1))
    }
    
    tm.Println(chart.Draw(data))

This can be found in examples/chart_example.go.

Drawing 2 separate graphs in different scales. Each graph have its own Y axe.

chart.Flags = tm.DRAW_INDEPENDENT

Drawing graph with relative scale (Grapwh draw starting from min value instead of zero)

chart.Flags = tm.DRAW_RELATIVE

# Packages

# Functions

Change background color of string: tm.Background("string", tm.RED) .
Make bold.
Clear screen.
Apply given color to string: tm.Color("RED STRING", tm.RED) .
CurrentHeight gets current height.
Flush buffer and ensure that it will not overflow screen.
GetXY gets relative or absolute coordinates To get relative, set PCT flag to number: // Get 10% of total width to `x` and 20 to y x, y = tm.GetXY(10|tm.PCT, 20) .
Height gets console height.
Move cursor to given position.
Move cursor backward relative the current position.
Move cursor down relative the current position.
Move cursor forward relative the current position.
Move cursor up relative the current position.
Move string to possition.
Create new Box.
Same as here http://golang.org/pkg/text/tabwriter/#Writer.Init.
ResetLine returns carrier to start of line.
Width gets console width.

# Constants

List of possible colors.
List of possible colors.
List of possible colors.
List of possible colors.
List of possible colors.
List of possible colors.
Reset all custom styles.
Reset to default color.
Return cursor to start of line and clean it.
List of possible colors.
List of possible colors.

# Variables

Global screen buffer Its not recommended write to buffer dirrectly, use package Print,Printf,Println fucntions instead.

# Structs

Box allows you to create independent parts of screen, with its own buffer and borders.
Tabwriter with own buffer: totals := tm.NewTable(0, 10, 5, ' ', 0) fmt.Fprintf(totals, "Time\tStarted\tActive\tFinished\n") fmt.Fprintf(totals, "%s\t%d\t%d\t%d\n", "All", started, started-finished, finished) tm.Println(totals) Based on http://golang.org/pkg/text/tabwriter.

# Interfaces