Skip to content

Commit

Permalink
feat: lines priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat committed Dec 26, 2022
1 parent d802f6a commit 2f50824
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 21 deletions.
12 changes: 12 additions & 0 deletions internal/board/graphics/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ func (tc *TaggedCell) Is(key string, value string) bool {
return false
}
}

func (tc *TaggedCell) Match(tags map[string]string) bool {
if tags == nil {
return false
}
for k, v := range tags {
if tc.Is(k, v) {
return true
}
}
return false
}
21 changes: 9 additions & 12 deletions internal/board/graphics/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,15 @@ func (l *LineStack) add(cell *LinesCell) {
}

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
}
}
if first == nil || last == nil {
return false
}
if hashLines(first) == 0b_0101 && hashLines(last) == 0b_1010 {
// lines have crossed.
return true
} else if hashLines(last) == 0b_0101 && hashLines(first) == 0b_1010 {
// lines have crossed.
return true
}
return false
}
Expand Down
2 changes: 1 addition & 1 deletion internal/board/graphics/lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func TestLineStack_Render_Crossed_2(t *testing.T) {
lineStack.add(&LinesCell{l: true, r: true})
a.Equal('┴', lineStack.Render())
lineStack.add(&LinesCell{t: true, b: true})
a.Equal('', lineStack.Render())
a.Equal('', lineStack.Render())
}
2 changes: 1 addition & 1 deletion internal/board/graphics/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (m *Matrix) Render() string {
rendered := ""
for j := 0; j < m.h; j++ {
for i := 0; i < m.w; i++ {
rendered += string(m.elements[j][i].Render())
rendered += string(m.elements[j][i].Render(nil))
}
rendered += "\n"
}
Expand Down
28 changes: 26 additions & 2 deletions internal/board/graphics/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func (cs *CellStack) Is(key string, value string) bool {
return false
}

func (cs *CellStack) Match(tags map[string]string) bool {
if tags == nil {
return false
}
for k, v := range tags {
if cs.Is(k, v) {
return true
}
}
return false
}

func (cs *CellStack) PlaceChar(char rune) *TaggedCell {
charTaggedCell := NewTaggedCell(CharCell(char))
cs.add(charTaggedCell)
Expand All @@ -56,10 +68,22 @@ var arrowMap = map[bool]rune{
false: '▷',
}

func (cs *CellStack) Render() rune {
func (cs *CellStack) Render(
priorityTags map[string]string,
) rune {
lineStack := LineStack{}
priorityCellStack := *cs

if cs.Match(priorityTags) {
priorityCellStack = CellStack{}
for _, cell := range *cs {
if cell.Match(priorityTags) {
priorityCellStack.add(cell)
}
}
}

for _, taggedCell := range *cs {
for _, taggedCell := range priorityCellStack {
switch cell := taggedCell.Cell.(type) {
case EmptyCell:
// nothing.
Expand Down
20 changes: 17 additions & 3 deletions internal/board/graphics/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@ func TestCellStack_Render_lines(t *testing.T) {
cs.add(NewTaggedCell(&LinesCell{
b: true,
}))
a.Equal('│', cs.Render())
a.Equal('│', cs.Render(nil))
}

func TestCellStack_Render_linesPriority(t *testing.T) {
a := require.New(t)
cs := CellStack{}
cs.add(NewTaggedCell(&LinesCell{
t: true,
r: true,
}))
cs.add(NewTaggedCell(&LinesCell{
l: true,
r: true,
}).WithTag("test", "true"))
a.Equal('─', cs.Render(map[string]string{"test": "true"}))
}

func TestCellStack_Render_charHasPriority(t *testing.T) {
Expand All @@ -25,7 +39,7 @@ func TestCellStack_Render_charHasPriority(t *testing.T) {
t: true,
}))
cs.PlaceChar('a')
a.Equal('a', cs.Render())
a.Equal('a', cs.Render(nil))
}

func TestCellStack_Render_arrowHasPriority(t *testing.T) {
Expand All @@ -35,5 +49,5 @@ func TestCellStack_Render_arrowHasPriority(t *testing.T) {
cs.add(NewTaggedCell(&LinesCell{
t: true,
}))
a.Equal('▷', cs.Render())
a.Equal('▷', cs.Render(nil))
}
2 changes: 1 addition & 1 deletion internal/graph/.graph_test/Simple.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
│ │
├─┼▷2
│ │ │
│ └─▷4◁─┐
│ └─▷4◁─┐
│ │ │ │
└───┴─┴▷3┘
4 changes: 3 additions & 1 deletion internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func Loop(b *board.Board) error {
s.SetContent(
j,
i,
cells[i][j].Render(),
cells[i][j].Render(map[string]string{
"nodeIndex": strconv.Itoa(state.selected),
}),
nil,
style,
)
Expand Down

0 comments on commit 2f50824

Please sign in to comment.