v0.8.0: add cursor position recovery — if text exists but pos is 0, place cursor at end

position-cursor now also clamps cursor-line to valid range and
recovers from cursor-pos resets by moving to end of text.
This commit is contained in:
2026-05-18 14:43:30 -04:00
parent 46cac554ab
commit a65374e120

View File

@@ -343,11 +343,7 @@ and current sidebar mode (:auto/:visible/:hidden)."
(setf (st :dirty) (list nil nil nil))))
(defun position-cursor (fb w h)
"Draw cursor at the input insertion point using reverse video (Emacs-style).
The character under the cursor is redrawn with foreground and background
swapped. If the cursor is past the end of the input string, a reversed
space is drawn."
"Draw cursor at the input insertion point using reverse video (Emacs-style)."
(let* ((sw (if (sidebar-visible-p w) (or (st :sidebar-width) 42) 0))
(cw (- w sw))
(hpad 2)
@@ -358,12 +354,18 @@ and current sidebar mode (:auto/:visible/:hidden)."
(lines (cl-tty.box:word-wrap text prompt-w))
(n-lines (max 1 (length lines)))
(cl 0) (cc 0) (accum 0))
;; Find which wrapped line the cursor is on
;; Find which wrapped line the cursor falls on
(dotimes (i n-lines)
(let ((len (length (nth i lines))))
(when (and (>= pos accum) (<= pos (+ accum len)))
(when (and (>= pos accum) (or (<= pos (+ accum len))
(= i (1- n-lines))))
(setf cl i cc (- pos accum)))
(incf accum (1+ len))))
;; If text exists but pos is 0, move cursor to end (recovery for pos reset)
(when (and (plusp text-len) (zerop pos))
(setf pos text-len
cl (1- n-lines)
cc (length (car (last lines)))))
(let* ((panel-rows (max 4 (+ n-lines 2)))
(panel-top (- h 4 panel-rows -1))
(cx (+ hpad 2 cc))