toot-toot: more elaborate cursor_up

This commit is contained in:
Kartik K. Agaram 2021-12-22 20:28:58 -08:00
parent 2c76aa9ff0
commit 7a7a334a5d
1 changed files with 59 additions and 12 deletions

View File

@ -409,19 +409,66 @@
>end >end
- __teliva_timestamp: original - __teliva_timestamp: original
cursor_up: cursor_up:
>function cursor_up(s, idx) >function cursor_up(s, old_idx)
> if idx <= 1 then return idx end > local max = string.len(s)
> -- check column within current line, then go to start of previous line, then count off columns there > local i = 1
> local colidx = col_within_line(s, idx) > -- compute oldcol, the screen column of old_idx
> local newidx = skip_to_start_of_previous_line(s, idx) > local oldcol = 0
> if newidx == idx then return idx end > local col = 0
> if s[newidx] == '\n' then return newidx end > while true do
> for i=2,colidx do -- we're already starting at col 1 > if i > max then
> if newidx >= string.len(s) then break end > -- abnormal old_idx
> if s[newidx] == '\n' then break end > return old_idx
> newidx = newidx+1 > end
> if i == old_idx then
> oldcol = col
> break
> end
> if s[i] == '\n' then
> col = 0
> else
> col = col+1
> end
> i = i+1
> end
> -- find previous newline
> i = i-col-1
> -- scan back to previous line
> if s[i] == '\n' then
> i = i-1
> end
> curses.addstr('c:'..col)
> while true do
> curses.addstr('|'..i)
> if i < 1 then
> -- current line is at top
> break
> end
> if s[i] == '\n' then
> break
> end
> i = i-1
> end
> -- compute index at same column on next line
> -- i is at a newline
> curses.addstr('/'..i)
> i = i+1
> col = 0
> while true do
> if i > max then
> -- next line is at bottom and is too short; position at end of it
> return i
> end
> if s[i] == '\n' then
> -- next line is too short; position at end of it
> return i
> end
> if col == oldcol then
> return i
> end
> col = col+1
> i = i+1
> end > end
> return newidx
>end >end
> >
>function test_cursor_up() >function test_cursor_up()