term

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2019 License: MIT Imports: 5 Imported by: 2

README

Term

Build Status

Online API documentation

term API documentation at godoc.org

Features and limitations

  • Provides an easy way to get started drawing colorful characters at any position (X,Y) in a terminal.
  • Uses ncurses and the gdamore/tcell module.

Simple example

package main

import (
	. "github.com/xyproto/term"
)

func main() {
	Init()
	Clear()
	Say(10, 7, "hi")
	Flush()
	WaitForKey()
	Close()
}

Another example

package main

import (
	"fmt"

	"github.com/xyproto/term"
)

// Loop and echo the input until "quit" is typed
func Repeat() {
	for {
		// Retrieve user input, with a prompt. Use ReadLn() for no prompt.
		line := term.Ask("> ")

		// Check if the user wants to quit
		if line == "quit" {
			break
		}

		// Repeat what was just said
		fmt.Println("You said: " + line)
	}
}

func main() {
	fmt.Print(`
Welcome to Repeat 1.0!

Type "quit" when done.

Ready.

`)
	Repeat()
}

General information

Documentation

Overview

Package term offers a simple way to use ncurses and output colored text

Index

Constants

View Source
const (
	TLCHAR = '╭' // top left
	TRCHAR = '╮' // top right
	BLCHAR = '╰' // bottom left
	BRCHAR = '╯' // bottom right
	VCHAR  = '│' // vertical line, left side
	VCHAR2 = '│' // vertical line, right side
	HCHAR  = '─' // horizontal line
	HCHAR2 = '─' // horizontal bottom line
)

These are used for drawing "ASCII-art" boxes

Only a few selected colors are included here, for providing a coherent visual appearance for command line applications. For more colors, import and use termbox directly.

View Source
const VersionString = "term 0.2"

Variables

View Source
var (
	BG          = Blue
	TITLECOLOR  = Cyan | Bold
	TEXTCOLOR   = Black
	BOXBG       = White
	BOXLIGHT    = White | Bold
	BOXDARK     = Black
	BUTTONFOCUS = Yellow | Bold
	BUTTONTEXT  = White | Bold
	LISTFOCUS   = Red
	LISTTEXT    = Black
)

Default color/attribute settings

Functions

func Ask

func Ask(prompt string) string

Ask a question, wait for textual input followed by a newline

func AskYesNo

func AskYesNo(question string, noIsDefault bool) bool

Ask a yes/no question, don't wait for newline

func Clear

func Clear()

Remove all text. Clear the screen.

func Close

func Close()

Close the text screen (using curses)

func DrawAsciiArt

func DrawAsciiArt(x, y int, text string) int

Outputs a multiline string at the given coordinates. Uses the box background color. Returns the final y coordinate after drawing.

func DrawBackground

func DrawBackground()

Draw the background color. Clear the screen.

func DrawButton

func DrawButton(x, y int, text string, active bool)

Draws a button widget at the given placement, with the given text. If active is False, it will look more "grayed out".

func DrawList

func DrawList(r *Box, items []string, selected int)

Draw a list widget. Takes a Box struct for the size and position. Takes a list of strings to be listed and an int that represents which item is currently selected. Does not scroll or wrap.

func DrawRaw

func DrawRaw(x, y int, text string) int

Outputs a multiline string at the given coordinates. Uses the default background color. Returns the final y coordinate after drawing.

func FilterS

func FilterS(f func(string) bool, sl []string) (result []string)

Filter out all strings where the function does not return true

func First

func First(f ReturnsTwoInts) int

Get the first value from the function that returns two ints

func Flush

func Flush()

Update the screen with what has been written so far

func Init

func Init() error

Initialize the text screen (using curses)

func MapS

func MapS(f func(string) string, sl []string) (result []string)

Map a function on each element of a slice of strings

func PollEvent

func PollEvent() *termbox.Event

Retrieve the next event in the queue (like a keypress)

func ReadLn

func ReadLn() string

Read a line from stdin

func Repeat

func Repeat(text string, n int) string

Repeat a string n number of times

func RepeatRune added in v0.2.2

func RepeatRune(r rune, n int) string

Repeat a rune n number of times

func Say

func Say(x int, y int, text string)

Place text at the given x and y coordinate, using the default color scheme.

func ScreenHeight

func ScreenHeight() int

Retrieve the number of lines of characters available on the current screen

func ScreenWidth

func ScreenWidth() int

Retrieve the number of character columns available on the current screen

func Second

func Second(f ReturnsTwoInts) int

Get the second value from the function that returns two ints

func SetBg

func SetBg(bg termbox.Attribute)

Set the background color

func SetFg

func SetFg(fg termbox.Attribute)

Set the text/forground color

func Splitlines

func Splitlines(s string) []string

Split a string on any newline: \n, \r or \r\n

func WaitForKey

func WaitForKey()

Wait for Esc, Enter or Space to be pressed

func Write

func Write(x int, y int, text string, fg termbox.Attribute, bg termbox.Attribute)

Place text at the given x and y coordinate. fg is the foreground color, while bg is the background color.

func WriteRune added in v0.2.2

func WriteRune(x int, y int, r rune, fg termbox.Attribute, bg termbox.Attribute)

Place a rune at the given x and y coordinate. fg is the foreground color, while bg is the background color.

Types

type Box

type Box struct {
	// contains filtered or unexported fields
}

Box has one outer and one innner rectangle. This is useful when having margins that surrounds content.

func NewBox

func NewBox() *Box

Create a new Box / container.

func (*Box) Center

func (b *Box) Center(container *Box)

Place a Box at the center of the given container.

func (*Box) Fill

func (b *Box) Fill(container *Box)

Place a Box so that it fills the entire given container.

func (*Box) FillWithMargins

func (b *Box) FillWithMargins(container *Box, margins int)

Place a Box inside a given container, with the given margins. Margins are given in number of characters.

func (*Box) FillWithPercentageMargins

func (b *Box) FillWithPercentageMargins(container *Box, horizmarginp float32, vertmarginp float32)

Place a Box inside a given container, using the given percentage wise ratios. horizmarginp can for example be 0.1 for a 10% horizontal margin around the inner box. vertmarginp works similarly, but for the vertical margins.

func (*Box) GetContentPos

func (b *Box) GetContentPos() (int, int)

Retrieves the position of the inner rectangle.

func (*Box) GetFrame

func (b *Box) GetFrame() *Rect

Get the outer frame (box size + pos)

func (*Box) GetInner

func (b *Box) GetInner() *Rect

Get the inner rectangle (content size + pos)

func (*Box) Place

func (b *Box) Place(container *Box)

Place a Box within the given container.

func (*Box) SetFrame

func (b *Box) SetFrame(r *Rect)

Set the outer frame (box size + pos)

func (*Box) SetInner

func (b *Box) SetInner(r *Rect)

Set the inner rectangle (content size + pos)

func (*Box) SetNicePlacement

func (b *Box) SetNicePlacement(container *Box)

Place a Box so that it either fills the given container, or is placed 1/3 from the upper left edge, depending on how much space is left.

func (*Box) SetThirdPlace

func (b *Box) SetThirdPlace(container *Box)

Set the position of the Box to 1/3 of the size of the inner rectangle of the given container.

func (*Box) SetThirdSize

func (b *Box) SetThirdSize(container *Box)

Set the size of the Box to 1/3 of the size of the inner rectangle of the given container.

type Rect

type Rect struct {
	X int
	Y int
	W int
	H int
}

Rect is a position, width and height

func DrawBox

func DrawBox(r *Box, extrude bool) *Rect

Draw a box using ASCII graphics. The given Box struct defines the size and placement. If extrude is True, the box looks a bit more like it's sticking out.

type ReturnsTwoInts

type ReturnsTwoInts func() (int, int)

Represents a function that takes no arguments and returns two integers

type TextOutput

type TextOutput struct {
	// contains filtered or unexported fields
}

func NewTextOutput

func NewTextOutput(color bool, enabled bool) *TextOutput

func (*TextOutput) DarkBlue

func (o *TextOutput) DarkBlue(s string) string

func (*TextOutput) DarkCyan

func (o *TextOutput) DarkCyan(s string) string

func (*TextOutput) DarkGray

func (o *TextOutput) DarkGray(s string) string

func (*TextOutput) DarkGreen

func (o *TextOutput) DarkGreen(s string) string

func (*TextOutput) DarkPurple

func (o *TextOutput) DarkPurple(s string) string

func (*TextOutput) DarkRed

func (o *TextOutput) DarkRed(s string) string

func (*TextOutput) DarkYellow

func (o *TextOutput) DarkYellow(s string) string

func (*TextOutput) Err

func (o *TextOutput) Err(msg string)

Write an error message in red to stderr if output is enabled

func (*TextOutput) ErrExit

func (o *TextOutput) ErrExit(msg string)

Write an error message to stderr and quit with exit code 1

func (*TextOutput) IsEnabled

func (o *TextOutput) IsEnabled() bool

Checks if textual output is enabled

func (*TextOutput) LightBlue

func (o *TextOutput) LightBlue(s string) string

func (*TextOutput) LightCyan

func (o *TextOutput) LightCyan(s string) string

func (*TextOutput) LightGreen

func (o *TextOutput) LightGreen(s string) string

func (*TextOutput) LightPurple

func (o *TextOutput) LightPurple(s string) string

func (*TextOutput) LightRed

func (o *TextOutput) LightRed(s string) string

func (*TextOutput) LightYellow

func (o *TextOutput) LightYellow(s string) string

func (*TextOutput) Println

func (o *TextOutput) Println(msg string)

Write a message to stdout if output is enabled

func (*TextOutput) White

func (o *TextOutput) White(s string) string

Directories

Path Synopsis
examples
box

Jump to

Keyboard shortcuts

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