about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-12-22 18:44:03 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-12-22 18:44:03 -0800
commit2c76aa9ff02aaecb99021ed12678cc1aa37c6e2a (patch)
treea1bf7daee9ee2bcb476635cdda12359a6cfeb8fb
parent77b464fbf0de32868467cb6cb076ec05aabfe5b3 (diff)
downloadteliva-2c76aa9ff02aaecb99021ed12678cc1aa37c6e2a.tar.gz
toot-toot: more verbose but clearer cursor_down
I actually got all tests to pass on the first try.
-rw-r--r--toot-toot.tlv60
1 files changed, 44 insertions, 16 deletions
diff --git a/toot-toot.tlv b/toot-toot.tlv
index fa841dc..7b59234 100644
--- a/toot-toot.tlv
+++ b/toot-toot.tlv
@@ -261,28 +261,56 @@
 - __teliva_timestamp: original
   cursor_down:
     >function cursor_down(s, old_idx)
-    >  local colidx = 0
-    >  local old_colidx = -1
-    >  for i=1,string.len(s) do
+    >  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
-    >      old_colidx = colidx
-    >    elseif colidx == old_colidx then  -- next line
-    >      return i
+    >      oldcol = col
+    >      break
     >    end
-    >    -- loop update
     >    if s[i] == '\n' then
-    >      if old_colidx ~= -1 and old_colidx > colidx then
-    >        return i
-    >      end
-    >      colidx = 0
+    >      col = 0
     >    else
-    >      colidx = colidx+1
+    >      col = col+1
     >    end
+    >    i = i+1
     >  end
-    >  if old_colidx == colidx then
-    >    return string.len(s)+1
-    >  else
-    >    return old_idx
+    >  -- skip rest of line
+    >  while true do
+    >    if i > max then
+    >      -- current line is at bottom
+    >      return old_idx
+    >    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
+    >  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
     >