Skip to content

Commit

Permalink
fix editor cursor/split bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Mar 7, 2020
1 parent 48e8882 commit 5e31d6e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
18 changes: 13 additions & 5 deletions userland/top.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,31 @@ async def main(cx):
ev = await cx.events.poll('resize')

if ev:
cx.echo(repr(ev) + '\r\n')
cx.echo('\r\n{}\r\n'.format(ev))
await cx.events.flush('resize')

ks = await cx.term.inkey(1)

if ks.code == cx.term.KEY_UP:
dirty = True
cx.echo(cx.term.bold_red('UP!\r\n'))
cx.echo(cx.term.bold_red('\r\nUP!\r\n'))
cx.echo('{}\r\n'.format(await cx.gosub('retval')))

elif ks.code == cx.term.KEY_DOWN:
dirty = True
cx.echo(cx.term.bold_red('DOWN!\r\n'))
cx.echo(cx.term.bold_red('\r\nDOWN!\r\n'))
await cx.gosub('down', 1, arg2='adsf')

elif ks.code == cx.term.KEY_ESCAPE:
dirty = True
cx.echo(cx.term.bold_red('ESCAPE!\r\n'))
cx.echo(cx.term.bold_red('\r\nESCAPE!\r\n'))

return

elif ks.code == cx.term.KEY_ENTER:
dirty = True
cx.echo('\r\n{}\r\n'.format(led.value[0]))

else:
cx.echo(led.process_keystroke(ks))
cx.echo(led.process_keystroke(ks) + cx.term.move_x(0) +
led.redraw_cursor())
74 changes: 56 additions & 18 deletions xthulu/ui/editors.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ def color(self, val):
self._color_str = val
self._color = getattr(self.term, val)

def redraw(self):
"Output sequence to redraw editor"
def redraw(self, redraw_cursor=True):
"""
Output sequence to redraw editor
:param bool redraw_cursor: Redraw cursor position as well
"""

out = ''
lenval = len(self.value)
longest = 0

for i in range(self.rows):
if i > 0:
Expand All @@ -64,7 +69,21 @@ def redraw(self):

if i < lenval:
out += self.term.move_left() * self.width
out += self._color(self.value[i][-self.width:])
text = self.value[i][-self.width:]
textlen = len(text)
out += self._color(text)

if textlen > longest:
longest = textlen

if redraw_cursor:
if self.rows > 1:
out += self.term.move_up() * self.rows

if longest > 0:
out += self.term.move_left() * (longest + 1)

out += self.redraw_cursor()

return out

Expand All @@ -74,8 +93,15 @@ def redraw_cursor(self):
located at top-left of editor
"""

return (self.term.move_down(self.pos[0]) +
self.term.move_right(self.pos[1]))
out = ''

if self.pos[0] > 0:
out += self.term.move_down() * self.pos[0]

if self.pos[1] > 0:
out += self.term.move_right() * self.pos[1]

return out

def process_keystroke(self, ks):
"Process keystroke and produce output (if any)"
Expand All @@ -84,37 +110,49 @@ def process_keystroke(self, ks):
before = row[:self.pos[1]]
after = row[self.pos[1]:]

# TODO wrap text, wrap cursor, up/down nav
# TODO wrap text/cursor, overflow, up/down, home, end, insert mode,
# tab, length limit enforcement
if ks.code == self.term.KEY_BACKSPACE:
if self.pos[1] == 0:
return ''

self.value[self.pos[0]] = before[:-1] + after
self.pos[1] -= 1

if self.pos[1] > 0:
self.pos[1] -= 1
return self._color(self.term.move_left() + after + ' ' +
(self.term.move_left() * (len(after) + 1)))

return self._color(self.term.move_left() + after + ' ' +
(self.term.move_left() * (len(after) + 1)))
elif ks.code == self.term.KEY_DELETE:
if self.pos[1] >= len(self.value[self.pos[0]]):
return ''

after = after[1:]
self.value[self.pos[0]] = before + after

return self._color(after + ' ' +
(self.term.move_left() * (len(after) + 1)))

elif ks.code == self.term.KEY_LEFT:
if self.pos[1] > 0:
self.pos[1] -= 1
if self.pos[1] == 0:
return ''

self.pos[1] -= 1

return self.term.move_left()

return self.term.move_left()
elif ks.code == self.term.KEY_RIGHT:
if self.pos[1] < len(self.value[self.pos[0]]) - 1:
self.pos[1] += 1
if self.pos[1] >= len(self.value[self.pos[0]]):
return ''

self.pos[1] += 1

return self.term.move_right()

return self.term.move_right()
elif ks.is_sequence:
return
return ''

ucs = str(ks)

# TODO handle tab
if not self.term.length(ucs):
return ''

Expand Down

0 comments on commit 5e31d6e

Please sign in to comment.