cterm

package module
v0.0.0-...-46801ba Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2014 License: BSD-3-Clause Imports: 1 Imported by: 0

README

cterm

ColorTerm (AKA: cterm) is a simple cross-platform go library for generating color terminal output.

Currently it has been designed and tested to work under: Linux, OSX and Windows.

If there is any interest, it could probably be ported to other operating systems without too much effort.

Documentation

Online documentation is available at: http://godoc.org/bitbucket.org/pseudomind/cterm

Screenshots

cterm example on OSX

The provided cterm example on OSX

cterm example on Windows

The provided cterm example on Windows

Documentation

Overview

cterm

ColorTerm is a library designed to make cross platform colored console output simple.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetColors

func SetColors(fg, bg Color)

SetColors sets the console foreground and background colors to the specified values.

Example
SetColors(PINK, GRAY)
fmt.Printf("Hello world!")
SetColorsSTD()
Output:

�[47m�[95mHello world!�[0m

func SetColorsSTD

func SetColorsSTD()

SetColorsSTD sets the the console foreground and background colors to the platform default values.

Example
SetColors(PINK, GRAY)
fmt.Printf("Here the colors are set to non-default values")
SetColorsSTD()
fmt.Printf("\nThis text uses platform default colors...")
Output:

�[47m�[95mHere the colors are set to non-default values�[0m
This text uses platform default colors...

Types

type Color

type Color int
const (
	// The following colors can be used to specify text or msg background color.
	BLACK   Color = iota
	DBLUE   Color = iota
	DGREEN  Color = iota
	DCYAN   Color = iota
	DRED    Color = iota
	DPINK   Color = iota
	DYELLOW Color = iota
	GRAY    Color = iota
	DGRAY   Color = iota
	BLUE    Color = iota
	GREEN   Color = iota
	CYAN    Color = iota
	RED     Color = iota
	PINK    Color = iota
	YELLOW  Color = iota
	WHITE   Color = iota
)
const (
	// The following standard colors are specific to the operating platform.
	STDBG Color = (Color)(cTRANSP)
	STDFG Color = GRAY
)

type Message

type Message struct {
	FGcolor Color
	BGcolor Color
	Data    string
}

This is the fundamental structure of this library. Each cterm.Message contains a string as well as the color information of both the background and foreground.

The implementation of the associated method Show() is platform specific.

func NewMessage

func NewMessage(msg string, optColors ...Color) Message

Creates a new (potentailly colorful) message using a minimal amount of input.

At minimum, a message can be created by only specifying a the string contents of the Message. When this is done, the default foreground/text color and default background color will be used for the message.

Optionally, the forground/text color and the background color of a Message can be specified by supplying addtional arguments. The first addtional argument (if supplied) will be used as the forground/text color. Similarly, the second addtional argument (if supplied) will be used as the background color. If it is desired to only set the background color, then the color `cterm.STDFG` should be used for the first addtional argument.

Example
msg1 := NewMessage("Text with default colors, ")
msg2 := NewMessage("Text, fg color specified, ", BLUE)
msg3 := NewMessage("Text, fg and bg color specified.", DBLUE, WHITE)
fmt.Println(msg1)
fmt.Println(msg2)
fmt.Println(msg3)
Output:

{7 -1 Text with default colors, }
{9 -1 Text, fg color specified, }
{1 15 Text, fg and bg color specified.}

func (*Message) Show

func (self *Message) Show()

The Show method prints out the contained message. With this method, the color configuration of the console is always set and then set back to platform console defaults.

type MessageBlock

type MessageBlock struct {
	Blocks []Message
}

This struct is simply an array of Messages. Using a message block might be preferable when constructing a complex set of messages. This struct also has the added advantage of providing a single `Show()` method which will print out all of the contained messages in the order they appear in the array.

func NewMessageBlock

func NewMessageBlock(msgs []Message) MessageBlock
Example
mb := NewMessageBlock([]Message{
	NewMessage("msg one,"),
	NewMessage(" msg two [BLUE],", BLUE),
	NewMessage(" msg three [RED]", RED)})

mb.Show()
fmt.Println()
Output:

�[0mmsg one,�[40m�[94m msg two [BLUE],�[40m�[91m msg three [RED]�[0m

func (*MessageBlock) Append

func (self *MessageBlock) Append(msg string, optColors ...Color)

Appends a new (potentailly colorful) message to the end of a MessageBlock using a minimal amount of input.

At minimum, a message can be created by only specifying a the string contents of the Message. When this is done, the default foreground/text color and default background color will be used for the message.

Optionally, the forground/text color and the background color of a Message can be specified by supplying addtional arguments. The first addtional argument (if supplied) will be used as the forground/text color. Similarly, the second addtional argument (if supplied) will be used as the background color. If it is desired to only set the background color, then the color `cterm.STDFG` should be used for the first addtional argument.

Example
var mb MessageBlock

mb.Append("Text with default colors\n")
mb.Append("Text, fg color specified\n", RED)
mb.Append("Text, fg and bg color specified\n", DBLUE, WHITE)
fmt.Println(mb.Blocks[0])
fmt.Println(mb.Blocks[1])
fmt.Println(mb.Blocks[2])
Output:

{7 -1 Text with default colors
}
{12 -1 Text, fg color specified
}
{1 15 Text, fg and bg color specified
}

func (*MessageBlock) AppendBlock

func (self *MessageBlock) AppendBlock(mb MessageBlock)

Appends one message block to the end of another

func (*MessageBlock) Show

func (self *MessageBlock) Show()

The Show method prints out all of the contained messages of a MessageBlock in the order they appear in the internal array.

Example
var mb MessageBlock

mb.Append("Text with default colors\n")

mb.Show()
Output:

�[0mText with default colors

Directories

Path Synopsis
This sample program demonstrates the capabilites of the cterm library.
This sample program demonstrates the capabilites of the cterm library.

Jump to

Keyboard shortcuts

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