From f08e81d72a4a3cb0e4fa08b323b0ce664a463f1d Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Tue, 18 Jun 2019 12:30:44 -0700 Subject: [PATCH] Readd drawille-go into repo --- canvas.go | 2 +- drawille/drawille.go | 90 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 - go.sum | 4 -- 4 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 drawille/drawille.go diff --git a/canvas.go b/canvas.go index 31d713e..9001d98 100644 --- a/canvas.go +++ b/canvas.go @@ -3,7 +3,7 @@ package termui import ( "image" - drawille "github.com/cjbassi/drawille-go" + "github.com/gizak/termui/v3/drawille" ) type Canvas struct { diff --git a/drawille/drawille.go b/drawille/drawille.go new file mode 100644 index 0000000..11401a3 --- /dev/null +++ b/drawille/drawille.go @@ -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 +} diff --git a/go.mod b/go.mod index ef0b95f..69dd6e2 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index 121df84..bda2a1a 100644 --- a/go.sum +++ b/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=