goterminal

package module
v0.0.0-...-614d345 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2018 License: MIT Imports: 8 Imported by: 28

README

goterminal GoDoc

A cross-platform Go-library for updating progress in terminal.

Installation

  go get -u github.com/apoorvam/goterminal

Usage

    func main() {
    	// get an instance of writer
    	writer := goterminal.New()

    	for i := 0; i < 100; i = i + 10 {
    		// add your text to writer's buffer
    		fmt.Fprintf(writer, "Downloading (%d/100) bytes...\n", i)
    		// write to terminal
    		writer.Print()
    		time.Sleep(time.Millisecond * 200)

    		// clear the text written by previous write, so that it can be re-written.
    		writer.Clear()
    	}

    	// reset the writer
    	writer.Reset()
    	fmt.Println("Download finished!")
    }

Example

output

Another example which uses the go-colortext library to re-write text along with using colors. Here is output of example:

output

Examples can be found here.

More on usage
  • Create a Writer instance.
  • Add your text to writer's buffer and call Print() to print text on Terminal. This can be called any number of times.
  • Call Clear() to move the cursor to position where first Print() started so that it can be over-written.
  • Reset() writer, so it clears its state for next Write.

Documentation

Overview

Package goterminal provides a way to write and re-write the text on console. It works on all platforms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	Out io.Writer
	Buf bytes.Buffer
	// contains filtered or unexported fields
}

Writer represents the IO writer which updates the UI and holds the buffer.

func New

func New(out io.Writer) *Writer

New returns a new instance of the Writer. It initializes the terminal width and buffer.

func (*Writer) Clear

func (w *Writer) Clear()

Flush moves the cursor to location where last write started and clears the text written using previous Write.

func (*Writer) GetTermDimensions

func (w *Writer) GetTermDimensions() (int, int)

GetTermDimensions returns the width and height of the current terminal

func (*Writer) Print

func (w *Writer) Print() error

Print writes the buffer contents to Out and resets the buffer. It stores the number of lines to go up the Writer in the Writer.lineCount.

func (*Writer) Reset

func (w *Writer) Reset()

Reset resets the Writer.

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL