about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-12-22 20:28:58 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-12-22 20:28:58 -0800
commit7a7a334a5d7990d8bec6e0d0ad0b1a5f52e579ae (patch)
tree3365677c94b16a7b8f4ff8d19d2e7e3e7beb7a58
parent2c76aa9ff02aaecb99021ed12678cc1aa37c6e2a (diff)
downloadteliva-7a7a334a5d7990d8bec6e0d0ad0b1a5f52e579ae.tar.gz
toot-toot: more elaborate cursor_up
-rw-r--r--toot-toot.tlv71
1 files changed, 59 insertions, 12 deletions
diff --git a/toot-toot.tlv b/toot-toot.tlv
index 7b59234..332ed92 100644
--- a/toot-toot.tlv
+++ b/toot-toot.tlv
@@ -409,19 +409,66 @@
     >end
 - __teliva_timestamp: original
   cursor_up:
-    >function cursor_up(s, idx)
-    >  if idx <= 1 then return idx end
-    >  -- check column within current line, then go to start of previous line, then count off columns there
-    >  local colidx = col_within_line(s, idx)
-    >  local newidx = skip_to_start_of_previous_line(s, idx)
-    >  if newidx == idx then return idx end
-    >  if s[newidx] == '\n' then return newidx end
-    >  for i=2,colidx do  -- we're already starting at col 1
-    >    if newidx >= string.len(s) then break end
-    >    if s[newidx] == '\n' then break end
-    >    newidx = newidx+1
+    >function cursor_up(s, old_idx)
+    >  local max = string.len(s)
+    >  local i = 1
+    >  -- compute oldcol, the screen column of old_idx
+    >  local oldcol = 0
+    >  local col = 0
+    >  while true do
+    >    if i > max then
+    >      -- abnormal old_idx
+    >      return old_idx
+    >    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
-    >  return newidx
     >end
     >
     >function test_cursor_up()