diff --git a/internal/board/graphics/cell.go b/internal/board/graphics/cell.go index fc40769..e828004 100644 --- a/internal/board/graphics/cell.go +++ b/internal/board/graphics/cell.go @@ -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 +} diff --git a/internal/board/graphics/lines.go b/internal/board/graphics/lines.go index fe14502..42894db 100644 --- a/internal/board/graphics/lines.go +++ b/internal/board/graphics/lines.go @@ -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 } diff --git a/internal/board/graphics/lines_test.go b/internal/board/graphics/lines_test.go index 31db15a..ad90c26 100644 --- a/internal/board/graphics/lines_test.go +++ b/internal/board/graphics/lines_test.go @@ -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()) } diff --git a/internal/board/graphics/matrix.go b/internal/board/graphics/matrix.go index c89e655..ac3d798 100644 --- a/internal/board/graphics/matrix.go +++ b/internal/board/graphics/matrix.go @@ -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" } diff --git a/internal/board/graphics/stack.go b/internal/board/graphics/stack.go index 21d8658..0f9011a 100644 --- a/internal/board/graphics/stack.go +++ b/internal/board/graphics/stack.go @@ -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) @@ -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. diff --git a/internal/board/graphics/stack_test.go b/internal/board/graphics/stack_test.go index 1fac049..7a6f132 100644 --- a/internal/board/graphics/stack_test.go +++ b/internal/board/graphics/stack_test.go @@ -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) { @@ -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) { @@ -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)) } diff --git a/internal/graph/.graph_test/Simple.txt b/internal/graph/.graph_test/Simple.txt index cec8b6c..a13e05f 100755 --- a/internal/graph/.graph_test/Simple.txt +++ b/internal/graph/.graph_test/Simple.txt @@ -4,6 +4,6 @@ │ │ ├─┼▷2 │ │ │ -│ └─┼▷4◁─┐ +│ └─├▷4◁─┐ │ │ │ │ └───┴─┴▷3┘ diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 1514960..e9af042 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -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, )