term

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: LGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package term provides colors and text formatting for the terminal.

Index

Examples

Constants

View Source
const (
	// HBar is a the Unicode horizontal bar character.
	HBar = "\u2500"
)

Variables

View Source
var ErrRune = errors.New("invalid encoded rune")

Functions

func Alert

func Alert() string

Alert returns the string "Problem:" using the error color.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	fmt.Print(term.Alert(), "something went wrong")
}
Output:

Problem:
something went wrong

func Bool

func Bool(b bool) string

Bool returns a checkmark ✓ when true or a cross ✗ when false.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	fmt.Println(term.Bool(true), "yes")
	fmt.Println(term.Bool(false), "no")
}
Output:

✓ yes
✗ no

func Border

func Border(w io.Writer, s string)

Border wraps the string around a single line border.

Example
package main

import (
	"os"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	term.Border(os.Stdout, "hi")
}
Output:

┌────┐
│ hi │
└────┘

func Center

func Center(width int, s string) string

Center align text to a the width of an area. If the width is less than the length of the string, the string is returned. There is no padding after the string.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	fmt.Print("[" + term.Center(10, "hi") + "]")
}
Output:

[    hi]

func Comment

func Comment(s string) string

Comment returns a string in the comment color.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	fmt.Print(term.Comment("Hi"))
}
Output:

Hi

func Example

func Example(s string) string

Example returns the string using the debug color.

func Fuzzy

func Fuzzy(s string) string

Fuzzy returns a string in the fuzzy color.

func GetEnv

func GetEnv(key string) string

GetEnv gets and formats the value of the environment variable named in the key.

func HR

func HR(w io.Writer, width int)

HR returns a horizontal ruler and a line break.

Example
package main

import (
	"os"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	term.HR(os.Stdout, 8)
}
Output:

────────
func Head(w io.Writer, width int, s string)

Head returns a colored and underlined string for use as a header. Provide a fixed width value for the underline border or set to zero. The header is colored with the fuzzy color.

Example
package main

import (
	"os"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	term.Head(os.Stdout, 10, "heading")
}
Output:

──────────
 heading

func Info

func Info(s string) string

Info returns a string in the info color.

func Inform

func Inform() string

Inform returns "Information:" using the info color.

func Options

func Options(w io.Writer, s string, shorthand, flag bool, opts ...string)

Options writes the string and a sorted list of opts to the writer. If shorthand is true, the options are underlined. If flag is true, the string is prepended with "flag".

Example
package main

import (
	"os"

	"github.com/bengarrett/retrotxtgo/term"
)

func main() {
	term.Options(os.Stdout, "this is a list of options",
		false, false, "option3", "option2", "option1")
}
Output:

this is a list of options.
  Options: option1, option2, option3

func Secondary

func Secondary(s string) string

Secondary returns a string in the secondary color.

func Term

func Term(colorEnv, env string) string

Term determines the terminal type based on the COLORTERM and TERM environment variables.

Possible reply values are: terminal, terminal16, terminal256, terminal16m.

The value terminal is a monochrome terminal.

The value terminal16 is a 4-bit, 16 color terminal.

The value terminal256 is a 8-bit, 256 color terminal.

The value terminal16m is a 24-bit, 16 million color terminal.

func UnderlineChar

func UnderlineChar(s string) (string, error)

UnderlineChar uses ANSI to underline the first character of a string.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/term"
	"github.com/gookit/color"
)

func main() {
	color.Enable = true
	defer func() { color.Enable = false }()
	s, _ := term.UnderlineChar("Z")
	fmt.Print(s)
}
Output:

�[0m�[4mZ�[0m

func UnderlineKeys

func UnderlineKeys(keys ...string) string

UnderlineKeys uses ANSI to underline the first letter of each key.

Types

type Terminal

type Terminal int

Terminal color support.

const (
	TermMono Terminal = iota // monochrome with no color
	Term16                   // ANSI standard 16 color
	Term88                   // XTerm with 88 colors
	Term256                  // XTerm with 256 colors
	Term16M                  // ANSI high-color with 16 million colors
)

func (Terminal) String

func (t Terminal) String() string

String returns the terminal as a named color value.

Jump to

Keyboard shortcuts

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