-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: better way of rendering cells
- Loading branch information
Gabriel Musat
committed
Dec 26, 2022
1 parent
205c854
commit d802f6a
Showing
12 changed files
with
258 additions
and
215 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
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
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
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
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
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
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,88 @@ | ||
package graphics | ||
|
||
var lineCharMap = map[int]rune{ | ||
0b_0000: ' ', | ||
0b_0001: '╷', | ||
0b_0010: '╶', | ||
0b_0011: '┌', | ||
0b_0100: '╵', | ||
0b_0101: '│', | ||
0b_0110: '└', | ||
0b_0111: '├', | ||
0b_1000: '╴', | ||
0b_1001: '┐', | ||
0b_1010: '─', | ||
0b_1011: '┬', | ||
0b_1100: '┘', | ||
0b_1101: '┤', | ||
0b_1110: '┴', | ||
0b_1111: '┼', | ||
} | ||
|
||
func hashLines(lines *LinesCell) int { | ||
result := 0b_0000 | ||
if lines == nil { | ||
return result | ||
} | ||
if lines.l { | ||
result += 0b_1000 | ||
} | ||
if lines.t { | ||
result += 0b_0100 | ||
} | ||
if lines.r { | ||
result += 0b_0010 | ||
} | ||
if lines.b { | ||
result += 0b_0001 | ||
} | ||
return result | ||
} | ||
|
||
type LineStack []*LinesCell | ||
|
||
func (l *LineStack) add(cell *LinesCell) { | ||
*l = append(*l, cell) | ||
} | ||
|
||
func areCrossing(first *LinesCell, last *LinesCell) bool { | ||
if first != nil && last != nil { | ||
if last.l && last.r && !last.t && !last.b { | ||
if !first.l && !first.r && first.t && first.b { | ||
// lines have crossed. | ||
return true | ||
} | ||
} else if !last.l && !last.r && last.t && last.b { | ||
if first.l && first.r && !first.t && !first.b { | ||
// lines have crossed. | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (l *LineStack) Render() rune { | ||
acc := LinesCell{} | ||
var prev *LinesCell | ||
for _, cell := range *l { | ||
// TODO: honestly, there should be a more elegant way of handling crossed lines. | ||
if areCrossing(prev, cell) { | ||
acc = LinesCell{} | ||
} | ||
if cell.l { | ||
acc.l = true | ||
} | ||
if cell.t { | ||
acc.t = true | ||
} | ||
if cell.r { | ||
acc.r = true | ||
} | ||
if cell.b { | ||
acc.b = true | ||
} | ||
prev = cell | ||
} | ||
return lineCharMap[hashLines(&acc)] | ||
} |
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,38 @@ | ||
package graphics | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLineStack_Render(t *testing.T) { | ||
a := require.New(t) | ||
lineStack := LineStack{} | ||
lineStack.add(&LinesCell{t: true, b: true}) | ||
a.Equal('│', lineStack.Render()) | ||
lineStack.add(&LinesCell{b: true}) | ||
a.Equal('│', lineStack.Render()) | ||
lineStack.add(&LinesCell{l: true}) | ||
a.Equal('┤', lineStack.Render()) | ||
} | ||
|
||
func TestLineStack_Render_Crossed(t *testing.T) { | ||
a := require.New(t) | ||
lineStack := LineStack{} | ||
lineStack.add(&LinesCell{t: true, b: true}) | ||
lineStack.add(&LinesCell{l: true, r: true}) | ||
a.Equal('─', lineStack.Render()) | ||
lineStack.add(&LinesCell{t: true, b: true}) | ||
a.Equal('│', lineStack.Render()) | ||
} | ||
|
||
func TestLineStack_Render_Crossed_2(t *testing.T) { | ||
a := require.New(t) | ||
lineStack := LineStack{} | ||
lineStack.add(&LinesCell{t: true, r: true}) | ||
lineStack.add(&LinesCell{l: true, r: true}) | ||
a.Equal('┴', lineStack.Render()) | ||
lineStack.add(&LinesCell{t: true, b: true}) | ||
a.Equal('┼', lineStack.Render()) | ||
} |
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
Oops, something went wrong.