mirror of https://github.com/gizak/termui.git
Readd drawille-go into repo
This commit is contained in:
parent
5b80e0d4fe
commit
f08e81d72a
|
@ -3,7 +3,7 @@ package termui
|
|||
import (
|
||||
"image"
|
||||
|
||||
drawille "github.com/cjbassi/drawille-go"
|
||||
"github.com/gizak/termui/v3/drawille"
|
||||
)
|
||||
|
||||
type Canvas struct {
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
package drawille
|
||||
|
||||
import (
|
||||
"image"
|
||||
)
|
||||
|
||||
const BRAILLE_OFFSET = '\u2800'
|
||||
|
||||
var BRAILLE = [4][2]rune{
|
||||
{'\u0001', '\u0008'},
|
||||
{'\u0002', '\u0010'},
|
||||
{'\u0004', '\u0020'},
|
||||
{'\u0040', '\u0080'},
|
||||
}
|
||||
|
||||
type Color int
|
||||
|
||||
type Cell struct {
|
||||
Rune rune
|
||||
Color Color
|
||||
}
|
||||
|
||||
type Canvas struct {
|
||||
CellMap map[image.Point]Cell
|
||||
}
|
||||
|
||||
func NewCanvas() *Canvas {
|
||||
return &Canvas{
|
||||
CellMap: make(map[image.Point]Cell),
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Canvas) SetPoint(p image.Point, color Color) {
|
||||
point := image.Pt(p.X/2, p.Y/4)
|
||||
self.CellMap[point] = Cell{
|
||||
self.CellMap[point].Rune | BRAILLE[p.Y%4][p.X%2],
|
||||
color,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Canvas) SetLine(p0, p1 image.Point, color Color) {
|
||||
for _, p := range line(p0, p1) {
|
||||
self.SetPoint(p, color)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Canvas) GetCells() map[image.Point]Cell {
|
||||
cellMap := make(map[image.Point]Cell)
|
||||
for point, cell := range self.CellMap {
|
||||
cellMap[point] = Cell{cell.Rune + BRAILLE_OFFSET, cell.Color}
|
||||
}
|
||||
return cellMap
|
||||
}
|
||||
|
||||
func line(p0, p1 image.Point) []image.Point {
|
||||
points := []image.Point{}
|
||||
|
||||
leftPoint, rightPoint := p0, p1
|
||||
if leftPoint.X > rightPoint.X {
|
||||
leftPoint, rightPoint = rightPoint, leftPoint
|
||||
}
|
||||
|
||||
xDistance := absInt(leftPoint.X - rightPoint.X)
|
||||
yDistance := absInt(leftPoint.Y - rightPoint.Y)
|
||||
slope := float64(yDistance) / float64(xDistance)
|
||||
slopeSign := 1
|
||||
if rightPoint.Y < leftPoint.Y {
|
||||
slopeSign = -1
|
||||
}
|
||||
|
||||
targetYCoordinate := float64(leftPoint.Y)
|
||||
currentYCoordinate := leftPoint.Y
|
||||
for i := leftPoint.X; i < rightPoint.X; i++ {
|
||||
points = append(points, image.Pt(i, currentYCoordinate))
|
||||
targetYCoordinate += (slope * float64(slopeSign))
|
||||
for currentYCoordinate != int(targetYCoordinate) {
|
||||
points = append(points, image.Pt(i, currentYCoordinate))
|
||||
currentYCoordinate += slopeSign
|
||||
}
|
||||
}
|
||||
|
||||
return points
|
||||
}
|
||||
|
||||
func absInt(x int) int {
|
||||
if x >= 0 {
|
||||
return x
|
||||
}
|
||||
return -x
|
||||
}
|
2
go.mod
2
go.mod
|
@ -1,9 +1,7 @@
|
|||
module github.com/gizak/termui/v3
|
||||
|
||||
require (
|
||||
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd
|
||||
github.com/mattn/go-runewidth v0.0.2
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7
|
||||
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
|
||||
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 // indirect
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,10 +1,6 @@
|
|||
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd h1:XtfPmj9tQRilnrEmI1HjQhxXWRhEM+m8CACtaMJE/kM=
|
||||
github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd/go.mod h1:vjcQJUZJYD3MeVGhtZXSMnCHfUNZxsyYzJt90eCYxK4=
|
||||
github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o=
|
||||
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
|
||||
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
|
||||
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
|
||||
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 h1:Pn8fQdvx+z1avAi7fdM2kRYWQNxGlavNDSyzrQg2SsU=
|
||||
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
|
||||
|
|
Loading…
Reference in New Issue