From a65374e12066c482987019ab33bd8a64ea583f0c Mon Sep 17 00:00:00 2001 From: Amr Gharbeia Date: Mon, 18 May 2026 14:43:30 -0400 Subject: [PATCH] =?UTF-8?q?v0.8.0:=20add=20cursor=20position=20recovery=20?= =?UTF-8?q?=E2=80=94=20if=20text=20exists=20but=20pos=20is=200,=20place=20?= =?UTF-8?q?cursor=20at=20end?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit position-cursor now also clamps cursor-line to valid range and recovers from cursor-pos resets by moving to end of text. --- org/channel-tui-view.org | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/org/channel-tui-view.org b/org/channel-tui-view.org index cfe4283..555c5fe 100644 --- a/org/channel-tui-view.org +++ b/org/channel-tui-view.org @@ -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))