Skip to content

Commit

Permalink
chore: better reverse dependency rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat committed Dec 12, 2022
1 parent c950380 commit aceba27
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 26 deletions.
11 changes: 11 additions & 0 deletions internal/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ func TestBoard(t *testing.T) {
{from: 0, to: []int{1, 2, 3}},
},
},
{
Name: "Reverse dep with first node very long",
Blocks: []TestBlock{
{name: "some-really-long-file", x: 0, y: 0},
{name: "b", x: 3, y: 1},
},
Connections: []TestConnection{
{from: 0, to: []int{1}},
{from: 1, to: []int{0}},
},
},
}

for _, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions internal/board/board_test/ReverseDeps.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
a◁┐
b
a◁
b
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some-really-long-file◁
└─▷b─────────────────┘
33 changes: 24 additions & 9 deletions internal/board/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package board
import (
"fmt"

graphics2 "dep-tree/internal/graphics"
"dep-tree/internal/graphics"
"dep-tree/internal/utils"
"dep-tree/internal/vector"
)

const (
Expand All @@ -16,7 +17,8 @@ type Connector struct {
to *Block
}

func (c *Connector) Render(matrix *graphics2.Matrix) error {
//nolint:gocyclo
func (c *Connector) Render(matrix *graphics.Matrix) error { // TODO: factor this function out.
reverseX := c.to.Position.X < c.from.Position.X
reverseY := c.to.Position.Y < c.from.Position.Y

Expand All @@ -29,9 +31,16 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error {
}

// 2. start with just one vertical step.
tracer := graphics2.NewLineTracer(from)

cur := tracer.MoveVertical(reverseY)
tracer := graphics.NewLineTracer(from)
var cur vector.Vector
if reverseY {
cur = tracer.MoveHorizontal(false)
if matrix.Cell(cur) == nil {
matrix.ExpandRight(1)
}
} else {
cur = tracer.MoveVertical(false)
}
cell := matrix.Cell(cur)
if cell.Is(cellType, block) || cell.Is(cellType, arrow) {
return fmt.Errorf("could not draw first vertical step on (%d, %d) because there is no space", cur.X, cur.Y)
Expand Down Expand Up @@ -60,14 +69,18 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error {
}
cur = tracer.MoveHorizontal(!reverseX)
cell := matrix.Cell(cur)
if cell == nil && reverseX {
matrix.ExpandRight(1)
cell = matrix.Cell(cur)
}
if cell == nil {
return fmt.Errorf("moved to invalid position (%d, %d) while tracing horizontal line", cur.X, cur.Y)
}
cell.Tag(noCrossOwnership, c.from.Id)
}

// 3. displacing vertically until aligned...
for cur.Y != c.to.Position.Y {
for cur.Y != c.to.Position.Y && cur.Y >= 0 && cur.Y < matrix.H() {
cur = tracer.MoveVertical(reverseY)
matrix.Cell(cur).Tag(noCrossOwnership, c.from.Id)
}
Expand All @@ -77,7 +90,7 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error {
if reverseX {
stopBefore = -len(c.to.Label)
}
for cur.X != c.to.Position.X-stopBefore {
for cur.X != c.to.Position.X-stopBefore && cur.X >= 0 && cur.X < matrix.W() {
cur = tracer.MoveHorizontal(reverseX)
}
err := tracer.Dump(matrix)
Expand All @@ -87,8 +100,10 @@ func (c *Connector) Render(matrix *graphics2.Matrix) error {

// 5. placing arrow in target node...
cell = matrix.Cell(cur)
cell.PlaceArrow(reverseX)
cell.Tag(cellType, arrow)
if cell != nil {
cell.PlaceArrow(reverseX)
cell.Tag(cellType, arrow)
}
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions internal/graph/graph_test/Simple.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
0
├▷1
├─┼▷2
│ └─┼▷4◁┐
└───┴─┴▷3
0
├▷1
├─┼▷2
│ └─┼▷4◁
└───┴─┴▷3
17 changes: 17 additions & 0 deletions internal/graphics/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ func NewMatrix(w int, h int) *Matrix {
}
}

func (m *Matrix) H() int {
return m.h
}

func (m *Matrix) W() int {
return m.w
}

func (m *Matrix) ExpandRight(n int) {
for row := range m.elements {
for i := 0; i < n; i++ {
m.elements[row] = append(m.elements[row], CellStack{})
}
}
m.w += n
}

func (m *Matrix) Cell(v vector.Vector) *CellStack {
if v.Y >= 0 && v.X >= 0 && v.Y < len(m.elements) && v.X < len(m.elements[v.Y]) {
return &m.elements[v.Y][v.X]
Expand Down
17 changes: 7 additions & 10 deletions internal/graphics/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package graphics

import (
"errors"
"fmt"

"dep-tree/internal/utils"
"dep-tree/internal/vector"
Expand Down Expand Up @@ -43,10 +42,6 @@ func (l *LineTracer) Dump(matrix *Matrix) error {
if fromTo.X != 0 && fromTo.Y != 0 {
return errors.New("cannot draw diagonal lines")
}
startCellStack := matrix.Cell(from)
if startCellStack == nil {
return fmt.Errorf("could not trace line in (%d, %d)", from.X, from.Y)
}
lines := Lines{
l: fromTo.X < 0,
r: fromTo.X > 0,
Expand All @@ -56,23 +51,25 @@ func (l *LineTracer) Dump(matrix *Matrix) error {

if lastCell == nil {
lastCell = LinesCell(lines)
startCellStack.add(lastCell)
startCellStack := matrix.Cell(from)
if startCellStack != nil {
startCellStack.add(lastCell)
}
} else {
lastCell.mergeLines(lines)
}

endCellStack := matrix.Cell(to)
if endCellStack == nil {
return fmt.Errorf("could not trace line in (%d, %d)", to.X, to.Y)
}
newCell := LinesCell(Lines{
l: fromTo.X > 0,
r: fromTo.X < 0,
t: fromTo.Y > 0,
b: fromTo.Y < 0,
})

endCellStack.add(newCell)
if endCellStack != nil {
endCellStack.add(newCell)
}
lastCell = newCell
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/utils/inlimits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package utils

func InLimits(i int, arr []any) bool {
return i >= 0 && i < len(arr)
}

0 comments on commit aceba27

Please sign in to comment.