diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-12-22 17:45:51 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-12-22 17:45:51 -0800 |
commit | 940b885be755987650ead5cab896af17c71354a9 (patch) | |
tree | d9d07b6f10184985b690d416901b78c96a72df0c | |
parent | c393528cd21dd7695a1fbae778071961da9834fe (diff) | |
download | teliva-940b885be755987650ead5cab896af17c71354a9.tar.gz |
toot-toot: redo cursor_down as an experiment
I want to support cursor movement across wrapped lines, and the old implementation doesn't seem on the right track for that. Interesting that this required me to add the new symmetric test.
-rw-r--r-- | toot-toot.tlv | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/toot-toot.tlv b/toot-toot.tlv index 20b40c7..e56e5ba 100644 --- a/toot-toot.tlv +++ b/toot-toot.tlv @@ -260,16 +260,36 @@ >end - __teliva_timestamp: original cursor_down: - >function cursor_down(s, idx) - > local colidx = col_within_line(s, idx) - > local newidx = skip_past_newline(s, idx) - > while true do - > if s[newidx] == '\n' then break end - > local newcolidx = col_within_line(s, newidx) - > if newcolidx == colidx then break end - > newidx = newidx+1 + >function cursor_down(s, old_idx) + > local colidx = 0 + > local old_colidx = -1 + > for i=1,string.len(s) do + > curses.addstr('|'..i..','..colidx) + > if i == old_idx then + > print('col:', colidx) + > old_colidx = colidx + > elseif colidx == old_colidx then -- next line + > print('->', i) + > return i + > end + > -- loop update + > if s[i] == '\n' then + > if old_colidx ~= -1 and old_colidx > colidx then + > print('=>', i) + > return i + > end + > colidx = 0 + > else + > colidx = colidx+1 + > end + > end + > if old_colidx == colidx then + > print('->', string.len(s)+1) + > return string.len(s)+1 + > else + > print('|>', old_idx) + > return old_idx > end - > return newidx >end > >function test_cursor_down() @@ -280,6 +300,7 @@ > check_eq(cursor_down('abc\ndef', 5), 5, 'cursor_down: bottom line first char') > check_eq(cursor_down('abc\ndef', 6), 6, 'cursor_down: bottom line mid char') > check_eq(cursor_down('abc\ndef', 7), 7, 'cursor_down: bottom line final char') + > check_eq(cursor_down('abc\n\ndef', 2), 5, 'cursor_down: to shorter line') >end - __teliva_timestamp: original skip_past_newline: |