Giter Site home page Giter Site logo

goterm's Introduction

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

goterm's People

Contributors

14rcole avatar anthrazz avatar buger avatar bugthesystem avatar codelingobot avatar different55 avatar dimapaloskin avatar ericfialkowski avatar gtardif avatar merlindmc avatar ndeloof avatar nerdmaster avatar notrev avatar pbarnum avatar radbrawler avatar tfogo avatar tsia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

goterm's Issues

[BUG] Undefined: math.MaxInt

go get github.com/buger/goterm                                                                                                                                                                      
# github.com/buger/goterm
../../../go/pkg/mod/github.com/buger/[email protected]/terminal.go:213:11: undefined: math.MaxInt

Error when installing: undefined: math.MaxInt.

go version go1.16.5 darwin/amd64

License?

I didn't see a license for this library. Could you point me to it or add one?

GetWinsize: inappropriate ioctl for device

Any idea why I get errors like these?

dave@tripper:goterm $ go run examples/chart_example.go                                  
Error: GetWinsize: inappropriate ioctl for device
dave@tripper:goterm $ go run examples/table_example.go                                  
Error: GetWinsize: inappropriate ioctl for device
dave@tripper:goterm $ go run examples/time_example.go                                   
Error: GetWinsize: inappropriate ioctl for device
Error: GetWinsize: inappropriate ioctl for device
Error: GetWinsize: inappropriate ioctl for device
Error: GetWinsize: inappropriate ioctl for device

Running on openbsd, if it matters. I've tried xterm, urxvt, tmux session - same error in each.

Thanks for any help.

rows of table

if table's rows more than 20,tm.Println will print nothing

colors mess up box formatting

since the box formatting directly uses len(line), the extra ANSI escape codes contained in line cause the box code to believe a line will be longer than it actually gets displayed as

Flush is not correct

Below is a test example:

package main

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

func main() {
	tm.Println("line1")
	tm.Print("line2")
	tm.Flush()

	tm.Println("line3")
	tm.Flush()
}

The ouput is as follows:
image
The output is not expected.
Actually, below is our expected:
image
Below are changes for Flush:
image

No output in VS Code debug console because Width/Height detected as -1

package main

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

func main() {
	println("before")
	goterm.Println("foo")
	goterm.Flush()
	println("after", goterm.Width(), goterm.Height())
}

Works if I start the program manually in a regular VS Code terminal (or any terminal really):

image

Doesnt work if I run it via a VS Code launch config, where the output will go into the VS Code debug console. Notice Width/Height is -1, and the text printed by goterm is missing

image

The root cause is that goterm limits its output to the detected Width/Height,

My expectation would be for goterm to handle -1/-1 by disabling row/col limits.

Need an example for using Color and Background

I am trying to change the color of text in the terminal, as well as the background of that text. I am unsure of how to do this. The documentation is not helpful and there are no examples.

Not working on windows terminal

I'm not able to make it work on windows terminal.

package main

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

func main() {
	defer tm.Println("\nAll is said and done.")
	var wg sync.WaitGroup
	ticker := time.Tick(time.Second)
	for i := 1; i <= 10; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()			
			<-ticker
			tm.MoveCursor(1, 1)
			// log.Printf("\CLS")
			tm.Printf("On %d/10 ", i)
			tm.Println(tm.Background(tm.Color(tm.Bold("Important header"), tm.RED), tm.WHITE))
			tm.Flush()

		}(i)
	}
	wg.Wait()
}

Desired Output:

On 10/10 Important header
Important Header in Red Bold with White Background.

Output:

�[1;1HOn 1/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 6/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 5/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 7/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 2/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 3/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 8/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 9/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 10/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

�[1;1HOn 4/10 �[47m�[31m�[1mImportant header�[0m�[0m�[0m

Is there a specific way to make it work or are windows terminals not compatible with the lib.

How to get user input at specific position?

I want to move my cursor using MoveCursor() and get a user input at that location. Using fmt.Scanln() does not work properly and getting user input using bufio doesn't give expected results either.

Using `unix.EOPNOTSUPP` will fail when build on windows.

Hey @buger , the last PR #38 brought the change which prevented built for the Windows OS.

if errors.Is(err, unix.EOPNOTSUPP) {
	return math.MaxInt
}

I suppose, that there are should be built tags, for Linux and separate terminal_windows.go w/o Operation not supported on transport endpoint (EOPNOTSUPP) constant.
Thanks :)

build failed on armv6

vendor/github.com/buger/goterm/terminal.go:106: constant 2147483648 overflows int
vendor/github.com/buger/goterm/terminal.go:110: constant 2147483648 overflows int

Is it work in alpine ?

I use docker container run golang.

docker pull golang:1.9-alpine3.7
docker run --name golang -d golang:1.9-alpine3.7
docker exec -ti golang sh

in container:

/go # akp add --no-cache git
/go # go get github.com/buger/goterm
/go # mkdir -p /go/src/github.com/test
/go # cd /go/src/github.com/test
/go/src/github.com/test # vi main.go

main.go

package main

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

then run it

/go/src/github.com/test # go run main.go

but it not work

like this

/go/src/github.com/test # go run main.go 


asdoifjaof


weirjqoweirjoq


^Csignal: interrupt
/go/src/github.com/test # 

Flush Issues

File : terminal.go

// Flush buffer and ensure that it will not overflow screen
func Flush() {
	for idx, str := range strings.Split(Screen.String(), "\n") {
		if idx > Height() {
			return
		}

		Output.WriteString(str + "\n")
	}

	Output.Flush()
	Screen.Reset()
}

This use "return" ,don't use "break"
What is the reason for that?
Don't has error message ,user don't kown

(English is relatively poor, hope forgive me)

time_example.go without moving the cursor to (1,1)

Is there any way to print in-place without resetting the cursor to (1,1).
I tried retrieving (x,y) from GetXY method with (-1,-1) params but that still resets the cursor.

Thanks for the great package.

Width incorrectly reported inside tmux

Given the code:

package main

import (
	"fmt"
	"time"

	"github.com/buger/goterm"
)

func main() {
	for {
		w := goterm.Width()
		fmt.Printf("\rwidth: %d", w)
		time.Sleep(100 * time.Millisecond)
	}
}

Width reported correctly without tmux:
normalf.gif

Width reported incorrectly inside tmux:
tmux.gif

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.