Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Advanced terminal output in Go

License

NotificationsYou must be signed in to change notification settings

buger/goterm

Repository files navigation

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

Initially created forGor.

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")funcmain() {tm.Clear()// Clear current screenfor {// 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 renderingtime.Sleep(time.Second)    }}

This can be seen inexamples/time_example.go. Torun it yourself, go into your$GOPATH/src/github.com/buger/goterm directoryand rungo 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 linesbox:=tm.NewBox(30|tm.PCT,20,0)// Add some content to the box// Note that you can add ANY content, even tablesfmt.Fprint(box,"Some box content")// Move Box to approx center of the screentm.Print(tm.MoveTo(box.String(),40|tm.PCT,40|tm.PCT))tm.Flush()

This can be found inexamples/box_example.go.

Draw table:

// Based on http://golang.org/pkg/text/tabwritertotals:=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 inexamples/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)")fori:=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 inexamples/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

[8]ページ先頭

©2009-2025 Movatter.jp