curse

package
v0.1.3-beta Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2016 License: GPL-3.0 Imports: 9 Imported by: 0

README

Curse

A utility for manipulating the terminal cursor. Current feature set:

  • Get terminal cursor position
  • Move cursor
  • Move up, down n-lines
  • Clear line
  • Clear Screen (up, down, all)
  • Set Color

Basic Example usage (see below for an inline-progress bar):

    package main

    import (
        "fmt"
        "log"

        "github.com/sethgrid/curse"
    )

    func main() {

        c, err := curse.New()
        if err != nil {
            log.Fatal(err)
        }

        c.SetColorBold(curse.RED).SetBackgroundColor(curse.BLACK)
        fmt.Println("Position: ", c.Position)
        c.SetDefaultStyle()
        fmt.Println("something to be erased")
        c.MoveUp(1).EraseCurrentLine().MoveDown(1)
    }

Progress Bar Example:

    package main

    import (
        "fmt"
        "strings"
        "time"

        "github.com/sethgrid/curse"
    )

    func main() {
        fmt.Println("Progress Bar")
        total := 150
        progressBarWidth := 80
        c, _ := curse.New()

        // give some buffer space on the terminal
        fmt.Println()

        // display a progress bar
        for i := 0; i <= total; i++ {
            c.MoveUp(1)
            c.EraseCurrentLine()
            fmt.Printf("%d/%d ", i, total)

            c.MoveDown(1)
            c.EraseCurrentLine()
            fmt.Printf("%s", progressBar(i, total, progressBarWidth))

            time.Sleep(time.Millisecond * 25)
        }
        // end the previous last line of output
        fmt.Println()
        fmt.Println("Complete")
    }

    func progressBar(progress, total, width int) string {
        bar := make([]string, width)
        for i, _ := range bar {
            if float32(progress)/float32(total) > float32(i)/float32(width) {
                bar[i] = "*"
            } else {
                bar[i] = " "
            }
        }
        return "[" + strings.Join(bar, "") + "]"
    }

Documentation

Index

Constants

View Source
const (
	// control
	ESC = 27

	// style
	BLACK   = 0
	RED     = 1
	GREEN   = 2
	YELLOW  = 3
	BLUE    = 4
	MAGENTA = 5
	CYAN    = 6
	WHITE   = 7

	FORGROUND  = 30
	BACKGROUND = 40
)

Variables

This section is empty.

Functions

func GetCursorPosition

func GetCursorPosition() (col int, line int, err error)

func GetScreenDimensions

func GetScreenDimensions() (cols int, lines int, err error)

using named returns to help when using the method to know what is what

Types

type Cursor

type Cursor struct {
	Position
	StartingPosition Position
	Style
	// contains filtered or unexported fields
}

func New

func New() (*Cursor, error)

func (*Cursor) EraseAll

func (c *Cursor) EraseAll() *Cursor

func (*Cursor) EraseCurrentLine

func (c *Cursor) EraseCurrentLine() *Cursor

func (*Cursor) EraseDown

func (c *Cursor) EraseDown() *Cursor

func (*Cursor) EraseUp

func (c *Cursor) EraseUp() *Cursor

func (*Cursor) ModeRaw

func (c *Cursor) ModeRaw() *Cursor

func (*Cursor) ModeRestore

func (c *Cursor) ModeRestore() *Cursor

func (*Cursor) Move

func (c *Cursor) Move(col, line int) *Cursor

func (*Cursor) MoveDown

func (c *Cursor) MoveDown(nLines int) *Cursor

func (*Cursor) MoveLeft

func (c *Cursor) MoveLeft(nSpaces int) *Cursor

func (*Cursor) MoveRight

func (c *Cursor) MoveRight(nSpaces int) *Cursor

func (*Cursor) MoveUp

func (c *Cursor) MoveUp(nLines int) *Cursor

func (*Cursor) Reset

func (c *Cursor) Reset() *Cursor

func (*Cursor) SetBackgroundColor

func (c *Cursor) SetBackgroundColor(color int) *Cursor

func (*Cursor) SetColor

func (c *Cursor) SetColor(color int) *Cursor

func (*Cursor) SetColorBold

func (c *Cursor) SetColorBold(color int) *Cursor

func (*Cursor) SetDefaultStyle

func (c *Cursor) SetDefaultStyle() *Cursor

type Position

type Position struct {
	X, Y int
}

type Style

type Style struct {
	Foreground, Background, Bold int
}

Jump to

Keyboard shortcuts

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