SiDCo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 3 Imported by: 0

README

SiDCo

Go Report Card Go Reference

SImple Drawing in Console. Not very much features, but that all what sometimes needed.

See example below.

Installation

Install and update this package with go get -u github.com/AlexxSap/SiDCo

Example 1 (for v1.0.0)
package main

import (
	"time"

	canvas "github.com/AlexxSap/SiDCo"
)

func drawSample(cnv *canvas.Canvas, isHorizontal bool) {

	cnv.ClearInner()

	lineSize, columnSize := cnv.Size().Line, cnv.Size().Column

	if isHorizontal {
		points1, points2 := make([]canvas.Point, 0), make([]canvas.Point, 0)
		for i := 1; i <= columnSize; i++ {
			points1 = append(points1, canvas.Point{1, i})
			points2 = append(points2, canvas.Point{lineSize - 1, i})
		}
		cnv.DrawPath("*", points1)
		cnv.DrawPath("+", points2)

	} else {

		points1, points2 := make([]canvas.Point, 0), make([]canvas.Point, 0)
		for i := 1; i < lineSize; i++ {
			points1 = append(points1, canvas.Point{i, 1})
			points2 = append(points2, canvas.Point{i, columnSize - 1})
		}

		cnv.DrawPath("^", points1)
		cnv.DrawPath("#", points2)
	}
}

func main() {

	cnv1, err := canvas.NewCanvas(canvas.Point{1, 1}, canvas.Point{10, 20})
	if err != nil {
		panic(err)
	}
	cnv1.DrawBoxWithTitle("Simple box")

	cnv2, err := canvas.NewCanvas(canvas.Point{1, 25}, canvas.Point{5, 30})
	if err != nil {
		panic(err)
	}
	cnv2.DrawColoredBoxWithTitle("Colored box", canvas.ColorRed, canvas.ColorGreen)

	for i := 0; i < 5; i++ {
		drawSample(&cnv1, i%2 == 0)
		drawSample(&cnv2, i%2 != 0)

		cnv2.DrawColoredText("constant text", canvas.Point{2, 3}, canvas.ColorYellow)

		time.Sleep(1 * time.Second)
	}
}


sample2

Example 2

Game snake - https://github.com/AlexxSap/snake

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearScreen added in v1.1.0

func ClearScreen()

ClearScreen clear all terminal screen. Need invoke this method before draw.

Types

type Canvas

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

Canvas is the canvas for drawing

func NewCanvas

func NewCanvas(start, size Point) (Canvas, error)

NewCanvas create canvas with custom start point and size

func (Canvas) ClearInner

func (cnv Canvas) ClearInner()

ClearInner clear all in the box

func (Canvas) DrawBoxWithTitle

func (cnv Canvas) DrawBoxWithTitle(title string) error

DrawBoxWithTitle draw box around canvas with some title

func (Canvas) DrawColoredBoxWithTitle

func (cnv Canvas) DrawColoredBoxWithTitle(title string, boxColor Color, textColor Color) error

DrawColoredBoxWithTitle draw box with spicified color around canvas with some colored title

func (Canvas) DrawColoredText

func (cnv Canvas) DrawColoredText(text string, point Point, color Color)

DrawColoredText print colored text on specified position

func (Canvas) DrawPath

func (cnv Canvas) DrawPath(sym string, points []Point)

DrawPath draw symbol 'sym' in point 'points'

func (Canvas) DrawSource added in v1.1.0

func (cnv Canvas) DrawSource(pp PointProvider)

DrawSource draw symbol on position provided by PointProvider

func (Canvas) DrawText

func (cnv Canvas) DrawText(text string, point Point)

DrawText print text on specified position

func (Canvas) EndDraw added in v1.1.0

func (cnv Canvas) EndDraw()

EndDraw invoke when the drawing is end and need to put cursor pos to the bottom of screen

func (Canvas) SetColor added in v1.1.0

func (cnv Canvas) SetColor(c Color)

SetColor set specified color for text

func (Canvas) SetDefaultColor added in v1.1.0

func (cnv Canvas) SetDefaultColor()

SetDefaultColor reset color to default

func (Canvas) Size

func (cnv Canvas) Size() Point

Size resurn size of box

type Color

type Color int
const (
	ColorReset Color = iota
	ColorBlack
	ColorRed
	ColorGreen
	ColorYellow
	ColorBlue
	ColorMagenta
	ColorCyan
	ColorWhite
)

func (Color) String

func (color Color) String() string

type Point

type Point struct {
	Line, Column int
}

Point represents position in console

type PointProvider added in v1.1.0

type PointProvider interface {
	Provide() (string, int, int)
}

PointProvider interface for provide point and symbol for draw

Jump to

Keyboard shortcuts

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