forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2015 Zack Guo <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a MIT license that can | ||
// be found in the LICENSE file. | ||
|
||
package termui | ||
|
||
/* | ||
dots: | ||
,___, | ||
|1 4| | ||
|2 5| | ||
|3 6| | ||
|7 8| | ||
````` | ||
*/ | ||
|
||
var brailleBase = '\u2800' | ||
|
||
var brailleOftMap = [4][2]rune{ | ||
{'\u0001', '\u0008'}, | ||
{'\u0002', '\u0010'}, | ||
{'\u0004', '\u0020'}, | ||
{'\u0040', '\u0080'}} | ||
|
||
// Canvas contains drawing map: i,j -> rune | ||
type Canvas map[[2]int]rune | ||
|
||
// NewCanvas returns an empty Canvas | ||
func NewCanvas() Canvas { | ||
return make(map[[2]int]rune) | ||
} | ||
|
||
func chOft(x, y int) rune { | ||
return brailleOftMap[y%4][x%2] | ||
} | ||
|
||
func (c Canvas) rawCh(x, y int) rune { | ||
if ch, ok := c[[2]int{x, y}]; ok { | ||
return ch | ||
} | ||
return '\u0000' //brailleOffset | ||
} | ||
|
||
// return coordinate in terminal | ||
func chPos(x, y int) (int, int) { | ||
return y / 4, x / 2 | ||
} | ||
|
||
// Set sets a point (x,y) in the virtual coordinate | ||
func (c Canvas) Set(x, y int) { | ||
i, j := chPos(x, y) | ||
ch := c.rawCh(i, j) | ||
ch |= chOft(x, y) | ||
c[[2]int{i, j}] = ch | ||
} | ||
|
||
// Unset removes point (x,y) | ||
func (c Canvas) Unset(x, y int) { | ||
i, j := chPos(x, y) | ||
ch := c.rawCh(i, j) | ||
ch &= ^chOft(x, y) | ||
c[[2]int{i, j}] = ch | ||
} | ||
|
||
// Buffer returns un-styled points | ||
func (c Canvas) Buffer() []Point { | ||
ps := make([]Point, len(c)) | ||
i := 0 | ||
for k, v := range c { | ||
ps[i] = newPoint(v+brailleBase, k[0], k[1]) | ||
i++ | ||
} | ||
return ps | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package termui | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
) | ||
|
||
func TestCanvasSet(t *testing.T) { | ||
c := NewCanvas() | ||
c.Set(0, 0) | ||
c.Set(0, 1) | ||
c.Set(0, 2) | ||
c.Set(0, 3) | ||
c.Set(1, 3) | ||
c.Set(2, 3) | ||
c.Set(3, 3) | ||
c.Set(4, 3) | ||
c.Set(5, 3) | ||
spew.Dump(c) | ||
} | ||
|
||
func TestCanvasUnset(t *testing.T) { | ||
c := NewCanvas() | ||
c.Set(0, 0) | ||
c.Set(0, 1) | ||
c.Set(0, 2) | ||
c.Unset(0, 2) | ||
spew.Dump(c) | ||
c.Unset(0, 3) | ||
spew.Dump(c) | ||
} | ||
|
||
func TestCanvasBuffer(t *testing.T) { | ||
c := NewCanvas() | ||
c.Set(0, 0) | ||
c.Set(0, 1) | ||
c.Set(0, 2) | ||
c.Set(0, 3) | ||
c.Set(1, 3) | ||
c.Set(2, 3) | ||
c.Set(3, 3) | ||
c.Set(4, 3) | ||
c.Set(5, 3) | ||
c.Set(6, 3) | ||
c.Set(7, 2) | ||
c.Set(8, 1) | ||
c.Set(9, 0) | ||
bufs := c.Buffer() | ||
rs := make([]rune, len(bufs)) | ||
for i, v := range bufs { | ||
rs[i] = v.Ch | ||
} | ||
spew.Dump(string(rs)) | ||
} |