v0.5.0: Text input + keybinding system
Four new modules: - input.lisp: terminal raw mode, escape sequence parser, key/mouse event structs, read-event backend integration - text-input.lisp: single-line text input with cursor, insertion, deletion, ctrl-A/E/W/U/K, on-submit callback, max-length - textarea.lisp: multi-line text input with cursor up/down, newline, backspace joins lines, delete, undo/redo stack - keybindings.lisp: layered keymap dispatch (global/local/focused), defkeymap macro, key spec matching with modifier prefixes 60 test assertions, 100% GREEN: RED: 0/12, 0/27, 0/30 — no tests existed GREEN: 60/60 across backend (27), box (58), input (60) Dependencies: sb-posix for terminal raw mode (tcgetattr/tcsetattr) Test files: 30 input tests covering all widgets and keybinding system
This commit is contained in:
163
src/components/text-input.lisp
Normal file
163
src/components/text-input.lisp
Normal file
@@ -0,0 +1,163 @@
|
||||
(in-package #:cl-tui.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; TextInput class
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defclass text-input (dirty-mixin)
|
||||
((value :initform "" :initarg :value :accessor text-input-value
|
||||
:type string)
|
||||
(cursor :initform 0 :initarg :cursor :accessor text-input-cursor
|
||||
:type fixnum)
|
||||
(placeholder :initform "" :initarg :placeholder
|
||||
:accessor text-input-placeholder :type string)
|
||||
(max-length :initform nil :initarg :max-length
|
||||
:accessor text-input-max-length)
|
||||
(on-submit :initform nil :initarg :on-submit
|
||||
:accessor text-input-on-submit)
|
||||
(layout-node :initform (make-layout-node) :accessor text-input-layout-node)
|
||||
(focusable :initform t :accessor text-input-focusable)))
|
||||
|
||||
(defun make-text-input (&key value cursor placeholder max-length on-submit)
|
||||
(make-instance 'text-input
|
||||
:value (or value "")
|
||||
:cursor (or cursor 0)
|
||||
:placeholder (or placeholder "")
|
||||
:max-length max-length
|
||||
:on-submit on-submit))
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Editing operations
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defun text-input-insert (input char)
|
||||
"Insert CHAR at the cursor position in INPUT."
|
||||
(let* ((val (text-input-value input))
|
||||
(pos (text-input-cursor input))
|
||||
(max (text-input-max-length input)))
|
||||
(when (and max (>= (length val) max))
|
||||
(return-from text-input-insert))
|
||||
(setf (text-input-value input)
|
||||
(concatenate 'string
|
||||
(subseq val 0 pos)
|
||||
(string char)
|
||||
(subseq val pos)))
|
||||
(incf (text-input-cursor input))
|
||||
(mark-dirty input)))
|
||||
|
||||
(defun text-input-backspace (input)
|
||||
"Delete character before cursor."
|
||||
(let* ((val (text-input-value input))
|
||||
(pos (text-input-cursor input)))
|
||||
(when (zerop pos) (return-from text-input-backspace))
|
||||
(setf (text-input-value input)
|
||||
(concatenate 'string
|
||||
(subseq val 0 (1- pos))
|
||||
(subseq val pos)))
|
||||
(decf (text-input-cursor input))
|
||||
(mark-dirty input)))
|
||||
|
||||
(defun text-input-delete (input)
|
||||
"Delete character at cursor."
|
||||
(let* ((val (text-input-value input))
|
||||
(pos (text-input-cursor input)))
|
||||
(when (>= pos (length val))
|
||||
(return-from text-input-delete))
|
||||
(setf (text-input-value input)
|
||||
(concatenate 'string
|
||||
(subseq val 0 pos)
|
||||
(subseq val (1+ pos))))
|
||||
(mark-dirty input)))
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Cursor movement
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defun text-input-move-left (input)
|
||||
(when (plusp (text-input-cursor input))
|
||||
(decf (text-input-cursor input))))
|
||||
|
||||
(defun text-input-move-right (input)
|
||||
(when (< (text-input-cursor input) (length (text-input-value input)))
|
||||
(incf (text-input-cursor input))))
|
||||
|
||||
(defun text-input-move-home (input)
|
||||
(setf (text-input-cursor input) 0))
|
||||
|
||||
(defun text-input-move-end (input)
|
||||
(setf (text-input-cursor input) (length (text-input-value input))))
|
||||
|
||||
(defun text-input-delete-word-before (input)
|
||||
"Delete from cursor back to previous word boundary."
|
||||
(let* ((val (text-input-value input))
|
||||
(pos (text-input-cursor input)))
|
||||
(when (zerop pos)
|
||||
(return-from text-input-delete-word-before))
|
||||
(let* ((start (or (position-if (lambda (c) (not (char= c #\Space)))
|
||||
val :end pos :from-end t)
|
||||
0))
|
||||
(word-start (or (and (plusp start)
|
||||
(position #\Space val :end start :from-end t))
|
||||
0))
|
||||
(delete-start (if (and (zerop word-start)
|
||||
(or (char/= (char val 0) #\Space)
|
||||
(zerop start)))
|
||||
0
|
||||
(if (zerop start)
|
||||
(1+ word-start)
|
||||
(1+ (or (position #\Space val :end start :from-end t)
|
||||
0))))))
|
||||
(setf (text-input-value input)
|
||||
(concatenate 'string
|
||||
(subseq val 0 delete-start)
|
||||
(subseq val pos)))
|
||||
(setf (text-input-cursor input) delete-start)
|
||||
(mark-dirty input))))
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Key event handler
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defun handle-text-input (input event)
|
||||
"Process a key-event on a text-input widget."
|
||||
(cond
|
||||
((key-event-ctrl event)
|
||||
(case (key-event-key event)
|
||||
(:a (text-input-move-home input))
|
||||
(:e (text-input-move-end input))
|
||||
(:w (text-input-delete-word-before input))
|
||||
(:u (progn
|
||||
(setf (text-input-value input)
|
||||
(subseq (text-input-value input)
|
||||
(text-input-cursor input)))
|
||||
(setf (text-input-cursor input) 0)
|
||||
(mark-dirty input)))
|
||||
(:k (progn
|
||||
(setf (text-input-value input)
|
||||
(subseq (text-input-value input) 0
|
||||
(text-input-cursor input)))
|
||||
(mark-dirty input)))
|
||||
(t nil)))
|
||||
(t
|
||||
(case (key-event-key event)
|
||||
(:left (text-input-move-left input))
|
||||
(:right (text-input-move-right input))
|
||||
(:home (text-input-move-home input))
|
||||
(:end (text-input-move-end input))
|
||||
(:backspace (text-input-backspace input))
|
||||
(:delete (text-input-delete input))
|
||||
(:enter (let ((cb (text-input-on-submit input)))
|
||||
(when cb (funcall cb (text-input-value input)))))
|
||||
(:tab nil)
|
||||
(:escape nil)
|
||||
;; Insert printable characters
|
||||
(otherwise
|
||||
(let ((ch (code-char (key-event-code event))))
|
||||
(when (and ch (graphic-char-p ch))
|
||||
(text-input-insert input ch))))))))
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Rendering (stub — proper rendering uses theme + backend)
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defmethod render ((in text-input) (backend t))
|
||||
"Render a text-input widget. Full rendering requires *current-backend*,
|
||||
*current-theme*, and the rendering pipeline. This is a no-op stub for
|
||||
unit testing the widget logic."
|
||||
(declare (ignore in backend))
|
||||
(values))
|
||||
Reference in New Issue
Block a user