goTerminal

package module
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 5 Imported by: 5

README

go-terminal

Go Report Card GoDoc MIT Licensed

A package that facilitates the creation of command line applications with custom colors and fonts and cursor manipulation

Installing

go get https://github.com/leandroveronezi/go-terminal

Examples

SGR (Select Graphic Rendition)
package main

import (
	"fmt"
	"github.com/leandroveronezi/go-terminal"
)

func main() {

	goTerminal.Clean()
	goTerminal.CursorLineColumn(1, 1)

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Bold)
	fmt.Println("Bold")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Faint)
	fmt.Println("Faint")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Italic)
	fmt.Println("Italic")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Underlined)
	fmt.Println("Underlined")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BlinkSlow)
	fmt.Println("BlinkSlow")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Reverse)
	fmt.Println("Reverse")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Invisible)
	fmt.Println("Invisible")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Strike)
	fmt.Println("Strike")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Overline)
	fmt.Println("Overline")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Strike, goTerminal.Italic)
	fmt.Println("Strike + Italic")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Strike, goTerminal.Italic, goTerminal.Reverse)
	fmt.Println("Strike + Italic + Reverse")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Bold, goTerminal.Italic)
	fmt.Println("Bold + Italic")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Italic, goTerminal.Underlined)
	fmt.Println("Italic + Underlined")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.Italic, goTerminal.Underlined, goTerminal.Reverse, goTerminal.BlinkSlow)
	fmt.Print("Italic + Underlined + Reverse + BlinkSlow")

	goTerminal.SetSGR(goTerminal.Reset)

	pos, err := goTerminal.Size()

	if err == nil {		
		fmt.Println("x:", pos.X)
		fmt.Println("y:", pos.Y)
	} else {
		fmt.Println(err)
	}

	goTerminal.CursorNextLine()

	goTerminal.SetSGR(goTerminal.Reset)
	goTerminal.SetSGR(goTerminal.BackgroundDefault)

}

Standard colors
package main

import (
	"fmt"
	"github.com/leandroveronezi/go-terminal"
)

func main() {

	goTerminal.Clean()
	goTerminal.CursorLineColumn(1, 1)

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundDefault)
	fmt.Println("Default")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundBlack)
	fmt.Println("Black")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundRed)
	fmt.Println("Red")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundGreen)
	fmt.Println("Green")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundYellow)
	fmt.Println("Yellow")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundBlue)
	fmt.Println("Blue")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundMagenta)
	fmt.Println("Magenta")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundCyan)
	fmt.Println("Cyan")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightGray)
	fmt.Println("LightGray")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundDarkGray)
	fmt.Println("DarkGray")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightRed)
	fmt.Println("LightRed")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightGreen)
	fmt.Println("LightGreen")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightYellow)
	fmt.Println("LightYellow")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightBlue)
	fmt.Println("LightBlue")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightMagenta)
	fmt.Println("LightMagenta")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundLightCyan)
	fmt.Println("LightCyan")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundWhite)
	fmt.Println("White")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.ForegroundDefault)

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundDefault)
	fmt.Println("Default")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundBlack)
	fmt.Println("Black")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundRed)
	fmt.Println("Red")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundGreen)
	fmt.Println("Green")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundYellow)
	fmt.Println("Yellow")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundBlue)
	fmt.Println("Blue")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundMagenta)
	fmt.Println("Magenta")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundCyan)
	fmt.Println("Cyan")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightGray)
	fmt.Println("LightGray")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundDarkGray)
	fmt.Println("DarkGray")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightRed)
	fmt.Println("LightRed")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightGreen)
	fmt.Println("LightGreen")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightYellow)
	fmt.Println("LightYellow")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightBlue)
	fmt.Println("LightBlue")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightMagenta)
	fmt.Println("LightMagenta")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundLightCyan)
	fmt.Println("LightCyan")
	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundWhite)
	fmt.Println("White")

	goTerminal.SetSGR(goTerminal.Reset, goTerminal.BackgroundDefault)

}

RGB colors
package main

import (
	"fmt"
	"github.com/leandroveronezi/go-terminal"
)

func main() {

	goTerminal.Clean()
	goTerminal.CursorLineColumn(1, 1)
	goTerminal.SetSGR(goTerminal.Reset)

	x := 1
	y := 1

	for i := 0; i < 256; i++ {

		goTerminal.ColorRGBBackground(i, 0, 0)
		goTerminal.CursorLineColumn(x, y)
		fmt.Print(" ")

		goTerminal.ColorRGBBackground(0, i, 0)
		goTerminal.CursorLineColumn(x, y+20)
		fmt.Print(" ")

		goTerminal.ColorRGBBackground(0, 0, i)
		goTerminal.CursorLineColumn(x, y+40)
		fmt.Print(" ")

		y = y + 1

		if y >= 16 {
			y = 1
			x = x + 1
		}

	}

	goTerminal.CursorNextLine()

	goTerminal.SetSGR(goTerminal.Reset)
	goTerminal.SetSGR(goTerminal.BackgroundDefault)

}

256 colors
package main

import (
	"fmt"
	"github.com/leandroveronezi/go-terminal"
)

func main() {

	goTerminal.Clean()
	goTerminal.CursorLineColumn(1, 1)
	goTerminal.SetSGR(goTerminal.Reset)

	for i := 0; i <= 16; i++ {

		for j := 0; j <= 16; j++ {

			code := i*16 + j
			goTerminal.Color256Background(code)
			goTerminal.CursorLineColumn(i, j)
			fmt.Print(" ")

		}

	}

	goTerminal.CursorNextLine()

	goTerminal.SetSGR(goTerminal.Reset)
	goTerminal.SetSGR(goTerminal.BackgroundDefault)

}

Cursor Control

package main

import (
	"fmt"
	"github.com/leandroveronezi/go-terminal"
)

func main() {

    goTerminal.Clean()
    goTerminal.CursorLineColumn(1, 1)
    goTerminal.SetSGR(goTerminal.Reset)
    
    //A moves cursor up by n
    goTerminal.CursorUp(1)
    
    //moves cursor down by n
    goTerminal.CursorDown(1)
            
    //moves cursor right by n
    goTerminal.CursorRight(1)
            
    //moves cursor left by n
    goTerminal.CursorLeft(1)
            
    //Save cursor position and attributes
    goTerminal.SaveCursorAttrs()
            
    //Restore cursor position and attributes
    goTerminal.RestoreCursorAttrs()
    
    //Save cursor position
    goTerminal.SaveCursor()
    
    //Restore cursor position
    goTerminal.RestoreCursor()
    
    //Move to next line
    goTerminal.CursorNextLine()
    
    //moves cursor to column n
    goTerminal.CursorColumn(10)
            
    //Move cursor to screen location v,h
    goTerminal.CursorLineColumn(4, 1)
    
    //Move cursor to upper left corner
    goTerminal.CursorHome()
    
    //Move cursor to upper left corner
    goTerminal.CursorLeftHome()
    
    //CursorHide hide the cursor.
    goTerminal.CursorHide()
    
    //CursorShow shows the cursor.
    goTerminal.CursorShow()

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Output = os.Stdout

Functions

func Clean

func Clean()

Clean screen

func Color256Background

func Color256Background(Color int)

Select 256 Background color

func Color256Foreground

func Color256Foreground(Color int)

Select 256 Foreground color

func ColorRGBBackground

func ColorRGBBackground(r, g, b int)

Select RGB Background color

func ColorRGBForeground

func ColorRGBForeground(r, g, b int)

Select RGB Foreground color

func CursorColumn

func CursorColumn(n int)

moves cursor to column n

func CursorDown

func CursorDown(n int)

moves cursor down by n

func CursorHide

func CursorHide()

CursorHide hide the cursor.

func CursorHome

func CursorHome()

Move cursor to upper left corner

func CursorLeft

func CursorLeft(n int)

moves cursor left by n

func CursorLeftHome

func CursorLeftHome()

Move cursor to upper left corner hvhome

func CursorLineColumn

func CursorLineColumn(Line, Column int)

Move cursor to screen location v,h

func CursorNextLine

func CursorNextLine()

Move to next line

func CursorRight

func CursorRight(n int)

moves cursor right by n

func CursorShow

func CursorShow()

CursorShow shows the cursor.

func CursorUp

func CursorUp(n int)

A moves cursor up by n

func EraseEndOfLine

func EraseEndOfLine()

Erases from the current cursor position to the end of the current line.

func EraseLine

func EraseLine()

Erases the entire current line.

func EraseStartOfLine

func EraseStartOfLine()

Erases from the current cursor position to the start of the current line.

func Fprintf

func Fprintf(format string, a ...interface{}) (n int, err error)

func Fprintln

func Fprintln(a ...interface{}) (n int, err error)

func Print

func Print(a ...interface{}) (n int, err error)

func Println

func Println(a ...interface{}) (n int, err error)

func RestoreCursor

func RestoreCursor()

Restore cursor position

func RestoreCursorAttrs

func RestoreCursorAttrs()

Restore cursor position and attributes

func SaveCursor

func SaveCursor()

Save cursor position

func SaveCursorAttrs

func SaveCursorAttrs()

Save cursor position and attributes

func ScreenErase

func ScreenErase()

Erases the screen with the background colour and moves the cursor to home.

func ScreenEraseDown

func ScreenEraseDown()

Erases the screen from the current line down to the bottom of the screen.

func ScreenEraseUp

func ScreenEraseUp()

Erases the screen from the current line up to the top of the screen.

func SetSGR

func SetSGR(Colors ...SGR)

Set SGR (Select Graphic Rendition) parameters

Types

type Attribute

type Attribute int

type Coord

type Coord struct {
	X int
	Y int
}

func Size

func Size() (*Coord, error)

Size returns the height and width of the terminal.

type SGR

type SGR int

SGR (Select Graphic Rendition) parameters

const (
	Reset      SGR = 0  //0	Reset / Normal	all attributes off
	Bold       SGR = 1  //1	Bold or increased intensity
	Faint      SGR = 2  //2	Faint (decreased intensity)
	Italic     SGR = 3  //3	Italic	Not widely supported. Sometimes treated as inverse.
	Underlined SGR = 4  //4	Underline
	BlinkSlow  SGR = 5  //5	Slow Blink	less than 150 per minute
	BlinkRapid SGR = 6  //6	Rapid Blink	MS-DOS ANSI.SYS; 150+ per minute; not widely supported
	Reverse    SGR = 7  //7	reverse video	swap foreground and background colors
	Invisible  SGR = 8  //8	Conceal	Not widely supported.
	Strike     SGR = 9  //9	Crossed-out	Characters legible, but marked for deletion.
	Overline   SGR = 53 //53	Overlined

	ForegroundDefault      SGR = 39
	ForegroundBlack        SGR = 30
	ForegroundRed          SGR = 31
	ForegroundGreen        SGR = 32
	ForegroundYellow       SGR = 33
	ForegroundBlue         SGR = 34
	ForegroundMagenta      SGR = 35
	ForegroundCyan         SGR = 36
	ForegroundLightGray    SGR = 37
	ForegroundDarkGray     SGR = 90
	ForegroundLightRed     SGR = 91
	ForegroundLightGreen   SGR = 92
	ForegroundLightYellow  SGR = 93
	ForegroundLightBlue    SGR = 94
	ForegroundLightMagenta SGR = 95
	ForegroundLightCyan    SGR = 96
	ForegroundWhite        SGR = 97

	BackgroundDefault      SGR = 49
	BackgroundBlack        SGR = 40
	BackgroundRed          SGR = 41
	BackgroundGreen        SGR = 42
	BackgroundYellow       SGR = 43
	BackgroundBlue         SGR = 44
	BackgroundMagenta      SGR = 45
	BackgroundCyan         SGR = 46
	BackgroundLightGray    SGR = 47
	BackgroundDarkGray     SGR = 100
	BackgroundLightRed     SGR = 101
	BackgroundLightGreen   SGR = 102
	BackgroundLightYellow  SGR = 103
	BackgroundLightBlue    SGR = 104
	BackgroundLightMagenta SGR = 105
	BackgroundLightCyan    SGR = 106
	BackgroundWhite        SGR = 107
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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