Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 73 additions & 23 deletions adia/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def extendtop(self, addrows):
def __str__(self):
return '\n'.join(str(line) for line in self._backend) + '\n'

def get_char(self, col, row):
if col >= self.cols:
self.extendright((col + 1) - self.cols)

if row >= self.rows:
self.extendbottom((row + 1) - self.rows)

return self._backend[row][col]

def set_char(self, col, row, char):
if col >= self.cols:
self.extendright((col + 1) - self.cols)
Expand Down Expand Up @@ -143,26 +152,67 @@ def draw_bottomarrow(self, col, row, length, **kw):
self.draw_vline(col, row, length - 1, **kw)
self.set_char(col, row + length - 1, 'v')

def route(self, col1, row1, col2, row2):
overlap = False

try:
for i in self._backend[row1][col1:col2 + 1]:
if i == '-' or '|':
overlap = True
break

except IndexError:
overlap = False

if not overlap:
self.draw_hline(col1, row1, col2 - col1)
self.draw_vline(col2, row1 + 1, row2 - row1 - 2)
self.set_char(col2, row2 - 1, 'v')
self.set_char(col2, row1, '+')

else:
self.draw_vline(col1, row1, (row2 - row1))
self.set_char(col1, row2, '+')
self.draw_hline(col1 + 1, row2, col2 - col1 - 1)
self.set_char(col2 - 1, row2, '>')
def route(self, startcol, startrow, endcol, endrow):
path = [(startcol, startrow)]

def can(col, row):
return self.get_char(col, row) == ' ' and \
(col, row) not in path

def try_(col, row):
if can(col, row):
path.append((col, row))
return True

return False

def walk():
col, row = path[-1]
done = False

if col == endcol - 1 and row == endrow:
return

if col == endcol - 1:
intrested_hdir = 0
else:
intrested_hdir = 1 if col < endcol else -1

if row == endrow:
intrested_vdir = 0
else:
intrested_vdir = 1 if row < endrow else -1

try:
# from pudb import set_trace; col > 16 and set_trace()
# Try horizontal
if intrested_hdir:
if try_(col + intrested_hdir, row):
return
elif try_(col, row + intrested_vdir):
return
elif try_(col, row - 1):
return
elif try_(col, row + 1):
return

# Try vertical
elif intrested_vdir:
if try_(col, row + intrested_vdir):
return
elif try_(col - 1, row):
return
elif try_(col + 1, row):
return

else:
done = True

finally:
if not done:
walk()

walk()
path.append((endcol, endrow))
for col, row in path:
self.set_char(col, row, '*')
36 changes: 23 additions & 13 deletions tests/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,30 @@ def test_canvas_drawarrowtext():
''')


def test_canvas_get_char():
c = Canvas()
assert c.get_char(0, 0) == ' '
assert c.get_char(1, 1) == ' '
c.set_char(1, 1, 'x')
assert c.get_char(1, 1) == 'x'


def test_canvas_route_horizontal():
c = Canvas()
c.route(2, 3, 7, 8)
c.route(2, 4, 7, 8)
c.draw_box(2, 2, 5, 5)
c.draw_box(10, 2, 5, 5)
c.draw_box(18, 2, 5, 5)
c.route(6, 4, 18, 4)
assert eqdia(str(c), '''
............
. .
. .
. .
. -----+ .
. | | .
. | | .
. | | .
. | v .
. +---> .
............
...........
. .
. .
. .
. .
. .
. .
. .
. .
. .
...........
''')