hexes

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: MIT Imports: 5 Imported by: 4

README

hexes

A simple curses alternative written in go. For now, it requires stty and tput to work. Test the examples!

Game of Life in hexes

Usage

package main

import (
	"time"
	"fmt"
	"unicode"
	"os"

	"github.com/hhhhhhhhhn/hexes"
)

func main() {
	duration := 2 * time.Millisecond
	r := hexes.New(os.Stdin, os.Stdout)
	r.SetDefaultAttribute(hexes.NORMAL)
	r.Start()

	for i := 0; i < 10000; i++ {
		for !unicode.IsGraphic(rune(i)) {
			i++
		}
		row := i % r.Rows
		col := (i * 4) % r.Cols

		r.Set(row, col, rune(i))
		time.Sleep(duration)

		if i % 1000 == 0 {
			r.SetAttribute(r.DefaultAttribute)
		}
		if i % 1000 == 200 {
			r.SetAttribute(hexes.RED)
		}
		if i % 1000 == 400 {
			r.SetAttribute(hexes.Join(hexes.BG_CYAN, hexes.WHITE, hexes.BOLD))
		}
		if i % 1000 == 600 {
			r.SetAttribute(hexes.Join(hexes.BG_YELLOW, hexes.RED, hexes.BOLD, hexes.ITALIC))
		}
		if i % 1000 == 800 {
			r.SetAttribute(hexes.REVERSE)
		}
	}
	r.End()
}

Examples

Image viewer in hexes Snake in hexes

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NORMAL     = Attribute("\033[0m")
	BOLD       = Attribute("\033[1m")
	FAINT      = Attribute("\033[2m")
	ITALIC     = Attribute("\033[3m")
	UNDERLINE  = Attribute("\033[4m")
	SLOW_BLINK = Attribute("\033[5m")
	FAST_BLINK = Attribute("\033[6m")
	REVERSE    = Attribute("\033[7m")
	STRIKE     = Attribute("\033[8m")

	BLACK   = Attribute("\033[30m")
	RED     = Attribute("\033[31m")
	GREEN   = Attribute("\033[32m")
	YELLOW  = Attribute("\033[33m")
	BLUE    = Attribute("\033[34m")
	MAGENTA = Attribute("\033[35m")
	CYAN    = Attribute("\033[36m")
	WHITE   = Attribute("\033[37m")

	BG_BLACK   = Attribute("\033[40m")
	BG_RED     = Attribute("\033[41m")
	BG_GREEN   = Attribute("\033[42m")
	BG_YELLOW  = Attribute("\033[43m")
	BG_BLUE    = Attribute("\033[44m")
	BG_MAGENTA = Attribute("\033[45m")
	BG_CYAN    = Attribute("\033[46m")
	BG_WHITE   = Attribute("\033[47m")
)

Functions

This section is empty.

Types

type Attribute added in v0.3.0

type Attribute []byte

Type representing the style of the text being written (e.g. color, font weight). Internally, just command sequences (a.k.a. ANSI escape codes).

func Join added in v0.3.0

func Join(attributes ...Attribute) Attribute

Create an attribute joining the given.

func TrueColor

func TrueColor(red, green, blue int) Attribute

Creates an attribute that sets the foreground to the RGB value.

func TrueColorBg

func TrueColorBg(red, green, blue int) Attribute

Creates an attribute that sets the background to the RGB value.

type Renderer

type Renderer struct {
	Lines            [][]rune      // (RO) The virtual output characters.
	Attributes       [][]Attribute // (RO) The virtual output Attributes.
	Rows             int           // (RO) The amount of rows in virtual output.
	Cols             int           // (RO) The amount of columns in virtual output.
	CursorRow        int           // (RO) The row the cursor is in the terminal.
	CursorCol        int           // (RO) The column the cursor is in the terminal.
	CurrentAttribute Attribute     // (RO) The terminal's current attribute.
	DefaultAttribute Attribute     // (RO) The preferred default attribute of the renderer.
	Out              io.Writer     // (RO) The default writer to send terminal data to.
	In               io.Reader     // (RO) The default reader to get data from.
}

Gives an abstraction to render text in any position in the terminal.

func New

func New(in io.Reader, out io.Writer) *Renderer

Creates a new Renderer using in (e.g. os.Stdin) as input and out (e.g. os.Stdout) as output.

func (*Renderer) End

func (r *Renderer) End()

Restores terminal to default state.

func (*Renderer) MoveCursor

func (r *Renderer) MoveCursor(row, col int)

Moves terminal cursor to a position (0 indexed).

func (*Renderer) NewAttribute added in v0.3.0

func (r *Renderer) NewAttribute(attributes ...Attribute) Attribute

Creates a new attribute based on the default attribute.

func (*Renderer) Refresh

func (r *Renderer) Refresh()

Redraws virtual output to the terminal, handling resizes.

func (*Renderer) Set

func (r *Renderer) Set(row, col int, value rune)

Sets the cell at row, col (0 indexed) to the character given.

func (*Renderer) SetAttribute

func (r *Renderer) SetAttribute(attribute Attribute)

Sets the Attribute of the text being written.

func (*Renderer) SetDefaultAttribute

func (r *Renderer) SetDefaultAttribute(attribute Attribute)

Sets the preferred Attribute with which to prepend new ones.

func (*Renderer) SetString

func (r *Renderer) SetString(row, col int, value string)

Sets the cells starting at row, col (0 indexed) to value, accounting for wide characters.

func (*Renderer) Start

func (r *Renderer) Start()

Initializes and configures the Renderer and user's terminal.

Directories

Path Synopsis
examples
gol

Jump to

Keyboard shortcuts

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