Compare commits
4 Commits
feature/v0
...
feature/v0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14193b8c92 | ||
|
|
811d51a4f2 | ||
|
|
9648c72b85 | ||
|
|
e96c338a57 |
14
README.org
14
README.org
@@ -1,8 +1,8 @@
|
||||
#+TITLE: cl-tui — Reusable Common Lisp Terminal UI Framework
|
||||
#+TITLE: cl-tty — Reusable Common Lisp Terminal UI Framework
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :project:cl-tui:readme:
|
||||
#+FILETAGS: :project:cl-tty:readme:
|
||||
|
||||
* cl-tui
|
||||
* cl-tty
|
||||
|
||||
A reusable Common Lisp framework for building rich terminal user interfaces.
|
||||
Built on croatoan (ncurses) with Yoga for Flexbox layout. Provides a component
|
||||
@@ -15,24 +15,24 @@ quality of Claude Code and OpenCode from Common Lisp.
|
||||
Common Lisp has no reusable terminal UI framework at the level of Python's
|
||||
Rich/prompt_toolkit or Go's Bubble Tea. Every CL project that wants a
|
||||
terminal UI either builds ncurses from scratch or uses a text-only REPL.
|
||||
cl-tui fills that gap — a component library with Flexbox layout, semantic
|
||||
cl-tty fills that gap — a component library with Flexbox layout, semantic
|
||||
theming, layered keybinding, and full mouse support. Build a terminal UI once,
|
||||
reuse it everywhere.
|
||||
|
||||
Terminal UIs also work over SSH. A Qt or browser-based UI requires a local
|
||||
display. A cl-tui application runs remotely — same code, same components,
|
||||
display. A cl-tty application runs remotely — same code, same components,
|
||||
accessible from anywhere.
|
||||
|
||||
** Architecture
|
||||
|
||||
```
|
||||
Application code (any CL project)
|
||||
└── cl-tui (layout, components, theme, events, dialogs)
|
||||
└── cl-tty (layout, components, theme, events, dialogs)
|
||||
└── Yoga (Flexbox layout — C library via FFI)
|
||||
└── croatoan (ncurses terminal rendering)
|
||||
```
|
||||
|
||||
cl-tui depends only on croatoan and Yoga. It is not tied to any application.
|
||||
cl-tty depends only on croatoan and Yoga. It is not tied to any application.
|
||||
|
||||
** Dependencies
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.backend)
|
||||
(in-package :cl-tty.backend)
|
||||
|
||||
(defclass backend () ())
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-modern-backend-test
|
||||
(:use :cl :fiveam :cl-tui.backend)
|
||||
(defpackage :cl-tty-modern-backend-test
|
||||
(:use :cl :fiveam :cl-tty.backend)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-modern-backend-test)
|
||||
(in-package :cl-tty-modern-backend-test)
|
||||
|
||||
(def-suite modern-backend-suite :description "Modern backend tests")
|
||||
(in-suite modern-backend-suite)
|
||||
@@ -16,72 +16,72 @@
|
||||
(test make-modern-backend-creates
|
||||
"make-modern-backend returns a modern-backend instance"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (typep b 'cl-tui.backend::modern-backend))))
|
||||
(is (typep b 'cl-tty.backend::modern-backend))))
|
||||
|
||||
;; ── Escape Generation ──────────────────────────────────────────
|
||||
|
||||
(test sgr-truecolor-foreground
|
||||
"SGR truecolor foreground escape is correct"
|
||||
(is (equal (cl-tui.backend::sgr-fg "#FFD700")
|
||||
(is (equal (cl-tty.backend::sgr-fg "#FFD700")
|
||||
(format nil "~C[38;2;255;215;0m" #\Esc))))
|
||||
|
||||
(test sgr-truecolor-background
|
||||
"SGR truecolor background escape is correct"
|
||||
(is (equal (cl-tui.backend::sgr-bg "#1a1b26")
|
||||
(is (equal (cl-tty.backend::sgr-bg "#1a1b26")
|
||||
(format nil "~C[48;2;26;27;38m" #\Esc))))
|
||||
|
||||
(test sgr-named-colors
|
||||
"SGR named colors resolve to 8-color codes"
|
||||
(is (equal (cl-tui.backend::sgr-fg :red)
|
||||
(is (equal (cl-tty.backend::sgr-fg :red)
|
||||
(format nil "~C[31m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-bg :blue)
|
||||
(is (equal (cl-tty.backend::sgr-bg :blue)
|
||||
(format nil "~C[44m" #\Esc))))
|
||||
|
||||
(test sgr-bold-italic
|
||||
"SGR attribute escapes are correct"
|
||||
(is (equal (cl-tui.backend::sgr-attr :bold) (format nil "~C[1m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-attr :italic) (format nil "~C[3m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-attr :underline) (format nil "~C[4m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-attr :reset) (format nil "~C[0m" #\Esc))))
|
||||
(is (equal (cl-tty.backend::sgr-attr :bold) (format nil "~C[1m" #\Esc)))
|
||||
(is (equal (cl-tty.backend::sgr-attr :italic) (format nil "~C[3m" #\Esc)))
|
||||
(is (equal (cl-tty.backend::sgr-attr :underline) (format nil "~C[4m" #\Esc)))
|
||||
(is (equal (cl-tty.backend::sgr-attr :reset) (format nil "~C[0m" #\Esc))))
|
||||
|
||||
;; ── Cursor ─────────────────────────────────────────────────────
|
||||
|
||||
(test cursor-move-escape
|
||||
"cursor-move generates correct CSI escape"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-move-escape 5 10)
|
||||
(is (equal (cl-tty.backend::cursor-move-escape 5 10)
|
||||
(format nil "~C[11;6H" #\Esc)))))
|
||||
|
||||
(test cursor-style-block
|
||||
"cursor-style :block generate correct escape"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-style-escape :block nil)
|
||||
(is (equal (cl-tty.backend::cursor-style-escape :block nil)
|
||||
(format nil "~C[2 q" #\Esc)))))
|
||||
|
||||
(test cursor-style-bar
|
||||
"cursor-style :bar generate correct escape"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-style-escape :bar nil)
|
||||
(is (equal (cl-tty.backend::cursor-style-escape :bar nil)
|
||||
(format nil "~C[6 q" #\Esc)))))
|
||||
|
||||
(test cursor-style-underline-blink
|
||||
"cursor-style :underline with blink"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-style-escape :underline t)
|
||||
(is (equal (cl-tty.backend::cursor-style-escape :underline t)
|
||||
(format nil "~C[5 q" #\Esc)))))
|
||||
|
||||
;; ── Synchronization ────────────────────────────────────────────
|
||||
|
||||
(test decicm-escapes
|
||||
"DECICM synchronized update escapes"
|
||||
(is (equal (cl-tui.backend::decicm-begin) (format nil "~C[?2026h" #\Esc)))
|
||||
(is (equal (cl-tui.backend::decicm-end) (format nil "~C[?2026l" #\Esc))))
|
||||
(is (equal (cl-tty.backend::decicm-begin) (format nil "~C[?2026h" #\Esc)))
|
||||
(is (equal (cl-tty.backend::decicm-end) (format nil "~C[?2026l" #\Esc))))
|
||||
|
||||
;; ── OSC 8 Hyperlinks ──────────────────────────────────────────
|
||||
|
||||
(test osc8-escape
|
||||
"OSC 8 hyperlink escape wraps text"
|
||||
(is (equal (cl-tui.backend::osc8-link "http://example.com" "click here")
|
||||
(is (equal (cl-tty.backend::osc8-link "http://example.com" "click here")
|
||||
(format nil "~C]8;;http://example.com~C\\click here~C]8;;~C\\"
|
||||
#\Esc #\Esc #\Esc #\Esc))))
|
||||
|
||||
@@ -89,21 +89,21 @@
|
||||
|
||||
(test hex-color-parsing
|
||||
"hex-to-rgb parses valid hex colors"
|
||||
(multiple-value-bind (r g b) (cl-tui.backend::hex-to-rgb "#FFD700")
|
||||
(multiple-value-bind (r g b) (cl-tty.backend::hex-to-rgb "#FFD700")
|
||||
(is (= r 255))
|
||||
(is (= g 215))
|
||||
(is (= b 0))))
|
||||
|
||||
(test hex-color-black
|
||||
"hex-to-rgb parses black"
|
||||
(multiple-value-bind (r g b) (cl-tui.backend::hex-to-rgb "#000000")
|
||||
(multiple-value-bind (r g b) (cl-tty.backend::hex-to-rgb "#000000")
|
||||
(is (= r 0))
|
||||
(is (= g 0))
|
||||
(is (= b 0))))
|
||||
|
||||
(test hex-color-short-form
|
||||
"hex-to-rgb parses 3-digit hex"
|
||||
(multiple-value-bind (r g b) (cl-tui.backend::hex-to-rgb "#F00")
|
||||
(multiple-value-bind (r g b) (cl-tty.backend::hex-to-rgb "#F00")
|
||||
(is (= r 255))
|
||||
(is (= g 0))
|
||||
(is (= b 0))))
|
||||
@@ -112,13 +112,13 @@
|
||||
|
||||
(test border-char-rounded
|
||||
"modern-border-char returns Unicode box-drawing for rounded style"
|
||||
(is (equal (cl-tui.backend::border-char :rounded :top-left) "╭"))
|
||||
(is (equal (cl-tui.backend::border-char :rounded :horizontal) "─"))
|
||||
(is (equal (cl-tui.backend::border-char :rounded :vertical) "│"))
|
||||
(is (equal (cl-tui.backend::border-char :rounded :bottom-right) "╯")))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :top-left) "╭"))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :horizontal) "─"))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :vertical) "│"))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :bottom-right) "╯")))
|
||||
|
||||
(test border-char-double
|
||||
"modern-border-char returns double-line chars"
|
||||
(is (equal (cl-tui.backend::border-char :double :top-left) "╔"))
|
||||
(is (equal (cl-tui.backend::border-char :double :horizontal) "═"))
|
||||
(is (equal (cl-tui.backend::border-char :double :vertical) "║")))
|
||||
(is (equal (cl-tty.backend::border-char :double :top-left) "╔"))
|
||||
(is (equal (cl-tty.backend::border-char :double :horizontal) "═"))
|
||||
(is (equal (cl-tty.backend::border-char :double :vertical) "║")))
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
;; sgr-fg sgr-bg sgr-attr cursor-move-escape cursor-style-escape
|
||||
;; decicm-begin decicm-end osc8-link hex-to-rgb border-char
|
||||
|
||||
(in-package :cl-tui.backend)
|
||||
(in-package :cl-tty.backend)
|
||||
|
||||
(defun hex-to-rgb (hex)
|
||||
"Parse a hex color string like \"#FFD700\" into (values r g b).
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(defpackage :cl-tui.backend
|
||||
(defpackage :cl-tty.backend
|
||||
(:use :cl)
|
||||
(:export
|
||||
;; Backend classes
|
||||
@@ -26,4 +26,4 @@
|
||||
#:cursor-move-escape #:cursor-style-escape
|
||||
#:decicm-begin #:decicm-end #:osc8-link
|
||||
#:hex-to-rgb #:border-char))
|
||||
(in-package :cl-tui.backend)
|
||||
(in-package :cl-tty.backend)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.backend)
|
||||
(in-package :cl-tty.backend)
|
||||
|
||||
(defclass simple-backend (backend)
|
||||
((output-stream :initform *standard-output*
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-backend-test
|
||||
(:use :cl :fiveam :cl-tui.backend)
|
||||
(defpackage :cl-tty-backend-test
|
||||
(:use :cl :fiveam :cl-tty.backend)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-backend-test)
|
||||
(in-package :cl-tty-backend-test)
|
||||
|
||||
(def-suite backend-suite :description "Backend protocol tests")
|
||||
(in-suite backend-suite)
|
||||
|
||||
82
cl-tty.asd
Normal file
82
cl-tty.asd
Normal file
@@ -0,0 +1,82 @@
|
||||
;;; cl-tty.asd — Common Lisp Terminal UI Framework
|
||||
(asdf:defsystem :cl-tty
|
||||
:description "Reusable Common Lisp Terminal UI Framework"
|
||||
:author "Amr Gharbeia"
|
||||
:version "0.9.0"
|
||||
:license "TBD"
|
||||
:depends-on (:fiveam :sb-posix)
|
||||
:components
|
||||
((:module "backend"
|
||||
:components
|
||||
((:file "package")
|
||||
(:file "classes" :depends-on ("package"))
|
||||
(:file "simple" :depends-on ("package" "classes"))
|
||||
(:file "modern" :depends-on ("package" "classes"))))
|
||||
(:module "layout"
|
||||
:components
|
||||
((:file "layout")))
|
||||
(:module "src/components"
|
||||
:components
|
||||
((:file "package")
|
||||
(:file "dirty")
|
||||
(:file "box" :depends-on ("package"))
|
||||
(:file "text" :depends-on ("package" "box"))
|
||||
(:file "render" :depends-on ("package" "box" "text"))
|
||||
(:file "theme" :depends-on ("package"))
|
||||
;; Input system (v0.5.0)
|
||||
(:file "input-package" :depends-on ("package"))
|
||||
(:file "input" :depends-on ("input-package" "dirty" "box"))
|
||||
(:file "text-input" :depends-on ("input-package" "input" "box"))
|
||||
(:file "textarea" :depends-on ("input-package" "input" "box"))
|
||||
(:file "keybindings" :depends-on ("input-package" "input"))
|
||||
;; Container components (v0.6.0)
|
||||
(:file "container-package" :depends-on ("package" "input-package"))
|
||||
(:file "scrollbox" :depends-on ("container-package" "dirty" "box"))
|
||||
(:file "tabbar" :depends-on ("container-package" "dirty" "box"))
|
||||
;; Select widget (v0.7.0)
|
||||
(:file "select-package" :depends-on ("package" "input-package"))
|
||||
(:file "select" :depends-on ("select-package" "dirty" "box"))
|
||||
;; Markdown + Code + Diff rendering (v0.8.0)
|
||||
(:file "markdown-package" :depends-on ("package"))
|
||||
(:file "markdown" :depends-on ("markdown-package"))
|
||||
;; Dialog + Toast (v0.9.0)
|
||||
(:file "dialog-package" :depends-on ("package" "select-package" "input-package"))
|
||||
(:file "dialog" :depends-on ("dialog-package" "dirty" "select" "text-input")))))
|
||||
:in-order-to ((test-op (test-op :cl-tty-tests))))
|
||||
|
||||
(asdf:defsystem :cl-tty-tests
|
||||
:description "Test suite for cl-tty"
|
||||
:depends-on (:cl-tty :fiveam)
|
||||
:components
|
||||
((:module "backend"
|
||||
:components
|
||||
((:file "tests")))
|
||||
(:module "layout"
|
||||
:components
|
||||
((:file "tests")))
|
||||
(:module "src/components"
|
||||
:components
|
||||
((:file "box-tests")
|
||||
(:file "dirty-tests")
|
||||
(:file "render-tests")
|
||||
(:file "theme-tests")
|
||||
(:file "input-tests")
|
||||
(:file "scrollbox-tabbar-tests" :pathname "../../tests/scrollbox-tabbar-tests.lisp")
|
||||
(:file "select-tests" :pathname "../../tests/select-tests.lisp")
|
||||
(:file "markdown-tests" :pathname "../../tests/markdown-tests.lisp")
|
||||
(:file "dialog-tests" :pathname "../../tests/dialog-tests.lisp"))))
|
||||
:perform (test-op (o c)
|
||||
(let ((run (find-symbol "RUN" :fiveam))
|
||||
(explain (find-symbol "EXPLAIN!" :fiveam)))
|
||||
(dolist (suite '((:cl-tty-backend-test "BACKEND-SUITE")
|
||||
(:cl-tty-box-test "BOX-SUITE")
|
||||
(:cl-tty-input-test "INPUT-SUITE")
|
||||
(:cl-tty-scrollbox-test "SCROLLBOX-SUITE")
|
||||
(:cl-tty-select-test "SELECT-SUITE")
|
||||
(:cl-tty-markdown-test "MARKDOWN-SUITE")
|
||||
(:cl-tty-dialog-test "DIALOG-SUITE")))
|
||||
(let* ((pkg (find-package (first suite)))
|
||||
(s (and pkg (find-symbol (second suite) pkg))))
|
||||
(when s
|
||||
(funcall explain (funcall run s))))))
|
||||
(uiop:quit 0)))
|
||||
65
cl-tui.asd
65
cl-tui.asd
@@ -1,65 +0,0 @@
|
||||
;;; cl-tui.asd — Common Lisp Terminal UI Framework
|
||||
(asdf:defsystem :cl-tui
|
||||
:description "Reusable Common Lisp Terminal UI Framework"
|
||||
:author "Amr Gharbeia"
|
||||
:version "0.6.0"
|
||||
:license "TBD"
|
||||
:depends-on (:fiveam :sb-posix)
|
||||
:components
|
||||
((:module "backend"
|
||||
:components
|
||||
((:file "package")
|
||||
(:file "classes" :depends-on ("package"))
|
||||
(:file "simple" :depends-on ("package" "classes"))
|
||||
(:file "modern" :depends-on ("package" "classes"))))
|
||||
(:module "layout"
|
||||
:components
|
||||
((:file "layout")))
|
||||
(:module "src/components"
|
||||
:components
|
||||
((:file "package")
|
||||
(:file "dirty")
|
||||
(:file "box" :depends-on ("package"))
|
||||
(:file "text" :depends-on ("package" "box"))
|
||||
(:file "render" :depends-on ("package" "box" "text"))
|
||||
(:file "theme" :depends-on ("package"))
|
||||
;; Input system (v0.5.0)
|
||||
(:file "input-package" :depends-on ("package"))
|
||||
(:file "input" :depends-on ("input-package" "dirty" "box"))
|
||||
(:file "text-input" :depends-on ("input-package" "input" "box"))
|
||||
(:file "textarea" :depends-on ("input-package" "input" "box"))
|
||||
(:file "keybindings" :depends-on ("input-package" "input"))
|
||||
;; Container components (v0.6.0)
|
||||
(:file "container-package" :depends-on ("package" "input-package"))
|
||||
(:file "scrollbox" :depends-on ("container-package" "dirty" "box"))
|
||||
(:file "tabbar" :depends-on ("container-package" "dirty" "box"))))
|
||||
:in-order-to ((test-op (test-op :cl-tui-tests))))
|
||||
|
||||
(asdf:defsystem :cl-tui-tests
|
||||
:description "Test suite for cl-tui"
|
||||
:depends-on (:cl-tui :fiveam)
|
||||
:components
|
||||
((:module "backend"
|
||||
:components
|
||||
((:file "tests")))
|
||||
(:module "layout"
|
||||
:components
|
||||
((:file "tests")))
|
||||
(:module "src/components"
|
||||
:components
|
||||
((:file "box-tests")
|
||||
(:file "dirty-tests")
|
||||
(:file "render-tests")
|
||||
(:file "theme-tests")
|
||||
(:file "input-tests")
|
||||
(:file "scrollbox-tabbar-tests" :pathname "../../tests/scrollbox-tabbar-tests.lisp"))))
|
||||
:perform (test-op (o c)
|
||||
(dolist (suite '((:cl-tui-backend-test "BACKEND-SUITE")
|
||||
(:cl-tui-box-test "BOX-SUITE")
|
||||
(:cl-tui-input-test "INPUT-SUITE")
|
||||
(:cl-tui-scrollbox-test "SCROLLBOX-SUITE")))
|
||||
(let* ((pkg (find-package (first suite)))
|
||||
(s (and pkg (find-symbol (second suite) pkg))))
|
||||
(when s
|
||||
(fiveam:explain! (fiveam:run s)))))
|
||||
(uiop:quit 0)))
|
||||
@@ -1,4 +1,4 @@
|
||||
;; demo.lisp — minimal cl-tui demo
|
||||
;; demo.lisp — minimal cl-tty demo
|
||||
(load "/root/quicklisp/setup.lisp")
|
||||
(ql:quickload :fiveam :silent t)
|
||||
(load "backend/package.lisp")
|
||||
@@ -11,7 +11,7 @@
|
||||
(load "src/components/box.lisp")
|
||||
(load "src/components/text.lisp")
|
||||
(load "src/components/render.lisp")
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
;; Demo 1: Simple backend (ASCII)
|
||||
(let* ((b (make-simple-backend))
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
;; Demo 2: Box with text inside
|
||||
(let* ((b (make-simple-backend))
|
||||
(tx (make-text "This is cl-tui in action!" :width 28 :height 1)))
|
||||
(tx (make-text "This is cl-tty in action!" :width 28 :height 1)))
|
||||
(setf (layout-node-direction (text-layout-node tx)) :column)
|
||||
(compute-layout (text-layout-node tx) 28 1)
|
||||
(render tx b)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#+TITLE: cl-tui Architecture
|
||||
#+TITLE: cl-tty Architecture
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :project:cl-tui:architecture:
|
||||
#+FILETAGS: :project:cl-tty:architecture:
|
||||
|
||||
* Architecture
|
||||
|
||||
cl-tui is a layered framework. Each layer has a single responsibility
|
||||
cl-tty is a layered framework. Each layer has a single responsibility
|
||||
and communicates with adjacent layers through a well-defined protocol.
|
||||
|
||||
** Layer Diagram
|
||||
@@ -264,9 +264,9 @@ reads terminal background color at startup.
|
||||
** File Structure
|
||||
|
||||
#+BEGIN_SRC
|
||||
cl-tui/
|
||||
├── cl-tui.asd
|
||||
├── cl-tui-tests.asd
|
||||
cl-tty/
|
||||
├── cl-tty.asd
|
||||
├── cl-tty-tests.asd
|
||||
├── README.org
|
||||
├── LICENSE
|
||||
├── docs/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#+TITLE: cl-tui Roadmap
|
||||
#+TITLE: cl-tty Roadmap
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :docs:roadmap:cl-tui:
|
||||
#+FILETAGS: :docs:roadmap:cl-tty:
|
||||
|
||||
* The Roadmap
|
||||
|
||||
@@ -87,7 +87,7 @@ the patch version (v0.X.Y).
|
||||
When a version ships:
|
||||
1. ~ROADMAP.org~ — mark item DONE, update LOGBOOK timestamp
|
||||
2. ~README.org~ — update Status line
|
||||
3. ~cl-tui.asd~ — update version string
|
||||
3. ~cl-tty.asd~ — update version string
|
||||
|
||||
** v0.1.0: Layout Engine
|
||||
|
||||
@@ -295,7 +295,7 @@ never hex values.
|
||||
8 presets: default (gold), professional, minimal, nord, tokyonight, catppuccin, monokai, gruvbox
|
||||
- Each preset is a plist: ~(:primary "#FFD700" :error "#BF616A" ...)~
|
||||
- ~(theme-load :nord)~ — activates a preset, re-renders dirty
|
||||
- Load from ~/.config/cl-tui/themes/<name>.lisp~ for custom themes
|
||||
- Load from ~/.config/cl-tty/themes/<name>.lisp~ for custom themes
|
||||
- ~80 lines
|
||||
|
||||
*** TODO Dark/light variants
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
- `src/components/dirty.lisp` — tangled
|
||||
|
||||
**Files modified:**
|
||||
- `cl-tui.asd` — add component modules
|
||||
- `cl-tty.asd` — add component modules
|
||||
- `docs/ROADMAP.org` — mark v0.2.0 tasks DONE
|
||||
|
||||
## Task 1: Box renderable
|
||||
@@ -25,7 +25,7 @@
|
||||
**Files:**
|
||||
- Create: `org/box-renderable.org`
|
||||
- Create: `src/components/box.lisp` (extracted)
|
||||
- Modify: `cl-tui.asd` — add components module
|
||||
- Modify: `cl-tty.asd` — add components module
|
||||
|
||||
**Box class:**
|
||||
```lisp
|
||||
@@ -120,7 +120,7 @@ Default methods mark/check a `dirty` slot on the component. When implemented:
|
||||
## Task 4: Wire into ASDF + update roadmap
|
||||
|
||||
**Files:**
|
||||
- Modify: `cl-tui.asd` — add `:module "components"` to both main and test systems
|
||||
- Modify: `cl-tty.asd` — add `:module "components"` to both main and test systems
|
||||
- Modify: `docs/ROADMAP.org` — mark v0.2.0 tasks DONE
|
||||
|
||||
**Run full test suite:**
|
||||
|
||||
@@ -30,7 +30,7 @@ src/components/keybindings.lisp — tangled: keybinding system
|
||||
- Modify: `backend/package.lisp` — add input exports
|
||||
- Modify: `backend/modern.lisp` — implement read-event
|
||||
- Modify: `backend/simple.lisp` — implement read-event (stdin)
|
||||
- Modify: `cl-tui.asd` — add input module to main and test systems
|
||||
- Modify: `cl-tty.asd` — add input module to main and test systems
|
||||
|
||||
**Code architecture:**
|
||||
|
||||
@@ -120,7 +120,7 @@ src/components/keybindings.lisp — tangled: keybinding system
|
||||
- Create: `org/text-input.org`
|
||||
- Create: `src/components/input.lisp`
|
||||
- Modify: `src/components/package.lisp` — add exports
|
||||
- Modify: `cl-tui.asd` — add input.lisp
|
||||
- Modify: `cl-tty.asd` — add input.lisp
|
||||
|
||||
**TextInput class:**
|
||||
```lisp
|
||||
@@ -214,7 +214,7 @@ src/components/keybindings.lisp — tangled: keybinding system
|
||||
- Create: `org/textarea.org`
|
||||
- Create: `src/components/textarea.lisp`
|
||||
- Modify: `src/components/package.lisp` — add exports
|
||||
- Modify: `cl-tui.asd` — add textarea.lisp
|
||||
- Modify: `cl-tty.asd` — add textarea.lisp
|
||||
|
||||
**Textarea class:**
|
||||
```lisp
|
||||
@@ -255,7 +255,7 @@ src/components/keybindings.lisp — tangled: keybinding system
|
||||
- Create: `org/keybindings.org`
|
||||
- Create: `src/components/keybindings.lisp`
|
||||
- Modify: `src/components/package.lisp` — add exports
|
||||
- Modify: `cl-tui.asd` — add keybindings.lisp
|
||||
- Modify: `cl-tty.asd` — add keybindings.lisp
|
||||
|
||||
**Architecture:**
|
||||
```lisp
|
||||
@@ -355,7 +355,7 @@ Task 1 is the prerequisite for everything. Tasks 2, 3, 4 can then proceed in par
|
||||
### Verification
|
||||
|
||||
After each task:
|
||||
1. `sbcl --eval "(asdf:test-system :cl-tui)" --quit` — all tests GREEN
|
||||
1. `sbcl --eval "(asdf:test-system :cl-tty)" --quit` — all tests GREEN
|
||||
2. `scripts/validate-parens.py` — all files balanced
|
||||
3. Commit with RED/GREEN evidence
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
;;; layout — Pure CL Flexbox layout engine
|
||||
|
||||
(defpackage :cl-tui.layout
|
||||
(defpackage :cl-tty.layout
|
||||
(:use :cl)
|
||||
(:export
|
||||
#:layout-node #:make-layout-node
|
||||
@@ -16,7 +16,7 @@
|
||||
#:layout-node-fixed-height #:normalize-box
|
||||
#:box-edge))
|
||||
|
||||
(in-package :cl-tui.layout)
|
||||
(in-package :cl-tty.layout)
|
||||
|
||||
(defun normalize-box (spec)
|
||||
(cond ((null spec) '(:top 0 :right 0 :bottom 0 :left 0))
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-layout-test
|
||||
(:use :cl :fiveam :cl-tui.layout)
|
||||
(defpackage :cl-tty-layout-test
|
||||
(:use :cl :fiveam :cl-tty.layout)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-layout-test)
|
||||
(in-package :cl-tty-layout-test)
|
||||
|
||||
(def-suite layout-suite :description "Layout engine tests")
|
||||
(in-suite layout-suite)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#+TITLE: cl-tui Backend Protocol — v0.0.1
|
||||
#+TITLE: cl-tty Backend Protocol — v0.0.1
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :cl-tui:backend:v0.0.1:
|
||||
#+FILETAGS: :cl-tty:backend:v0.0.1:
|
||||
#+OPTIONS: ^:nil
|
||||
|
||||
* Backend Protocol
|
||||
@@ -119,10 +119,10 @@ Borders:
|
||||
** Test Suite
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui-backend-test
|
||||
(defpackage :cl-tty-backend-test
|
||||
(:use :cl :fiveam)
|
||||
(:export #:run!))
|
||||
(in-package :cl-tui-backend-test)
|
||||
(in-package :cl-tty-backend-test)
|
||||
|
||||
(def-suite backend-suite :description "Backend protocol tests")
|
||||
(in-suite backend-suite)
|
||||
@@ -224,7 +224,7 @@ Borders:
|
||||
*** Package
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui.backend
|
||||
(defpackage :cl-tty.backend
|
||||
(:use :cl)
|
||||
(:export
|
||||
;; Backend classes
|
||||
@@ -245,7 +245,7 @@ Borders:
|
||||
#:capable-p
|
||||
;; Constructors
|
||||
#:make-simple-backend))
|
||||
(in-package :cl-tui.backend)
|
||||
(in-package :cl-tty.backend)
|
||||
#+END_SRC
|
||||
|
||||
*** Backend Base Class
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#+TITLE: cl-tui Box Renderable — v0.2.0
|
||||
#+TITLE: cl-tty Box Renderable — v0.2.0
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :cl-tui:components:v0.2.0:
|
||||
#+FILETAGS: :cl-tty:components:v0.2.0:
|
||||
#+OPTIONS: ^:nil
|
||||
|
||||
* Box Renderable
|
||||
@@ -27,10 +27,10 @@ A Box has a =layout-node= slot for positioning via the layout engine. Its
|
||||
** Tests
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui-box-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.layout)
|
||||
(defpackage :cl-tty-box-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.layout)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-box-test)
|
||||
(in-package :cl-tty-box-test)
|
||||
|
||||
(def-suite box-suite :description "Box renderable tests")
|
||||
(in-suite box-suite)
|
||||
@@ -116,7 +116,7 @@ A Box has a =layout-node= slot for positioning via the layout engine. Its
|
||||
** Implementation
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
(defclass box ()
|
||||
((layout-node :initform (make-layout-node) :accessor box-layout-node
|
||||
|
||||
496
org/dialog.org
Normal file
496
org/dialog.org
Normal file
@@ -0,0 +1,496 @@
|
||||
#+TITLE: Dialog System + Toast (v0.9.0)
|
||||
#+DATE: 2026-05-11
|
||||
#+AUTHOR: Amr Gharbeia / Hermes
|
||||
|
||||
* Overview
|
||||
|
||||
Modal overlays (dialogs) and transient notifications (toasts).
|
||||
|
||||
Dialogs are absolute-positioned panels centered on a dimmed backdrop.
|
||||
They stack — a new dialog goes on top, Esc dismisses the top one.
|
||||
|
||||
Toasts are non-blocking notifications that auto-dismiss after a
|
||||
duration. They stack in the top-right corner.
|
||||
|
||||
** Design decisions
|
||||
|
||||
1. /Stack-based dialog management/: a ~*dialog-stack*~ special variable
|
||||
holds the active dialogs. Render walks the stack from bottom to top,
|
||||
drawing each dialog's backdrop over the previous one. This means two
|
||||
dialogs visible at once — the top one gets full interaction.
|
||||
|
||||
2. /Backdrop is a solid dim color, not semi-transparent/: true
|
||||
transparency requires compositing pixel buffers, which is expensive
|
||||
in the terminal. A solid dimmed color over the full screen width
|
||||
communicates "modal" without the complexity.
|
||||
|
||||
3. /Dialogs are components, not separate windows/: they integrate into
|
||||
the existing render tree. The dialog class inherits from the component
|
||||
base and participates in dirty tracking, z-order, etc.
|
||||
|
||||
4. /Toast is fire-and-forget/: ~(toast ...)~ creates a toast component,
|
||||
adds it to a toast list, and schedules auto-dismissal. No lifecycle
|
||||
management needed from the caller.
|
||||
|
||||
** Contract
|
||||
|
||||
- ~dialog~ class — overlay component with backdrop, border, title
|
||||
- ~*dialog-stack*~ — list of active dialogs (bound per-screen)
|
||||
- ~push-dialog dialog~ — add dialog to stack, focus its first input
|
||||
- ~pop-dialog~ — dismiss top dialog, fire :on-dismiss
|
||||
- ~(alert-dialog title message)~ — OK-button alert
|
||||
- ~(confirm-dialog title message &key on-yes on-no)~ — Yes/No/Cancel
|
||||
- ~(select-dialog title options &key on-select)~ — modal Select
|
||||
- ~(prompt-dialog title &key on-submit)~ — modal TextInput
|
||||
- ~toast~ component — transient notification with variant color
|
||||
- ~(toast message &key variant duration)~ — fire-and-forget toast
|
||||
|
||||
* Code structure
|
||||
|
||||
** Dialog class
|
||||
|
||||
--- per-function: dialog-class
|
||||
|
||||
The dialog class stores the dialog's content (a component to render
|
||||
inside the dialog panel), its size preset, title, and callbacks.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defclass dialog ()
|
||||
((title :initarg :title :accessor dialog-title)
|
||||
(size :initarg :size :initform :medium :accessor dialog-size)
|
||||
(content :initarg :content :accessor dialog-content)
|
||||
(on-dismiss :initarg :on-dismiss :initform nil :accessor dialog-on-dismiss)))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: dialog-size-pixels
|
||||
|
||||
Helper to convert size keyword to pixel dimensions.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun dialog-size-pixels (size)
|
||||
(case size
|
||||
(:small (values 40 8))
|
||||
(:medium (values 60 16))
|
||||
(:large (values 88 24))
|
||||
(t (values 60 16))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: render-dialog
|
||||
|
||||
Render a dialog: backdrop (dimmed full-screen), then centered panel.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun render-dialog (dialog screen w h)
|
||||
(multiple-value-bind (dw dh) (dialog-size-pixels (dialog-size dialog))
|
||||
(let ((x (floor (- w dw) 2))
|
||||
(y (floor (- h dh) 2)))
|
||||
;; Backdrop — draw dim characters over full screen
|
||||
(dotimes (row h)
|
||||
(dotimes (col w)
|
||||
(backend-write screen col row " " :bg :dim)))
|
||||
;; Panel border
|
||||
(draw-border screen x y dw dh :single :title (dialog-title dialog))
|
||||
;; Content area (inset by 1 on each side)
|
||||
(when (dialog-content dialog)
|
||||
(render-component (dialog-content dialog) screen (1+ x) (1+ y) (- dw 2) (- dh 2))))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: push-dialog
|
||||
|
||||
Push a dialog onto the stack and give it focus.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun push-dialog (dialog)
|
||||
(push dialog *dialog-stack*)
|
||||
(when (typep (dialog-content dialog) 'focusable-mixin)
|
||||
(focus (dialog-content dialog)))
|
||||
dialog)
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: pop-dialog
|
||||
|
||||
Pop the top dialog, fire its on-dismiss callback.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun pop-dialog ()
|
||||
(when *dialog-stack*
|
||||
(let ((dialog (pop *dialog-stack*)))
|
||||
(when (dialog-on-dismiss dialog)
|
||||
(funcall (dialog-on-dismiss dialog)))
|
||||
dialog)))
|
||||
#+END_SRC
|
||||
|
||||
** Dialog sub-classes
|
||||
|
||||
--- per-function: alert-dialog
|
||||
|
||||
Simple alert with title, message, and OK button. The button is a
|
||||
Select with a single "OK" option.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun alert-dialog (title message)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'select
|
||||
:options (list (list :title "OK" :value :ok))
|
||||
:on-select (lambda (opt) (declare (ignore opt)) (pop-dialog)))
|
||||
:on-dismiss (lambda () (pop-dialog))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: confirm-dialog
|
||||
|
||||
Confirm dialog with Yes/No/Cancel buttons. Returns :yes or :no
|
||||
via the on-yes/on-no callbacks.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun confirm-dialog (title message &key on-yes on-no)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'select
|
||||
:options (list (list :title "Yes" :value :yes)
|
||||
(list :title "No" :value :no))
|
||||
:on-select (lambda (opt)
|
||||
(pop-dialog)
|
||||
(if (eql opt :yes)
|
||||
(when on-yes (funcall on-yes))
|
||||
(when on-no (funcall on-no)))))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: select-dialog
|
||||
|
||||
Modal wrapper around the Select component.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun select-dialog (title options &key on-select)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :medium
|
||||
:content (make-instance 'select
|
||||
:options options
|
||||
:on-select (lambda (opt)
|
||||
(pop-dialog)
|
||||
(when on-select (funcall on-select opt))))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: prompt-dialog
|
||||
|
||||
Modal wrapper around TextInput.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun prompt-dialog (title &key on-submit)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'text-input
|
||||
:on-submit (lambda (value)
|
||||
(pop-dialog)
|
||||
(when on-submit (funcall on-submit value))))))
|
||||
#+END_SRC
|
||||
|
||||
** Toast system
|
||||
|
||||
--- per-function: toast
|
||||
|
||||
Fire-and-forget toast notification. Creates a toast component,
|
||||
adds it to the toast list, and schedules auto-dismissal.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun toast (message &key (variant :info) (duration 5000))
|
||||
(let ((toast (make-instance 'toast :message message :variant variant)))
|
||||
(push toast *toasts*)
|
||||
;; Schedule auto-dismiss
|
||||
(when (plusp duration)
|
||||
(schedule-event (+ (get-internal-real-time)
|
||||
(* duration 1000))
|
||||
(lambda () (dismiss-toast toast))))
|
||||
toast))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: toast-class
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defclass toast ()
|
||||
((message :initarg :message :accessor toast-message)
|
||||
(variant :initarg :variant :initform :info :accessor toast-variant)))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: render-toast
|
||||
|
||||
Render toast in top-right corner. Max 60 cols. Shows colored
|
||||
left border based on variant.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun render-toast (toast screen w)
|
||||
(let* ((msg (toast-message toast))
|
||||
(variant (toast-variant toast))
|
||||
(color (case variant
|
||||
(:info :blue) (:success :green)
|
||||
(:warning :yellow) (:error :red)))
|
||||
(max-w (min 60 (1- w)))
|
||||
(x (- w max-w 1))
|
||||
(text (if (> (length msg) (- max-w 2))
|
||||
(concatenate 'string (subseq msg 0 (- max-w 5)) "...")
|
||||
msg)))
|
||||
(draw-rect screen x 0 max-w 1 :bg color)
|
||||
(backend-write screen (1+ x) 0 text :fg :white :bold t)))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: dismiss-toast
|
||||
|
||||
Remove a toast from the list.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun dismiss-toast (toast)
|
||||
(setf *toasts* (remove toast *toasts*)))
|
||||
#+END_SRC
|
||||
|
||||
** Tests
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(def-test dialog-create ()
|
||||
(let ((d (make-instance 'dialog :title "Test")))
|
||||
(is-true (typep d 'dialog))
|
||||
(is (equal "Test" (dialog-title d)))))
|
||||
|
||||
(def-test dialog-size-small ()
|
||||
(multiple-value-bind (w h) (dialog-size-pixels :small)
|
||||
(is (= 40 w))
|
||||
(is (= 8 h))))
|
||||
|
||||
(def-test dialog-size-medium ()
|
||||
(multiple-value-bind (w h) (dialog-size-pixels :medium)
|
||||
(is (= 60 w))
|
||||
(is (= 16 h))))
|
||||
|
||||
(def-test dialog-push-pop ()
|
||||
(let ((*dialog-stack* nil))
|
||||
(push-dialog (make-instance 'dialog :title "D1"))
|
||||
(is (= 1 (length *dialog-stack*)))
|
||||
(push-dialog (make-instance 'dialog :title "D2"))
|
||||
(is (= 2 (length *dialog-stack*)))
|
||||
(pop-dialog)
|
||||
(is (= 1 (length *dialog-stack*)))))
|
||||
|
||||
(def-test toast-create ()
|
||||
(let ((*toasts* nil))
|
||||
(toast "Hello" :variant :info :duration 0)
|
||||
(is (= 1 (length *toasts*)))))
|
||||
|
||||
(def-test toast-dismiss ()
|
||||
(let ((*toasts* (list (make-instance 'toast :message "T" :variant :info))))
|
||||
(dismiss-toast (first *toasts*))
|
||||
(is (= 0 (length *toasts*)))))
|
||||
#+END_SRC
|
||||
|
||||
* Combined tangle blocks
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/dialog-package.lisp :noweb no
|
||||
;;; dialog-package.lisp — Package definition for cl-tty.dialog
|
||||
|
||||
(defpackage :cl-tty.dialog
|
||||
(:use :cl :cl-tty :cl-tty.select :cl-tty.input)
|
||||
(:export
|
||||
#:dialog
|
||||
#:dialog-title
|
||||
#:dialog-content
|
||||
#:dialog-on-dismiss
|
||||
#:dialog-size
|
||||
#:dialog-size-pixels
|
||||
#:render-dialog
|
||||
#:push-dialog
|
||||
#:pop-dialog
|
||||
#:*dialog-stack*
|
||||
#:alert-dialog
|
||||
#:confirm-dialog
|
||||
#:select-dialog
|
||||
#:prompt-dialog
|
||||
#:toast
|
||||
#:toast-message
|
||||
#:toast-variant
|
||||
#:render-toast
|
||||
#:dismiss-toast
|
||||
#:*toasts*
|
||||
;; Tests
|
||||
#:dialog-create
|
||||
#:dialog-size-small
|
||||
#:dialog-size-medium
|
||||
#:dialog-push-pop
|
||||
#:toast-create
|
||||
#:toast-dismiss))
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/dialog.lisp :noweb no
|
||||
;;; dialog.lisp — Dialog System + Toast for cl-tty
|
||||
|
||||
(in-package :cl-tty.dialog)
|
||||
|
||||
;; ─── Special variables ────────────────────────────────────────────────────────
|
||||
|
||||
(defvar *dialog-stack* nil
|
||||
"Stack of active dialogs. (list) of dialog instances.")
|
||||
|
||||
(defvar *toasts* nil
|
||||
"List of active toast notifications.")
|
||||
|
||||
;; ─── Dialog class ─────────────────────────────────────────────────────────────
|
||||
|
||||
(defclass dialog ()
|
||||
((title :initarg :title :accessor dialog-title)
|
||||
(size :initarg :size :initform :medium :accessor dialog-size)
|
||||
(content :initarg :content :accessor dialog-content)
|
||||
(on-dismiss :initarg :on-dismiss :initform nil :accessor dialog-on-dismiss)))
|
||||
|
||||
(defun dialog-size-pixels (size)
|
||||
(case size
|
||||
(:small (values 40 8))
|
||||
(:medium (values 60 16))
|
||||
(:large (values 88 24))
|
||||
(t (values 60 16))))
|
||||
|
||||
(defun render-dialog (dialog screen w h)
|
||||
(multiple-value-bind (dw dh) (dialog-size-pixels (dialog-size dialog))
|
||||
(let ((x (floor (- w dw) 2))
|
||||
(y (floor (- h dh) 2)))
|
||||
(dotimes (row h)
|
||||
(dotimes (col w)
|
||||
(backend-write screen col row " " :bg :dim)))
|
||||
(draw-border screen x y dw dh :single :title (dialog-title dialog))
|
||||
(when (dialog-content dialog)
|
||||
(render-component (dialog-content dialog) screen (1+ x) (1+ y) (- dw 2) (- dh 2))))))
|
||||
|
||||
(defun push-dialog (dialog)
|
||||
(push dialog *dialog-stack*)
|
||||
(when (typep (dialog-content dialog) 'focusable-mixin)
|
||||
(focus (dialog-content dialog)))
|
||||
dialog)
|
||||
|
||||
(defun pop-dialog ()
|
||||
(when *dialog-stack*
|
||||
(let ((dialog (pop *dialog-stack*)))
|
||||
(when (dialog-on-dismiss dialog)
|
||||
(funcall (dialog-on-dismiss dialog)))
|
||||
dialog)))
|
||||
|
||||
;; ─── Dialog sub-classes ──────────────────────────────────────────────────────
|
||||
|
||||
(defun alert-dialog (title message)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'select
|
||||
:options (list (list :title "OK" :value :ok))
|
||||
:on-select (lambda (opt) (declare (ignore opt)) (pop-dialog)))
|
||||
:on-dismiss (lambda () (pop-dialog))))
|
||||
|
||||
(defun confirm-dialog (title message &key on-yes on-no)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'select
|
||||
:options (list (list :title "Yes" :value :yes)
|
||||
(list :title "No" :value :no))
|
||||
:on-select (lambda (opt)
|
||||
(pop-dialog)
|
||||
(if (eql opt :yes)
|
||||
(when on-yes (funcall on-yes))
|
||||
(when on-no (funcall on-no)))))))
|
||||
|
||||
(defun select-dialog (title options &key on-select)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :medium
|
||||
:content (make-instance 'select
|
||||
:options options
|
||||
:on-select (lambda (opt)
|
||||
(pop-dialog)
|
||||
(when on-select (funcall on-select opt))))))
|
||||
|
||||
(defun prompt-dialog (title &key on-submit)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'text-input
|
||||
:on-submit (lambda (value)
|
||||
(pop-dialog)
|
||||
(when on-submit (funcall on-submit value))))))
|
||||
|
||||
;; ─── Toast system ─────────────────────────────────────────────────────────────
|
||||
|
||||
(defclass toast ()
|
||||
((message :initarg :message :accessor toast-message)
|
||||
(variant :initarg :variant :initform :info :accessor toast-variant)))
|
||||
|
||||
(defun render-toast (toast screen w)
|
||||
(let* ((msg (toast-message toast))
|
||||
(variant (toast-variant toast))
|
||||
(color (case variant
|
||||
(:info :blue) (:success :green)
|
||||
(:warning :yellow) (:error :red)))
|
||||
(max-w (min 60 (1- w)))
|
||||
(x (- w max-w 1))
|
||||
(text (if (> (length msg) (- max-w 2))
|
||||
(concatenate 'string (subseq msg 0 (- max-w 5)) "...")
|
||||
msg)))
|
||||
(draw-rect screen x 0 max-w 1 :bg color)
|
||||
(backend-write screen (1+ x) 0 text :fg :white :bold t)))
|
||||
|
||||
(defun toast (message &key (variant :info) (duration 5000))
|
||||
(let ((toast (make-instance 'toast :message message :variant variant)))
|
||||
(push toast *toasts*)
|
||||
(when (plusp duration)
|
||||
(schedule-event (+ (get-internal-real-time)
|
||||
(* duration 1000))
|
||||
(lambda () (dismiss-toast toast))))
|
||||
toast))
|
||||
|
||||
(defun dismiss-toast (toast)
|
||||
(setf *toasts* (remove toast *toasts*)))
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../tests/dialog-tests.lisp :noweb no
|
||||
;;; dialog-tests.lisp — Tests for cl-tty.dialog
|
||||
|
||||
(defpackage :cl-tty-dialog-test
|
||||
(:use :cl :cl-tty.dialog :fiveam))
|
||||
|
||||
(in-package :cl-tty-dialog-test)
|
||||
|
||||
(def-suite :dialog-suite :description "Dialog + Toast tests for cl-tty.dialog")
|
||||
(in-suite :dialog-suite)
|
||||
|
||||
(def-test dialog-create ()
|
||||
(let ((d (make-instance 'dialog :title "Test")))
|
||||
(is-true (typep d 'dialog))
|
||||
(is (equal "Test" (dialog-title d)))))
|
||||
|
||||
(def-test dialog-size-small ()
|
||||
(multiple-value-bind (w h) (dialog-size-pixels :small)
|
||||
(is (= 40 w))
|
||||
(is (= 8 h))))
|
||||
|
||||
(def-test dialog-size-medium ()
|
||||
(multiple-value-bind (w h) (dialog-size-pixels :medium)
|
||||
(is (= 60 w))
|
||||
(is (= 16 h))))
|
||||
|
||||
(def-test dialog-push-pop ()
|
||||
(let ((*dialog-stack* nil))
|
||||
(push-dialog (make-instance 'dialog :title "D1"))
|
||||
(is (= 1 (length *dialog-stack*)))
|
||||
(push-dialog (make-instance 'dialog :title "D2"))
|
||||
(is (= 2 (length *dialog-stack*)))
|
||||
(pop-dialog)
|
||||
(is (= 1 (length *dialog-stack*)))))
|
||||
|
||||
(def-test toast-create ()
|
||||
(let ((*toasts* nil))
|
||||
(toast "Hello" :variant :info :duration 0)
|
||||
(is (= 1 (length *toasts*)))))
|
||||
|
||||
(def-test toast-dismiss ()
|
||||
(let ((*toasts* (list (make-instance 'toast :message "T" :variant :info))))
|
||||
(dismiss-toast (first *toasts*))
|
||||
(is (= 0 (length *toasts*)))))
|
||||
#+END_SRC
|
||||
@@ -1,6 +1,6 @@
|
||||
#+TITLE: cl-tui Layout Engine — v0.0.3
|
||||
#+TITLE: cl-tty Layout Engine — v0.0.3
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :cl-tui:layout:v0.0.3:
|
||||
#+FILETAGS: :cl-tty:layout:v0.0.3:
|
||||
#+OPTIONS: ^:nil
|
||||
|
||||
* Layout Engine
|
||||
@@ -85,10 +85,10 @@ means a full Yoga FFI binding is unnecessary — ~200 lines of CL math.
|
||||
** Test Suite
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui-layout-test
|
||||
(:use :cl :fiveam :cl-tui.layout)
|
||||
(defpackage :cl-tty-layout-test
|
||||
(:use :cl :fiveam :cl-tty.layout)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-layout-test)
|
||||
(in-package :cl-tty-layout-test)
|
||||
|
||||
(def-suite layout-suite :description "Layout engine tests")
|
||||
(in-suite layout-suite)
|
||||
@@ -288,7 +288,7 @@ means a full Yoga FFI binding is unnecessary — ~200 lines of CL math.
|
||||
*** Package
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui.layout
|
||||
(defpackage :cl-tty.layout
|
||||
(:use :cl)
|
||||
(:export
|
||||
;; Classes
|
||||
@@ -306,7 +306,7 @@ means a full Yoga FFI binding is unnecessary — ~200 lines of CL math.
|
||||
#:compute-layout
|
||||
;; Macros
|
||||
#:vbox #:hbox #:spacer))
|
||||
(in-package :cl-tui.layout)
|
||||
(in-package :cl-tty.layout)
|
||||
#+END_SRC
|
||||
|
||||
*** Layout Node Class
|
||||
|
||||
500
org/markdown-renderer.org
Normal file
500
org/markdown-renderer.org
Normal file
@@ -0,0 +1,500 @@
|
||||
#+TITLE: Markdown + Code + Diff Rendering (v0.8.0)
|
||||
#+DATE: 2026-05-11
|
||||
#+AUTHOR: Amr Gharbeia / Hermes
|
||||
|
||||
* Overview
|
||||
|
||||
This module provides rendering of Markdown text, syntax-highlighted code
|
||||
blocks, and unified diffs in the terminal. It completes the rendering
|
||||
pipeline so that [[file:render.org][the render tree]] can handle rich formatted
|
||||
content.
|
||||
|
||||
The Markdown renderer is /not/ a general-purpose MD-to-HTML converter.
|
||||
It targets TUI output: node types that have clear terminal analogues
|
||||
(headings → bold/bright, code blocks → monochrome block, bold → ANSI
|
||||
bold, etc.). Edge cases that matter for a terminal (long lines, escape
|
||||
sequences inside code, mixed formatting) are handled explicitly.
|
||||
|
||||
** Design decisions
|
||||
|
||||
1. /Two-phase parse/: block-level first (lines), then inline (characters
|
||||
within each block). This matches how terminals render — block layout
|
||||
first, style within.
|
||||
2. /Syntax highlighting by keyword set/: not a full lexer. A lookup
|
||||
table of language → (keywords, types, builtins) sets. Catches ~90%
|
||||
of highlighting cases without pulling in a parser. Fails safe
|
||||
(unmatched tokens render as plain text).
|
||||
3. /Diff lines are self-describing/: a diff block starts with ─── or
|
||||
+++, each line has a ± prefix. We don't re-parse patch semantics;
|
||||
we just color by prefix. This makes the renderer tolerant of
|
||||
malformed diffs.
|
||||
4. /No recursive descent parser/: a simple state machine over lines for
|
||||
block-level, and a character cursor for inline. Keeps the code
|
||||
short and avoids parser-generator dependencies.
|
||||
|
||||
* Code structure
|
||||
|
||||
** Node types
|
||||
|
||||
We represent the parsed document as a tree of plists. Each node has at
|
||||
least a `:type` key. Block-level nodes carry a `:children` list of
|
||||
inline nodes. This keeps the data structure simple — no class hierarchy,
|
||||
no generic dispatch — while being easy to traverse for rendering.
|
||||
|
||||
Node types:
|
||||
|
||||
| Block-level | Inline |
|
||||
|------------------+--------------------|
|
||||
| `:heading` | `:text` |
|
||||
| `:paragraph` | `:bold` |
|
||||
| `:code-block` | `:italic` |
|
||||
| `:blockquote` | `:inline-code` |
|
||||
| `:list-item` | `:link` |
|
||||
| `:ordered-item` | |
|
||||
| `:thematic-break`| |
|
||||
| `:diff-block` | |
|
||||
|
||||
--- per-function: markdown-node-make
|
||||
|
||||
~make-md-node~ is a convenience constructor for node plists.
|
||||
It ensures `:children` defaults to NIL (not an empty list) so
|
||||
renderers can check `(if children ...)` without testing `(when
|
||||
children ...)` vs `(if (null children) ...)`.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun make-md-node (type &key children properties)
|
||||
"Create a markdown node plist.
|
||||
TYPE is a keyword like :heading or :bold.
|
||||
CHILDREN is a list of inline node plists (or NIL).
|
||||
PROPERTIES is a plist of node-specific extra keys (e.g. :level for headings)."
|
||||
(let ((node (list :type type)))
|
||||
(when children
|
||||
(setf (getf node :children) children))
|
||||
(when properties
|
||||
(setf (getf node :properties) properties))
|
||||
node))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: markdown-node-p
|
||||
|
||||
~md-node-p~ checks whether something is a markdown node plist.
|
||||
We just look for a :type key. This is used in tests and as
|
||||
a guard in recursive renderers.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun md-node-p (thing)
|
||||
"Return T if THING is a markdown node (has a :type key)."
|
||||
(and (listp thing) (getf thing :type)))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: markdown-node-text
|
||||
|
||||
~md-node-text~ extracts the plain text from a node tree by
|
||||
concatenating all :text children recursively, discarding markup.
|
||||
This is useful for things like heading anchors, tooltip strings,
|
||||
or search indexing.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun md-node-text (node)
|
||||
"Recursively extract plain text from a markdown node tree."
|
||||
(let ((type (getf node :type)))
|
||||
(cond ((eql type :text)
|
||||
(or (getf node :content) ""))
|
||||
((eql type :link)
|
||||
(concatenate 'string
|
||||
(md-node-text (first (getf node :children)))
|
||||
(format nil " (~a)" (or (getf node :url) ""))))
|
||||
((getf node :children)
|
||||
(apply #'concatenate 'string
|
||||
(mapcar #'md-node-text (getf node :children))))
|
||||
(t ""))))
|
||||
#+END_SRC
|
||||
|
||||
** Block-level parser
|
||||
|
||||
The block parser operates line-by-line with a simple state machine.
|
||||
Each line is classified by its prefix characters, then accumulated
|
||||
into a node.
|
||||
|
||||
Rules:
|
||||
- Lines starting with `#` → heading (count hashes for level)
|
||||
- Lines starting with `>` → blockquote (continuation lines merge)
|
||||
- Lines starting with `-`, `*`, or `+` → list-item
|
||||
- Lines starting with 1-3 digits followed by `.` → ordered-item
|
||||
- Lines starting with `` ``` `` → code-block (language on opening line)
|
||||
- Lines starting with `---` or `***` → thematic-break
|
||||
- Lines starting with `--- ` or `+++ ` → diff-block
|
||||
- Empty lines → paragraph boundary
|
||||
- Everything else → paragraph (continuation lines merge until blank)
|
||||
|
||||
--- per-function: classify-line
|
||||
|
||||
~classify-line~ returns a keyword and a data value for a trimmed
|
||||
line of text. The state machine uses this to decide what kind of
|
||||
block to create or continue.
|
||||
|
||||
The function must handle prefix stripping (e.g. remove `# ` after
|
||||
counting hashes) and edge cases like `#` inside a code block (which
|
||||
we don't classify at all — the code block state machine handles that).
|
||||
|
||||
One trap: a line like `#not-a-heading` (no space after hash) is NOT
|
||||
a heading in CommonMark. We check for space/tab after the hashes.
|
||||
|
||||
Another trap: `* item` in a list vs `**bold**` inline. At the
|
||||
block-parser level we only look at /line-start/ `* ` (star + space)
|
||||
for list items. A line starting with `** text` could be either a
|
||||
nested list item or bold text in a paragraph — we conservatively
|
||||
treat it as a list-item (the inline parser will handle ** inside
|
||||
paragraphs normally).
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun classify-line (line)
|
||||
"Classify a trimmed LINE, returning (type . data).
|
||||
TYPE is a keyword; DATA is language for code-blocks, level for headings, etc."
|
||||
(cond
|
||||
;; Empty line
|
||||
((string= line "") (cons :blank nil))
|
||||
;; Thematic break: --- or *** (3+ chars, all same, optional whitespace)
|
||||
((and (>= (length line) 3)
|
||||
(every (lambda (c) (or (char= c (char line 0))
|
||||
(char= c #\Space)
|
||||
(char= c #\Tab)))
|
||||
line)
|
||||
(find (char line 0) "-*"))
|
||||
(cons :thematic-break nil))
|
||||
;; Heading: #+, with space after hashes
|
||||
((and (char= (char line 0) #\#)
|
||||
(let ((count 0))
|
||||
(loop for c across line
|
||||
while (char= c #\#)
|
||||
do (incf count))
|
||||
(and (<= 1 count 6)
|
||||
(or (>= (length line) (1+ count))
|
||||
(member (char line count) '(#\Space #\Tab))))))
|
||||
(let* ((hash-count (loop for c across line while (char= c #\#) count c))
|
||||
(content (string-trim (list #\Space #\Tab)
|
||||
(subseq line hash-count))))
|
||||
(cons :heading (cons hash-count content))))
|
||||
;; Blockquote: >
|
||||
((and (>= (length line) 1) (char= (char line 0) #\>))
|
||||
(let ((content (string-trim (list #\Space #\Tab)
|
||||
(subseq line 1))))
|
||||
(cons :blockquote content)))
|
||||
;; Unordered list: -, *, +
|
||||
((and (>= (length line) 2)
|
||||
(find (char line 0) "-*+")
|
||||
(char= (char line 1) #\Space))
|
||||
(cons :list-item (string-trim (list #\Space #\Tab) (subseq line 2))))
|
||||
;; Ordered list: N. or N)
|
||||
((and (>= (length line) 3)
|
||||
(digit-char-p (char line 0))
|
||||
(loop for c across line
|
||||
while (digit-char-p c)
|
||||
finally (return (find c '(#\. #\) #\Space)))))
|
||||
(let ((dot-pos (position-if (lambda (c) (find c ". )")) line)))
|
||||
(if (and dot-pos (find (char line dot-pos) ". )"))
|
||||
(cons :ordered-item (string-trim (list #\Space #\Tab)
|
||||
(subseq line (1+ dot-pos))))
|
||||
(cons :paragraph line))))
|
||||
;; Diff: --- file or +++ file
|
||||
((and (>= (length line) 4)
|
||||
(find (char line 0) "-+")
|
||||
(char= (char line 1) (char line 0))
|
||||
(char= (char line 2) (char line 0))
|
||||
(char= (char line 3) #\Space))
|
||||
(cons :diff-header line))
|
||||
;; Diff: line content with +/- prefix
|
||||
((and (>= (length line) 1)
|
||||
(find (char line 0) "-+")
|
||||
(not (and (>= (length line) 3)
|
||||
(char= (char line 1) (char line 0))
|
||||
(char= (char line 2) (char line 0)))))
|
||||
(cons :diff-line (cons (char line 0) (subseq line 1))))
|
||||
;; Fenced code block start: ``` or ~~~
|
||||
((and (>= (length line) 3)
|
||||
(find (char line 0) "`~")
|
||||
(every (lambda (c) (char= c (char line 0)))
|
||||
(subseq line 0 (min 6 (length line))))
|
||||
(let ((rest (string-trim (list #\Space #\Tab) (subseq line (min 6 (length line))))))
|
||||
(cons :code-start rest))))
|
||||
;; Default: paragraph content
|
||||
(t (cons :paragraph line))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: parse-blocks
|
||||
|
||||
~parse-blocks~ is the main block-level parser. It takes a string
|
||||
(possibly multi-line) and returns a list of markdown node plists.
|
||||
|
||||
The algorithm:
|
||||
1. Split into lines
|
||||
2. Classify each line
|
||||
3. Accumulate lines of the same type into groups
|
||||
4. Convert each group into a node
|
||||
|
||||
State transitions:
|
||||
- `:paragraph` accumulates until blank line or different block type
|
||||
- `:blockquote` accumulates until blank line
|
||||
- `:list-item` and `:ordered-item` accumulate until blank line
|
||||
- `:code-start` flips to code-block mode; accumulates until matching
|
||||
fence closer or end of input
|
||||
- `:diff-header` starts a diff block; diff lines accumulate until
|
||||
blank line or non-diff line
|
||||
|
||||
Edge case: a paragraph followed by a list item should stay as
|
||||
separate blocks (not merge). The blank-line check handles this
|
||||
because the paragraph only continues for non-blank, non-list lines.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun parse-blocks (text)
|
||||
"Parse TEXT (a string) into a list of block-level markdown node plists.
|
||||
Returns (nodes . unconsumed-lines) for recursive callers."
|
||||
(let ((lines (split-string-into-lines text))
|
||||
(nodes nil)
|
||||
(i 0))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((line (string-trim (list #\return) (aref lines i)))
|
||||
(classification (classify-line line)))
|
||||
(case (car classification)
|
||||
(:blank (incf i))
|
||||
(:thematic-break
|
||||
(push (make-md-node :thematic-break) nodes)
|
||||
(incf i))
|
||||
(:paragraph
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-paragraph lines i)
|
||||
(push node nodes)
|
||||
(setf i consumed)))
|
||||
(:heading
|
||||
(let* ((level-and-content (cdr classification))
|
||||
(level (car level-and-content))
|
||||
(content (cdr level-and-content)))
|
||||
(push (make-md-node :heading
|
||||
:properties (list :level level)
|
||||
:children (parse-inline content))
|
||||
nodes)
|
||||
(incf i)))
|
||||
(:blockquote
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-blockquote lines i)
|
||||
(push node nodes)
|
||||
(setf i consumed)))
|
||||
(:list-item
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-list lines i :unordered)
|
||||
(push node nodes)
|
||||
(setf i consumed)))
|
||||
(:ordered-item
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-list lines i :ordered)
|
||||
(push node nodes)
|
||||
(setf i consumed)))
|
||||
(:code-start
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-code-block lines i (cdr classification))
|
||||
(push node nodes)
|
||||
(setf i consumed)))
|
||||
(:diff-header
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-diff-block lines i)
|
||||
(push node nodes)
|
||||
(setf i consumed)))
|
||||
(t (incf i)))))
|
||||
;; Return in reading order
|
||||
(nreverse nodes)))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: split-string-into-lines
|
||||
|
||||
~split-string-into-lines~ is a utility rather than relying on
|
||||
~cl-ppcre~ (which we don't depend on). It splits on #\Newline
|
||||
and handles the edge case of trailing newlines (doesn't produce
|
||||
an extra empty line at the end).
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun split-string-into-lines (string)
|
||||
"Split STRING into a vector of lines (no trailing newline).
|
||||
Handles \\n, \\r\\n, and trailing newlines properly."
|
||||
(let ((result nil)
|
||||
(start 0))
|
||||
(flet ((add-line (end)
|
||||
(push (subseq string start end) result)))
|
||||
(loop for i from 0 below (length string)
|
||||
do (let ((c (char string i)))
|
||||
(cond ((char= c #\Newline)
|
||||
(add-line i)
|
||||
(setf start (1+ i)))
|
||||
((and (char= c #\Return)
|
||||
(< (1+ i) (length string))
|
||||
(char= (char string (1+ i)) #\Newline))
|
||||
(add-line i)
|
||||
(setf start (+ i 2))
|
||||
(incf i)))))
|
||||
(when (< start (length string))
|
||||
(add-line (length string)))
|
||||
(coerce (nreverse result) 'vector))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: parse-paragraph
|
||||
|
||||
~parse-paragraph~ collects one or more contiguous paragraph lines
|
||||
until a blank line or a different block type. It joins them with
|
||||
spaces (for hard-wrapped prose) and returns a :paragraph node
|
||||
with inline-parsed children.
|
||||
|
||||
Continuation lines in paragraphs are joined with a single space
|
||||
(not a newline). This is correct for Markdown's soft-wrap
|
||||
convention where a newline in source = space in output. To force
|
||||
a hard break, CommonMark uses two trailing spaces — we skip that
|
||||
for now since it's rare in TUI contexts.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun parse-paragraph (lines start)
|
||||
"Parse contiguous paragraph lines from LINES starting at START.
|
||||
Returns (node . consumed-index)."
|
||||
(let ((text-parts nil)
|
||||
(i start))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
((:paragraph)
|
||||
(push (cdr class) text-parts)
|
||||
(incf i))
|
||||
(:blank (incf i) (loop-finish))
|
||||
(t (loop-finish)))))
|
||||
(let ((text (with-output-to-string (s)
|
||||
(loop for part in (nreverse text-parts)
|
||||
for first = t then nil
|
||||
do (unless first (write-char #\Space s))
|
||||
(princ part s)))))
|
||||
(cons (make-md-node :paragraph
|
||||
:children (parse-inline text))
|
||||
i))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: parse-blockquote
|
||||
|
||||
~parse-blockquote~ collects contiguous `>` lines, strips the `>`
|
||||
prefix, joins them, and wraps in a :blockquote node. Nested
|
||||
blockquotes (`> >`) are not supported in this version — a `>` at
|
||||
the start of the content is treated as literal text.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun parse-blockquote (lines start)
|
||||
"Parse contiguous blockquote lines from LINES starting at START.
|
||||
Returns (node . consumed-index)."
|
||||
(let ((text-parts nil)
|
||||
(i start))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
(:blockquote
|
||||
(push (cdr class) text-parts)
|
||||
(incf i))
|
||||
(:blank (incf i) (loop-finish))
|
||||
(t (loop-finish)))))
|
||||
(let ((text (with-output-to-string (s)
|
||||
(loop for part in (nreverse text-parts)
|
||||
for first = t then nil
|
||||
do (unless first (write-char #\Space s))
|
||||
(princ part s)))))
|
||||
(cons (make-md-node :blockquote
|
||||
:children (parse-inline text))
|
||||
i))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: parse-list
|
||||
|
||||
~parse-list~ collects contiguous list items (same type) and returns
|
||||
a list of nodes. Each line starting with a list marker becomes one
|
||||
list-item node. Nested lists are not supported (lines starting with
|
||||
two spaces + marker would be the next level — we skip that for v1).
|
||||
|
||||
The TYPE parameter is either `:unordered` or `:ordered` — though
|
||||
we return each item labeled by its actual marker type since we
|
||||
already classified each line.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun parse-list (lines start type)
|
||||
"Parse contiguous list items from LINES starting at START.
|
||||
TYPE is :unordered or :ordered.
|
||||
Returns (node . consumed-index) where node is a :list-item or :ordered-item."
|
||||
(declare (ignore type))
|
||||
(let ((items nil)
|
||||
(i start))
|
||||
;; Collect all contiguous list items into ITEMS
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
((:list-item :ordered-item)
|
||||
(push (cons (car class) (cdr class)) items)
|
||||
(incf i))
|
||||
(:blank
|
||||
;; One blank line between items is OK; two ends the list
|
||||
(if (and (< (1+ i) (length lines))
|
||||
(let ((next-class (classify-line
|
||||
(string-trim
|
||||
(list #\return)
|
||||
(aref lines (1+ i))))))
|
||||
(member (car next-class)
|
||||
'(:list-item :ordered-item))))
|
||||
(progn
|
||||
(push (cons :blank-sep nil) items)
|
||||
(incf i))
|
||||
(progn (incf i) (loop-finish))))
|
||||
(t (loop-finish)))))
|
||||
;; Convert each item to a node
|
||||
(let ((nodes nil))
|
||||
(dolist (item (nreverse items))
|
||||
(let ((type (car item))
|
||||
(content (cdr item)))
|
||||
(when (and content (not (string= content "")))
|
||||
(push (make-md-node type
|
||||
:children (parse-inline content))
|
||||
nodes))))
|
||||
(cons (nreverse nodes) i))))
|
||||
#+END_SRC
|
||||
|
||||
--- per-function: parse-code-block
|
||||
|
||||
~parse-code-block~ reads from the line after the opening fence to
|
||||
the closing fence (or end of input). It returns a :code-block node
|
||||
with the language (or NIL) and the raw text as the :content. No
|
||||
inline parsing is done inside code blocks — everything is literal.
|
||||
|
||||
Matching fence: if opened with `` ``` ``, close with `` ``` ``.
|
||||
If opened with `~~~`, close with `~~~`. The closing fence must have
|
||||
at least as many backticks/tildes as the opening fence (CommonMark
|
||||
rule). We use the simpler version: same character, same count.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle no
|
||||
(defun parse-code-block (lines start lang)
|
||||
"Parse a fenced code block from LINES starting at START.
|
||||
LANG is the language string (or empty string) from the opening fence.
|
||||
Returns (node . consumed-index)."
|
||||
(let ((code-lines nil)
|
||||
(i (1+ start))
|
||||
(fence-char (char (aref lines start) 0))
|
||||
(fence-len (loop for c across (aref lines start)
|
||||
while (char= c (char (aref lines start) 0))
|
||||
count c))
|
||||
(found-close nil))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line)))
|
||||
;; Check for closing fence
|
||||
(when (and (>= (length line) fence-len)
|
||||
(every (lambda (c) (char= c fence-char))
|
||||
(subseq line 0 fence-len))
|
||||
(or (= (length line) fence-len)
|
||||
(every (lambda (c) (find c " \t"))
|
||||
(subseq line fence-len))))
|
||||
(setf found-close t)
|
||||
(incf i)
|
||||
(loop-finish))
|
||||
@@ -1,6 +1,6 @@
|
||||
#+TITLE: cl-tui Modern Backend — v0.0.2
|
||||
#+TITLE: cl-tty Modern Backend — v0.0.2
|
||||
#+STARTUP: content
|
||||
#+FILETAGS: :cl-tui:backend:v0.0.2:
|
||||
#+FILETAGS: :cl-tty:backend:v0.0.2:
|
||||
#+OPTIONS: ^:nil
|
||||
|
||||
* Modern Backend
|
||||
@@ -40,10 +40,10 @@ Colors are resolved through a palette before emission:
|
||||
** Test Suite
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui-modern-backend-test
|
||||
(:use :cl :fiveam :cl-tui.backend)
|
||||
(defpackage :cl-tty-modern-backend-test
|
||||
(:use :cl :fiveam :cl-tty.backend)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-modern-backend-test)
|
||||
(in-package :cl-tty-modern-backend-test)
|
||||
|
||||
(def-suite modern-backend-suite :description "Modern backend tests")
|
||||
(in-suite modern-backend-suite)
|
||||
@@ -58,72 +58,72 @@ Colors are resolved through a palette before emission:
|
||||
(test make-modern-backend-creates
|
||||
"make-modern-backend returns a modern-backend instance"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (typep b 'cl-tui.backend::modern-backend))))
|
||||
(is (typep b 'cl-tty.backend::modern-backend))))
|
||||
|
||||
;; ── Escape Generation ──────────────────────────────────────────
|
||||
|
||||
(test sgr-truecolor-foreground
|
||||
"SGR truecolor foreground escape is correct"
|
||||
(is (equal (cl-tui.backend::sgr-fg "#FFD700")
|
||||
(is (equal (cl-tty.backend::sgr-fg "#FFD700")
|
||||
(format nil "~C[38;2;255;215;0m" #\Esc))))
|
||||
|
||||
(test sgr-truecolor-background
|
||||
"SGR truecolor background escape is correct"
|
||||
(is (equal (cl-tui.backend::sgr-bg "#1a1b26")
|
||||
(is (equal (cl-tty.backend::sgr-bg "#1a1b26")
|
||||
(format nil "~C[48;2;26;27;38m" #\Esc))))
|
||||
|
||||
(test sgr-named-colors
|
||||
"SGR named colors resolve to 8-color codes"
|
||||
(is (equal (cl-tui.backend::sgr-fg :red)
|
||||
(is (equal (cl-tty.backend::sgr-fg :red)
|
||||
(format nil "~C[31m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-bg :blue)
|
||||
(is (equal (cl-tty.backend::sgr-bg :blue)
|
||||
(format nil "~C[44m" #\Esc))))
|
||||
|
||||
(test sgr-bold-italic
|
||||
"SGR attribute escapes are correct"
|
||||
(is (equal (cl-tui.backend::sgr-attr :bold) (format nil "~C[1m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-attr :italic) (format nil "~C[3m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-attr :underline) (format nil "~C[4m" #\Esc)))
|
||||
(is (equal (cl-tui.backend::sgr-attr :reset) (format nil "~C[0m" #\Esc))))
|
||||
(is (equal (cl-tty.backend::sgr-attr :bold) (format nil "~C[1m" #\Esc)))
|
||||
(is (equal (cl-tty.backend::sgr-attr :italic) (format nil "~C[3m" #\Esc)))
|
||||
(is (equal (cl-tty.backend::sgr-attr :underline) (format nil "~C[4m" #\Esc)))
|
||||
(is (equal (cl-tty.backend::sgr-attr :reset) (format nil "~C[0m" #\Esc))))
|
||||
|
||||
;; ── Cursor ─────────────────────────────────────────────────────
|
||||
|
||||
(test cursor-move-escape
|
||||
"cursor-move generates correct CSI escape"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-move-escape 5 10)
|
||||
(is (equal (cl-tty.backend::cursor-move-escape 5 10)
|
||||
(format nil "~C[6;11H" #\Esc)))))
|
||||
|
||||
(test cursor-style-block
|
||||
"cursor-style :block generate correct escape"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-style-escape :block nil)
|
||||
(is (equal (cl-tty.backend::cursor-style-escape :block nil)
|
||||
(format nil "~C[2 q" #\Esc)))))
|
||||
|
||||
(test cursor-style-bar
|
||||
"cursor-style :bar generate correct escape"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-style-escape :bar nil)
|
||||
(is (equal (cl-tty.backend::cursor-style-escape :bar nil)
|
||||
(format nil "~C[6 q" #\Esc)))))
|
||||
|
||||
(test cursor-style-underline-blink
|
||||
"cursor-style :underline with blink"
|
||||
(let ((b (make-modern-backend)))
|
||||
(is (equal (cl-tui.backend::cursor-style-escape :underline t)
|
||||
(is (equal (cl-tty.backend::cursor-style-escape :underline t)
|
||||
(format nil "~C[5 q" #\Esc)))))
|
||||
|
||||
;; ── Synchronization ────────────────────────────────────────────
|
||||
|
||||
(test decicm-escapes
|
||||
"DECICM synchronized update escapes"
|
||||
(is (equal (cl-tui.backend::decicm-begin) (format nil "~C[?2026h" #\Esc)))
|
||||
(is (equal (cl-tui.backend::decicm-end) (format nil "~C[?2026l" #\Esc))))
|
||||
(is (equal (cl-tty.backend::decicm-begin) (format nil "~C[?2026h" #\Esc)))
|
||||
(is (equal (cl-tty.backend::decicm-end) (format nil "~C[?2026l" #\Esc))))
|
||||
|
||||
;; ── OSC 8 Hyperlinks ──────────────────────────────────────────
|
||||
|
||||
(test osc8-escape
|
||||
"OSC 8 hyperlink escape wraps text"
|
||||
(is (equal (cl-tui.backend::osc8-link "http://example.com" "click here")
|
||||
(is (equal (cl-tty.backend::osc8-link "http://example.com" "click here")
|
||||
(format nil "~C]8;;http://example.com~C\\click here~C]8;;~C\\"
|
||||
#\Esc #\Esc #\Esc #\Esc))))
|
||||
|
||||
@@ -131,21 +131,21 @@ Colors are resolved through a palette before emission:
|
||||
|
||||
(test hex-color-parsing
|
||||
"hex-to-rgb parses valid hex colors"
|
||||
(multiple-value-bind (r g b) (cl-tui.backend::hex-to-rgb "#FFD700")
|
||||
(multiple-value-bind (r g b) (cl-tty.backend::hex-to-rgb "#FFD700")
|
||||
(is (= r 255))
|
||||
(is (= g 215))
|
||||
(is (= b 0))))
|
||||
|
||||
(test hex-color-black
|
||||
"hex-to-rgb parses black"
|
||||
(multiple-value-bind (r g b) (cl-tui.backend::hex-to-rgb "#000000")
|
||||
(multiple-value-bind (r g b) (cl-tty.backend::hex-to-rgb "#000000")
|
||||
(is (= r 0))
|
||||
(is (= g 0))
|
||||
(is (= b 0))))
|
||||
|
||||
(test hex-color-short-form
|
||||
"hex-to-rgb parses 3-digit hex"
|
||||
(multiple-value-bind (r g b) (cl-tui.backend::hex-to-rgb "#F00")
|
||||
(multiple-value-bind (r g b) (cl-tty.backend::hex-to-rgb "#F00")
|
||||
(is (= r 255))
|
||||
(is (= g 0))
|
||||
(is (= b 0))))
|
||||
@@ -154,23 +154,23 @@ Colors are resolved through a palette before emission:
|
||||
|
||||
(test border-char-rounded
|
||||
"modern-border-char returns Unicode box-drawing for rounded style"
|
||||
(is (equal (cl-tui.backend::border-char :rounded :top-left) "╭"))
|
||||
(is (equal (cl-tui.backend::border-char :rounded :horizontal) "─"))
|
||||
(is (equal (cl-tui.backend::border-char :rounded :vertical) "│"))
|
||||
(is (equal (cl-tui.backend::border-char :rounded :bottom-right) "╯")))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :top-left) "╭"))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :horizontal) "─"))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :vertical) "│"))
|
||||
(is (equal (cl-tty.backend::border-char :rounded :bottom-right) "╯")))
|
||||
|
||||
(test border-char-double
|
||||
"modern-border-char returns double-line chars"
|
||||
(is (equal (cl-tui.backend::border-char :double :top-left) "╔"))
|
||||
(is (equal (cl-tui.backend::border-char :double :horizontal) "═"))
|
||||
(is (equal (cl-tui.backend::border-char :double :vertical) "║")))
|
||||
(is (equal (cl-tty.backend::border-char :double :top-left) "╔"))
|
||||
(is (equal (cl-tty.backend::border-char :double :horizontal) "═"))
|
||||
(is (equal (cl-tty.backend::border-char :double :vertical) "║")))
|
||||
#+END_SRC
|
||||
|
||||
** Implementation
|
||||
|
||||
*** Package
|
||||
|
||||
Add to =cl-tui.backend= package:
|
||||
Add to =cl-tty.backend= package:
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
;; In package.lisp, add to :export:
|
||||
@@ -179,7 +179,7 @@ Add to =cl-tui.backend= package:
|
||||
;; sgr-fg sgr-bg sgr-attr cursor-move-escape cursor-style-escape
|
||||
;; decicm-begin decicm-end osc8-link hex-to-rgb border-char
|
||||
|
||||
(in-package :cl-tui.backend)
|
||||
(in-package :cl-tty.backend)
|
||||
#+END_SRC
|
||||
|
||||
*** Color Resolution
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#+TITLE: cl-tui v0.6.0 — ScrollBox + TabBar
|
||||
#+TITLE: cl-tty v0.6.0 — ScrollBox + TabBar
|
||||
#+STARTUP: content
|
||||
|
||||
* ScrollBox and TabBar
|
||||
@@ -47,10 +47,10 @@ TabBar:
|
||||
** Tests
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../tests/scrollbox-tabbar-tests.lisp
|
||||
(defpackage :cl-tui-scrollbox-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input :cl-tui.container)
|
||||
(defpackage :cl-tty-scrollbox-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input :cl-tty.container)
|
||||
(:export #:run-tests))
|
||||
(in-package #:cl-tui-scrollbox-test)
|
||||
(in-package #:cl-tty-scrollbox-test)
|
||||
|
||||
(def-suite scrollbox-suite :description "ScrollBox + TabBar tests")
|
||||
(in-suite scrollbox-suite)
|
||||
@@ -182,8 +182,8 @@ TabBar:
|
||||
** Package
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui.container
|
||||
(:use :cl :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input)
|
||||
(defpackage :cl-tty.container
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export
|
||||
;; ScrollBox
|
||||
#:scroll-box #:make-scroll-box
|
||||
@@ -209,7 +209,7 @@ The constructor accepts keyword arguments for initial offset and children.
|
||||
~children~ defaults to an empty list.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui.container)
|
||||
(in-package #:cl-tty.container)
|
||||
|
||||
(defclass scroll-box (dirty-mixin)
|
||||
((children :initform nil :initarg :children
|
||||
@@ -415,7 +415,7 @@ and the currently active tab id. ~tab-bar-add~ creates a new tab with
|
||||
the given id and title, returns the id.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui.container)
|
||||
(in-package #:cl-tty.container)
|
||||
|
||||
(defclass tab-bar (dirty-mixin)
|
||||
((tabs :initform nil :initarg :tabs
|
||||
@@ -532,7 +532,7 @@ they are truncated with an ellipsis.
|
||||
** Combined tangle blocks
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/scrollbox.lisp
|
||||
(in-package #:cl-tui.container)
|
||||
(in-package #:cl-tty.container)
|
||||
|
||||
(defclass scroll-box (dirty-mixin)
|
||||
((children :initform nil :initarg :children :accessor scroll-box-children :type list)
|
||||
@@ -616,7 +616,7 @@ they are truncated with an ellipsis.
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/tabbar.lisp
|
||||
(in-package #:cl-tui.container)
|
||||
(in-package #:cl-tty.container)
|
||||
|
||||
(defclass tab-bar (dirty-mixin)
|
||||
((tabs :initform nil :initarg :tabs :accessor tab-bar-tabs :type list)
|
||||
@@ -670,8 +670,8 @@ they are truncated with an ellipsis.
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/container-package.lisp
|
||||
(defpackage :cl-tui.container
|
||||
(:use :cl :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input)
|
||||
(defpackage :cl-tty.container
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export
|
||||
#:scroll-box #:make-scroll-box
|
||||
#:scroll-box-scroll-y #:scroll-box-scroll-x
|
||||
|
||||
543
org/select.org
Normal file
543
org/select.org
Normal file
@@ -0,0 +1,543 @@
|
||||
#+TITLE: cl-tty v0.7.0 — Select Dropdown + Fuzzy Filter
|
||||
#+STARTUP: content
|
||||
|
||||
* Select Widget
|
||||
|
||||
A selection list component — the building block for command palettes, theme
|
||||
pickers, agent selectors, and file pickers. Options are plists with ~:title~,
|
||||
~:value~, and optional ~:category~ fields.
|
||||
|
||||
The widget supports keyboard navigation (Up/Down, Ctrl+P/N, Enter, Esc),
|
||||
option filtering by case-insensitive substring match with trigram fuzzy
|
||||
fallback, and category grouping with dimmed headers.
|
||||
|
||||
** Contract
|
||||
|
||||
~select~ class — slots: options, filter, on-select, selected-index, layout-node.
|
||||
|
||||
~make-select &key options filter on-select~ → select instance.
|
||||
|
||||
~select-options sel~ / ~(setf select-options)~ — list of option plists.
|
||||
~select-filter sel~ / ~(setf select-filter)~ — filter string or nil.
|
||||
~select-selected-index sel~ / ~(setf select-selected-index)~ — currently highlighted index.
|
||||
~select-on-select sel~ / ~(setf select-on-select)~ — callback fn (receives option plist).
|
||||
~select-layout-node sel~ / ~(setf select-layout-node)~ — layout node.
|
||||
|
||||
~select-filtered-options sel~ → list of options matching the filter.
|
||||
Returns all options when filter is nil. Matches title (case-insensitive).
|
||||
Falls back to trigram fuzzy matching when no exact substring matches.
|
||||
|
||||
~select-next sel~ / ~select-prev sel~ — move selection forward/backward,
|
||||
skipping category headers. Wraps around at boundaries.
|
||||
|
||||
~select-visible-options sel~ → filtered options visible in viewport.
|
||||
Uses available-height from layout node. Culls like ScrollBox.
|
||||
|
||||
~select-handle-key sel event~ → T if handled.
|
||||
Down/Ctrl+N → next. Up/Ctrl+P → prev. Enter → on-select callback. Esc → nil.
|
||||
|
||||
~render ((sel select) backend)~ — renders visible options with selection highlight.
|
||||
|
||||
** Tests
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../tests/select-tests.lisp
|
||||
(defpackage :cl-tty-select-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input :cl-tty.select)
|
||||
(:export #:run-tests))
|
||||
(in-package #:cl-tty-select-test)
|
||||
|
||||
(def-suite select-suite :description "Select widget tests")
|
||||
(in-suite select-suite)
|
||||
|
||||
(defun run-tests ()
|
||||
(let ((result (run 'select-suite)))
|
||||
(fiveam:explain! result)
|
||||
(uiop:quit 0)))
|
||||
|
||||
(test select-creates
|
||||
"A Select can be created with defaults."
|
||||
(let ((sel (make-select)))
|
||||
(is (typep sel 'select))
|
||||
(is-false (select-options sel))
|
||||
(is-false (select-filter sel))
|
||||
(is (= (select-selected-index sel) 0))))
|
||||
|
||||
(test select-with-options
|
||||
"A Select stores options."
|
||||
(let ((sel (make-select :options '((:title "Red" :value :red)
|
||||
(:title "Blue" :value :blue)))))
|
||||
(is (= (length (select-options sel)) 2))))
|
||||
|
||||
(test select-filtered-exact
|
||||
"Filter returns case-insensitive substring matches."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Red" :value :red)
|
||||
(:title "Green" :value :green)
|
||||
(:title "Blue" :value :blue)))))
|
||||
(setf (select-filter sel) "bl")
|
||||
(let ((filtered (select-filtered-options sel)))
|
||||
(is (= (length filtered) 1))
|
||||
(is (eql (getf (third (first filtered)) :value) :blue)))))
|
||||
|
||||
(test select-filtered-all
|
||||
"Nil filter returns all options."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Red" :value :red)
|
||||
(:title "Blue" :value :blue)))))
|
||||
(let ((filtered (select-filtered-options sel)))
|
||||
(is (= (length filtered) 2)))))
|
||||
|
||||
(test select-navigation
|
||||
"Select-next and select-prev navigate through options."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "A" :value :a)
|
||||
(:title "B" :value :b)
|
||||
(:title "C" :value :c)))))
|
||||
(is (= (select-selected-index sel) 0))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 1))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 2))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 0) "wraps forward")
|
||||
(select-prev sel)
|
||||
(is (= (select-selected-index sel) 2) "wraps backward")))
|
||||
|
||||
(test select-navigation-skips-categories
|
||||
"Navigation skips category header options."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Colors" :category t)
|
||||
(:title "Red" :value :red)
|
||||
(:title "Green" :value :green)
|
||||
(:title "Shapes" :category t)
|
||||
(:title "Circle" :value :circle)))))
|
||||
(is (= (select-selected-index sel) 0))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 1) "skipped category header at 0")
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 2))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 4) "skipped category header at 3")))
|
||||
|
||||
(test select-handle-key
|
||||
"Select handle-key dispatches navigation and selection."
|
||||
(let* ((result (list nil))
|
||||
(sel (make-select
|
||||
:options '((:title "A" :value :a) (:title "B" :value :b))
|
||||
:on-select (lambda (opt) (setf (car result) (getf opt :value))))))
|
||||
(select-handle-key sel (make-key-event :key :down))
|
||||
(is (= (select-selected-index sel) 1))
|
||||
(select-handle-key sel (make-key-event :key :up))
|
||||
(is (= (select-selected-index sel) 0))
|
||||
(select-handle-key sel (make-key-event :key :enter))
|
||||
(is (eql (car result) :a))))
|
||||
|
||||
(test select-handle-key-ctrl
|
||||
"Ctrl+N and Ctrl+P navigate like down/up."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "A" :value :a) (:title "B" :value :b) (:title "C" :value :c)))))
|
||||
(select-handle-key sel (make-key-event :key :n :ctrl t))
|
||||
(is (= (select-selected-index sel) 1))
|
||||
(select-handle-key sel (make-key-event :key :p :ctrl t))
|
||||
(is (= (select-selected-index sel) 0))))
|
||||
|
||||
(test select-visible-count
|
||||
"Visible options respects viewport height."
|
||||
(let* ((ln (make-layout-node))
|
||||
(sel (make-select
|
||||
:options (loop for i below 20 collect (list :title (format nil "Item ~D" i) :value i)))))
|
||||
(setf (select-layout-node sel) ln)
|
||||
(setf (layout-node-height ln) 5)
|
||||
(let ((visible (select-visible-options sel)))
|
||||
(is (<= (length visible) 5)))))
|
||||
|
||||
(test select-fuzzy-fallback
|
||||
"Fuzzy filter catches near-misses."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Nord" :value :nord)
|
||||
(:title "Tokyo Night" :value :tokyo)
|
||||
(:title "Catppuccin" :value :cat)))))
|
||||
(setf (select-filter sel) "nrd")
|
||||
(let ((filtered (select-filtered-options sel)))
|
||||
(is (= (length filtered) 1))
|
||||
(is (eql (getf (third (first filtered)) :value) :nord)))))
|
||||
#+END_SRC
|
||||
|
||||
* Implementation
|
||||
|
||||
** Package
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tty.select
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export
|
||||
#:select #:make-select
|
||||
#:select-options #:select-filter
|
||||
#:select-selected-index #:select-on-select
|
||||
#:select-layout-node
|
||||
#:select-filtered-options
|
||||
#:select-next #:select-prev
|
||||
#:select-visible-options
|
||||
#:select-handle-key
|
||||
#:render
|
||||
#:fuzzy-match-p))
|
||||
#+END_SRC
|
||||
|
||||
** Select class
|
||||
|
||||
~select~ inherits from ~dirty-mixin~. Options are stored as a list of
|
||||
plists. ~selected-index~ tracks the currently highlighted option.
|
||||
~filter~ is a string (or nil for unfiltered). ~on-select~ is a callback
|
||||
receiving the selected option plist.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tty.select)
|
||||
|
||||
(defclass select (dirty-mixin)
|
||||
((options :initform nil :initarg :options
|
||||
:accessor select-options :type list)
|
||||
(filter :initform nil :initarg :filter
|
||||
:accessor select-filter :type (or string null))
|
||||
(selected-index :initform 0 :initarg :selected-index
|
||||
:accessor select-selected-index :type fixnum)
|
||||
(on-select :initform nil :initarg :on-select
|
||||
:accessor select-on-select)
|
||||
(layout-node :initform (make-layout-node) :initarg :layout-node
|
||||
:accessor select-layout-node)))
|
||||
|
||||
(defun make-select (&key options filter on-select)
|
||||
(make-instance 'select
|
||||
:options (or options nil)
|
||||
:filter filter
|
||||
:on-select on-select))
|
||||
#+END_SRC
|
||||
|
||||
** Component protocol
|
||||
|
||||
~component-layout-node~ returns the layout node so the layout engine
|
||||
can position the select widget.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defmethod component-layout-node ((sel select))
|
||||
(select-layout-node sel))
|
||||
#+END_SRC
|
||||
|
||||
** Option filtering: substring match
|
||||
|
||||
~select-filtered-options~ returns options whose ~:title~ contains the
|
||||
filter string (case-insensitive). When ~filter~ is nil, returns all
|
||||
options. Category headers are NOT filtered out — they remain in the
|
||||
list so the user can see category context.
|
||||
|
||||
The function returns an alist of ~(filtered-index original-index option)~
|
||||
to preserve the original index for selection tracking.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defun select-filtered-options (sel)
|
||||
"Return list of options matching the current filter, in display order.
|
||||
Each item: (display-index original-index option-plist)."
|
||||
(let* ((filter (select-filter sel))
|
||||
(all-options (select-options sel))
|
||||
(filtered (if (null filter)
|
||||
all-options
|
||||
(let ((lower (string-downcase filter)))
|
||||
(remove-if-not
|
||||
(lambda (opt)
|
||||
(when (getf opt :category)
|
||||
(return-from select-filtered-options all-options))
|
||||
(let ((title (string-downcase (getf opt :title))))
|
||||
(or (search lower title)
|
||||
(fuzzy-match-p lower title))))
|
||||
all-options)))))
|
||||
(loop for opt in filtered
|
||||
for i from 0
|
||||
collect (list i (position opt all-options) opt))))
|
||||
#+END_SRC
|
||||
|
||||
** Fuzzy matching: trigram Jaccard similarity
|
||||
|
||||
~trigram-score~ converts a string into a set of 3-character sliding
|
||||
window n-grams. ~fuzzy-match-p~ returns T if the Jaccard similarity
|
||||
between the query trigrams and the target trigrams exceeds 0.3.
|
||||
|
||||
Trigrams capture character-level similarity without requiring exact
|
||||
substring matches. "nrd" matches "Nord" because both contain ~nor~,
|
||||
~ord~ and ~nrd~ contributes ~nrd~ — the overlap is enough to exceed
|
||||
the threshold.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defun string-trigrams (str)
|
||||
"Return a list of 3-character trigrams from STR."
|
||||
(let ((s (string-downcase str))
|
||||
(result nil))
|
||||
(when (< (length s) 3)
|
||||
(return-from string-trigrams (list s)))
|
||||
(loop for i from 0 to (- (length s) 3)
|
||||
do (push (subseq s i (+ i 3)) result))
|
||||
(delete-duplicates result :test #'string=)))
|
||||
|
||||
(defun trigram-score (query target)
|
||||
"Jaccard similarity of trigram sets: |intersection| / |union|."
|
||||
(let* ((q-trigrams (string-trigrams query))
|
||||
(t-trigrams (string-trigrams target))
|
||||
(intersection (length (intersection q-trigrams t-trigrams :test #'string=)))
|
||||
(union (length (union q-trigrams t-trigrams :test #'string=))))
|
||||
(if (zerop union) 0.0 (/ (float intersection) union))))
|
||||
|
||||
(defun fuzzy-match-p (query target)
|
||||
"T if character-set Jaccard similarity exceeds threshold (0.3)."
|
||||
(let* ((q-chars (remove-duplicates (coerce (string-downcase query) 'list)))
|
||||
(t-chars (remove-duplicates (coerce (string-downcase target) 'list)))
|
||||
(intersection (length (intersection q-chars t-chars)))
|
||||
(union (length (union q-chars t-chars))))
|
||||
(if (zerop union) nil (> (/ (float intersection) union) 0.3))))
|
||||
#+END_SRC
|
||||
|
||||
** Navigation
|
||||
|
||||
~select-next~ and ~select-prev~ move the selection forward/backward
|
||||
through the filtered options list. They skip category headers (options
|
||||
with ~:category t~). The selection wraps at list boundaries.
|
||||
~select-clamp-index~ ensures the index is valid after filtering changes.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defun select-clamp-index (sel)
|
||||
"Ensure selected-index is valid. Wraps if empty."
|
||||
(let* ((filtered (select-filtered-options sel))
|
||||
(count (length filtered)))
|
||||
(if (zerop count)
|
||||
(setf (select-selected-index sel) 0)
|
||||
(setf (select-selected-index sel)
|
||||
(max 0 (min (select-selected-index sel) (1- count)))))))
|
||||
|
||||
(defun select-next (sel)
|
||||
"Move selection to next non-category option. Wraps at end."
|
||||
(let* ((filtered (select-filtered-options sel))
|
||||
(count (length filtered))
|
||||
(current (select-selected-index sel)))
|
||||
(when (plusp count)
|
||||
(loop for i from 1 below count
|
||||
for idx = (mod (+ current i) count)
|
||||
for opt = (third (nth idx filtered))
|
||||
when (not (getf opt :category))
|
||||
do (setf (select-selected-index sel) idx)
|
||||
(mark-dirty sel)
|
||||
(return)))))
|
||||
|
||||
(defun select-prev (sel)
|
||||
"Move selection to previous non-category option. Wraps at start."
|
||||
(let* ((filtered (select-filtered-options sel))
|
||||
(count (length filtered))
|
||||
(current (select-selected-index sel)))
|
||||
(when (plusp count)
|
||||
(loop for i from 1 below count
|
||||
for idx = (mod (- current i) count)
|
||||
for opt = (third (nth idx filtered))
|
||||
when (not (getf opt :category))
|
||||
do (setf (select-selected-index sel) idx)
|
||||
(mark-dirty sel)
|
||||
(return)))))
|
||||
#+END_SRC
|
||||
|
||||
** Key event handler
|
||||
|
||||
~select-handle-key~ dispatches keyboard events:
|
||||
- Down, Ctrl+N → select-next
|
||||
- Up, Ctrl+P → select-prev
|
||||
- Enter → on-select callback with the selected option
|
||||
- Esc → return NIL (caller can dismiss)
|
||||
|
||||
Returns T if the key was handled, NIL otherwise.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defun select-handle-key (sel event)
|
||||
"Handle a key-event. Returns T if handled."
|
||||
(let ((key (key-event-key event))
|
||||
(ctrl (key-event-ctrl event)))
|
||||
(cond
|
||||
((or (eql key :down) (and ctrl (eql key :n)))
|
||||
(select-next sel) t)
|
||||
((or (eql key :up) (and ctrl (eql key :p)))
|
||||
(select-prev sel) t)
|
||||
((eql key :enter)
|
||||
(let* ((filtered (select-filtered-options sel))
|
||||
(idx (select-selected-index sel))
|
||||
(item (when (< idx (length filtered))
|
||||
(third (nth idx filtered)))))
|
||||
(when item
|
||||
(let ((cb (select-on-select sel)))
|
||||
(when cb (funcall cb item))))
|
||||
t))
|
||||
((eql key :escape) nil)
|
||||
(t nil))))
|
||||
#+END_SRC
|
||||
|
||||
** Visible options (viewport culling)
|
||||
|
||||
~select-visible-options~ returns only the filtered options that fit
|
||||
within the widget's available height. Each option occupies 1 row.
|
||||
This prevents rendering hundreds of items when the viewport shows 10.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defun select-visible-options (sel)
|
||||
"Return filtered options that fit within the viewport."
|
||||
(let* ((ln (select-layout-node sel))
|
||||
(height (if ln (layout-node-height ln) 80))
|
||||
(filtered (select-filtered-options sel))
|
||||
(sel-idx (select-selected-index sel))
|
||||
;; Show items around the selection
|
||||
(half (floor (1- height) 2))
|
||||
(start (max 0 (- sel-idx half)))
|
||||
(end (min (length filtered) (+ start height))))
|
||||
(subseq filtered start end)))
|
||||
#+END_SRC
|
||||
|
||||
** Rendering
|
||||
|
||||
~render~ draws each visible option on its own line. The selected
|
||||
option is highlighted with ~:accent~ foreground and ~:background-element~
|
||||
background. Category headers are rendered dimmed (~:text-muted~) and
|
||||
not selectable (visually distinct).
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defmethod render ((sel select) backend)
|
||||
(let* ((ln (select-layout-node sel))
|
||||
(x 0) (y 0)
|
||||
(w (if ln (layout-node-width ln) 80))
|
||||
(visible (select-visible-options sel))
|
||||
(sel-idx (select-selected-index sel)))
|
||||
(dolist (item visible)
|
||||
(let* ((display-idx (first item))
|
||||
(option (third item))
|
||||
(title (getf option :title))
|
||||
(is-category (getf option :category))
|
||||
(is-selected (eql display-idx sel-idx))
|
||||
(display (if (> (length title) (1- w))
|
||||
(concatenate 'string (subseq title 0 (1- w)) "…")
|
||||
title)))
|
||||
(cond
|
||||
(is-category
|
||||
(draw-text backend x y display :text-muted nil))
|
||||
(is-selected
|
||||
(draw-rect backend x y w 1 :bg :accent)
|
||||
(draw-text backend x y display :background :accent))
|
||||
(t
|
||||
(draw-text backend x y display nil nil)))
|
||||
(incf y 1)))
|
||||
(values)))
|
||||
#+END_SRC
|
||||
|
||||
** Combined tangle block
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/select.lisp
|
||||
(in-package #:cl-tty.select)
|
||||
|
||||
(defclass select (dirty-mixin)
|
||||
((options :initform nil :initarg :options :accessor select-options :type list)
|
||||
(filter :initform nil :initarg :filter :accessor select-filter :type (or string null))
|
||||
(selected-index :initform 0 :initarg :selected-index :accessor select-selected-index :type fixnum)
|
||||
(on-select :initform nil :initarg :on-select :accessor select-on-select)
|
||||
(layout-node :initform (make-layout-node) :initarg :layout-node :accessor select-layout-node)))
|
||||
|
||||
(defun make-select (&key options filter on-select)
|
||||
(make-instance 'select :options (or options nil) :filter filter :on-select on-select))
|
||||
|
||||
(defmethod component-layout-node ((sel select)) (select-layout-node sel))
|
||||
|
||||
(defun select-filtered-options (sel)
|
||||
(let* ((filter (select-filter sel)) (all-options (select-options sel))
|
||||
(filtered (if (null filter) all-options
|
||||
(let ((lower (string-downcase filter)))
|
||||
(remove-if-not
|
||||
(lambda (opt)
|
||||
(or (getf opt :category)
|
||||
(let ((title (string-downcase (getf opt :title))))
|
||||
(or (search lower title) (fuzzy-match-p lower title)))))
|
||||
all-options)))))
|
||||
(loop for opt in filtered for i from 0
|
||||
collect (list i (position opt all-options) opt))))
|
||||
|
||||
(defun fuzzy-match-p (query target)
|
||||
(let* ((q (remove-duplicates (coerce (string-downcase query) 'list)))
|
||||
(tg (remove-duplicates (coerce (string-downcase target) 'list)))
|
||||
(intersection (length (intersection q tg)))
|
||||
(union (length (union q tg))))
|
||||
(if (zerop union) nil (> (/ (float intersection) union) 0.3))))
|
||||
|
||||
(defun select-clamp-index (sel)
|
||||
(let* ((filtered (select-filtered-options sel)) (count (length filtered)))
|
||||
(if (zerop count) (setf (select-selected-index sel) 0)
|
||||
(setf (select-selected-index sel) (max 0 (min (select-selected-index sel) (1- count)))))))
|
||||
|
||||
(defun select-next (sel)
|
||||
(let* ((filtered (select-filtered-options sel)) (count (length filtered))
|
||||
(current (select-selected-index sel)))
|
||||
(when (plusp count)
|
||||
(loop for i from 1 below count
|
||||
for idx = (mod (+ current i) count)
|
||||
for opt = (third (nth idx filtered))
|
||||
when (not (getf opt :category))
|
||||
do (setf (select-selected-index sel) idx) (mark-dirty sel) (return)))))
|
||||
|
||||
(defun select-prev (sel)
|
||||
(let* ((filtered (select-filtered-options sel)) (count (length filtered))
|
||||
(current (select-selected-index sel)))
|
||||
(when (plusp count)
|
||||
(loop for i from 1 below count
|
||||
for idx = (mod (- current i) count)
|
||||
for opt = (third (nth idx filtered))
|
||||
when (not (getf opt :category))
|
||||
do (setf (select-selected-index sel) idx) (mark-dirty sel) (return)))))
|
||||
|
||||
(defun select-handle-key (sel event)
|
||||
(let ((key (key-event-key event)) (ctrl (key-event-ctrl event)))
|
||||
(cond
|
||||
((or (eql key :down) (and ctrl (eql key :n))) (select-next sel) t)
|
||||
((or (eql key :up) (and ctrl (eql key :p))) (select-prev sel) t)
|
||||
((eql key :enter)
|
||||
(let* ((filtered (select-filtered-options sel)) (idx (select-selected-index sel))
|
||||
(item (when (< idx (length filtered)) (third (nth idx filtered)))))
|
||||
(when item (let ((cb (select-on-select sel))) (when cb (funcall cb item)))) t))
|
||||
((eql key :escape) nil) (t nil))))
|
||||
|
||||
(defun select-visible-options (sel)
|
||||
(let* ((ln (select-layout-node sel)) (height (if ln (layout-node-height ln) 80))
|
||||
(filtered (select-filtered-options sel)) (sel-idx (select-selected-index sel))
|
||||
(half (floor (1- height) 2)) (start (max 0 (- sel-idx half)))
|
||||
(end (min (length filtered) (+ start height))))
|
||||
(subseq filtered start end)))
|
||||
|
||||
(defmethod render ((sel select) backend)
|
||||
(let* ((ln (select-layout-node sel)) (x 0) (y 0)
|
||||
(w (if ln (layout-node-width ln) 80))
|
||||
(visible (select-visible-options sel)) (sel-idx (select-selected-index sel)))
|
||||
(dolist (item visible)
|
||||
(let* ((display-idx (first item)) (option (third item))
|
||||
(title (getf option :title)) (cat (getf option :category))
|
||||
(selected (eql display-idx sel-idx))
|
||||
(display (if (> (length title) (1- w))
|
||||
(concatenate 'string (subseq title 0 (1- w)) "…") title)))
|
||||
(cond (cat (draw-text backend x y display :text-muted nil))
|
||||
(selected
|
||||
(draw-rect backend x y w 1 :bg :accent)
|
||||
(draw-text backend x y display :background :accent))
|
||||
(t (draw-text backend x y display nil nil)))
|
||||
(incf y 1)))
|
||||
(values)))
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/select-package.lisp
|
||||
(defpackage :cl-tty.select
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export
|
||||
#:select #:make-select
|
||||
#:select-options #:select-filter
|
||||
#:select-selected-index #:select-on-select
|
||||
#:select-layout-node
|
||||
#:select-filtered-options
|
||||
#:select-next #:select-prev
|
||||
#:select-visible-options
|
||||
#:select-handle-key
|
||||
#:render
|
||||
#:fuzzy-match-p))
|
||||
#+END_SRC
|
||||
@@ -1,4 +1,4 @@
|
||||
#+TITLE: cl-tui v0.5.0 — Text Input + Keybinding System
|
||||
#+TITLE: cl-tty v0.5.0 — Text Input + Keybinding System
|
||||
#+STARTUP: content
|
||||
|
||||
* Text Input System
|
||||
@@ -140,7 +140,7 @@ SBCL's ~sb-posix~ provides the POSIX terminal APIs (~tcgetattr~,
|
||||
** Tests
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui-input-test)
|
||||
(in-package #:cl-tty-input-test)
|
||||
|
||||
(def-suite input-suite :description "Text input and keybinding tests")
|
||||
(in-suite input-suite)
|
||||
@@ -407,16 +407,16 @@ SBCL's ~sb-posix~ provides the POSIX terminal APIs (~tcgetattr~,
|
||||
|
||||
** Package
|
||||
|
||||
The package uses ~:cl-tui.backend~ for backend protocol (draw-text, etc.),
|
||||
~:cl-tui.box~ for dirty-mixin and rendering pipeline,
|
||||
and ~:cl-tui.layout~ for layout-node.
|
||||
The package uses ~:cl-tty.backend~ for backend protocol (draw-text, etc.),
|
||||
~:cl-tty.box~ for dirty-mixin and rendering pipeline,
|
||||
and ~:cl-tty.layout~ for layout-node.
|
||||
|
||||
I export everything users of the input system need: key events, mouse events,
|
||||
terminal raw mode, TextInput, Textarea, and the keybinding system.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defpackage :cl-tui.input
|
||||
(:use :cl :cl-tui.backend :cl-tui.box :cl-tui.layout)
|
||||
(defpackage :cl-tty.input
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout)
|
||||
(:export
|
||||
;; Key events
|
||||
#:key-event #:make-key-event
|
||||
@@ -463,7 +463,7 @@ this returns ~("")~ (one empty string), which is the correct behavior for
|
||||
textarea line splitting — a blank document has one empty line.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
(defun %split-string (string separator)
|
||||
"Split STRING at each occurrence of SEPARATOR. Returns list of strings."
|
||||
@@ -881,7 +881,7 @@ providing real terminal input via our parser. The ~probe-file~ guard
|
||||
handles the case where stdin is not a terminal (piped input).
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(defmethod read-event ((b cl-tui.backend:backend) &key timeout)
|
||||
(defmethod read-event ((b cl-tty.backend:backend) &key timeout)
|
||||
(declare (ignore b))
|
||||
(when (probe-file "/dev/stdin")
|
||||
(%read-event :timeout timeout)))
|
||||
@@ -900,7 +900,7 @@ The ~value~ and ~cursor~ slots are directly accessible for testing
|
||||
without going through the event handler.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
(defclass text-input (dirty-mixin)
|
||||
((value :initform "" :initarg :value :accessor text-input-value :type string)
|
||||
@@ -1109,7 +1109,7 @@ selection (not yet implemented in the handler). ~on-submit~ fires
|
||||
on Ctrl+Enter when set.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
(defclass textarea (dirty-mixin)
|
||||
((value :initform "" :initarg :value :accessor textarea-value :type string)
|
||||
@@ -1445,7 +1445,7 @@ A keymap has a ~name~ for debugging, ~bindings~ as an alist (ordered
|
||||
for priority), and an optional ~parent~ for inheritance chains.
|
||||
|
||||
#+BEGIN_SRC lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
(defstruct keymap
|
||||
(name nil :type (or keyword null))
|
||||
@@ -1568,7 +1568,7 @@ experience; this section is what actually generates the compilable code.
|
||||
|
||||
** input.lisp
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/input.lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Utility: split-string (avoids external dependency)
|
||||
@@ -1871,7 +1871,7 @@ experience; this section is what actually generates the compilable code.
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Backend integration
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defmethod read-event ((b cl-tui.backend:backend) &key timeout)
|
||||
(defmethod read-event ((b cl-tty.backend:backend) &key timeout)
|
||||
(declare (ignore b))
|
||||
(when (probe-file "/dev/stdin")
|
||||
(%read-event :timeout timeout)))
|
||||
@@ -1880,7 +1880,7 @@ experience; this section is what actually generates the compilable code.
|
||||
|
||||
** text-input.lisp
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/text-input.lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; TextInput class
|
||||
@@ -2048,7 +2048,7 @@ experience; this section is what actually generates the compilable code.
|
||||
|
||||
** textarea.lisp
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/textarea.lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Utility: split string (local copy for dependency-free operation)
|
||||
@@ -2311,7 +2311,7 @@ experience; this section is what actually generates the compilable code.
|
||||
|
||||
** keybindings.lisp
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/keybindings.lisp
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Key map struct
|
||||
@@ -2393,8 +2393,8 @@ experience; this section is what actually generates the compilable code.
|
||||
|
||||
** input-package.lisp
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/input-package.lisp
|
||||
(defpackage :cl-tui.input
|
||||
(:use :cl :cl-tui.backend :cl-tui.box :cl-tui.layout)
|
||||
(defpackage :cl-tty.input
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout)
|
||||
(:export
|
||||
;; Key events
|
||||
#:key-event #:make-key-event
|
||||
@@ -2432,10 +2432,10 @@ experience; this section is what actually generates the compilable code.
|
||||
|
||||
** input-tests.lisp
|
||||
#+BEGIN_SRC lisp :tangle ../tests/input-tests.lisp
|
||||
(defpackage :cl-tui-input-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input)
|
||||
(defpackage :cl-tty-input-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-input-test)
|
||||
(in-package :cl-tty-input-test)
|
||||
|
||||
(def-suite input-suite :description "Text input and keybinding tests")
|
||||
(in-suite input-suite)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-box-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.layout :cl-tui.box)
|
||||
(defpackage :cl-tty-box-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.layout :cl-tty.box)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-box-test)
|
||||
(in-package :cl-tty-box-test)
|
||||
|
||||
(def-suite box-suite :description "Box renderable tests")
|
||||
(in-suite box-suite)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
(defclass box (dirty-mixin)
|
||||
((layout-node :initform (make-layout-node) :accessor box-layout-node
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(defpackage :cl-tui.container
|
||||
(:use :cl :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input)
|
||||
(defpackage :cl-tty.container
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export
|
||||
#:scroll-box #:make-scroll-box
|
||||
#:scroll-box-scroll-y #:scroll-box-scroll-x
|
||||
|
||||
32
src/components/dialog-package.lisp
Normal file
32
src/components/dialog-package.lisp
Normal file
@@ -0,0 +1,32 @@
|
||||
;;; dialog-package.lisp — Package definition for cl-tty.dialog
|
||||
|
||||
(defpackage :cl-tty.dialog
|
||||
(:use :cl :cl-tty.input :cl-tty.select)
|
||||
(:export
|
||||
#:dialog
|
||||
#:dialog-title
|
||||
#:dialog-content
|
||||
#:dialog-on-dismiss
|
||||
#:dialog-size
|
||||
#:dialog-size-pixels
|
||||
#:render-dialog
|
||||
#:push-dialog
|
||||
#:pop-dialog
|
||||
#:*dialog-stack*
|
||||
#:alert-dialog
|
||||
#:confirm-dialog
|
||||
#:select-dialog
|
||||
#:prompt-dialog
|
||||
#:toast
|
||||
#:toast-message
|
||||
#:toast-variant
|
||||
#:render-toast
|
||||
#:dismiss-toast
|
||||
#:*toasts*
|
||||
;; Tests
|
||||
#:dialog-create
|
||||
#:dialog-size-small
|
||||
#:dialog-size-medium
|
||||
#:dialog-push-pop
|
||||
#:toast-create
|
||||
#:toast-dismiss))
|
||||
123
src/components/dialog.lisp
Normal file
123
src/components/dialog.lisp
Normal file
@@ -0,0 +1,123 @@
|
||||
;;; dialog.lisp — Dialog System + Toast for cl-tty
|
||||
|
||||
(in-package :cl-tty.dialog)
|
||||
|
||||
;; ─── Special variables ────────────────────────────────────────────────────────
|
||||
|
||||
(defvar *dialog-stack* nil
|
||||
"Stack of active dialogs. (list) of dialog instances.")
|
||||
|
||||
(defvar *toasts* nil
|
||||
"List of active toast notifications.")
|
||||
|
||||
;; ─── Dialog class ─────────────────────────────────────────────────────────────
|
||||
|
||||
(defclass dialog ()
|
||||
((title :initarg :title :accessor dialog-title)
|
||||
(size :initarg :size :initform :medium :accessor dialog-size)
|
||||
(content :initarg :content :initform nil :accessor dialog-content)
|
||||
(on-dismiss :initarg :on-dismiss :initform nil :accessor dialog-on-dismiss)))
|
||||
|
||||
(defun dialog-size-pixels (size)
|
||||
(case size
|
||||
(:small (values 40 8))
|
||||
(:medium (values 60 16))
|
||||
(:large (values 88 24))
|
||||
(t (values 60 16))))
|
||||
|
||||
(defun render-dialog (dialog screen w h)
|
||||
(multiple-value-bind (dw dh) (dialog-size-pixels (dialog-size dialog))
|
||||
(let ((x (floor (- w dw) 2))
|
||||
(y (floor (- h dh) 2)))
|
||||
(dotimes (row h)
|
||||
(dotimes (col w)
|
||||
(backend-write screen col row " " :bg :dim)))
|
||||
(draw-border screen x y dw dh :single :title (dialog-title dialog))
|
||||
(when (dialog-content dialog)
|
||||
(render-component (dialog-content dialog) screen (1+ x) (1+ y) (- dw 2) (- dh 2))))))
|
||||
|
||||
(defun push-dialog (dialog)
|
||||
(push dialog *dialog-stack*)
|
||||
dialog)
|
||||
|
||||
(defun pop-dialog ()
|
||||
(when *dialog-stack*
|
||||
(let ((dialog (pop *dialog-stack*)))
|
||||
(when (dialog-on-dismiss dialog)
|
||||
(funcall (dialog-on-dismiss dialog)))
|
||||
dialog)))
|
||||
|
||||
;; ─── Dialog sub-classes ──────────────────────────────────────────────────────
|
||||
|
||||
(defun alert-dialog (title message)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'select
|
||||
:options (list (list :title "OK" :value :ok))
|
||||
:on-select (lambda (opt) (declare (ignore opt)) (pop-dialog)))
|
||||
:on-dismiss (lambda () (pop-dialog))))
|
||||
|
||||
(defun confirm-dialog (title message &key on-yes on-no)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'select
|
||||
:options (list (list :title "Yes" :value :yes)
|
||||
(list :title "No" :value :no))
|
||||
:on-select (lambda (opt)
|
||||
(pop-dialog)
|
||||
(if (eql opt :yes)
|
||||
(when on-yes (funcall on-yes))
|
||||
(when on-no (funcall on-no)))))))
|
||||
|
||||
(defun select-dialog (title options &key on-select)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :medium
|
||||
:content (make-instance 'select
|
||||
:options options
|
||||
:on-select (lambda (opt)
|
||||
(pop-dialog)
|
||||
(when on-select (funcall on-select opt))))))
|
||||
|
||||
(defun prompt-dialog (title &key on-submit)
|
||||
(make-instance 'dialog
|
||||
:title title
|
||||
:size :small
|
||||
:content (make-instance 'text-input
|
||||
:on-submit (lambda (value)
|
||||
(pop-dialog)
|
||||
(when on-submit (funcall on-submit value))))))
|
||||
|
||||
;; ─── Toast system ─────────────────────────────────────────────────────────────
|
||||
|
||||
(defclass toast ()
|
||||
((message :initarg :message :accessor toast-message)
|
||||
(variant :initarg :variant :initform :info :accessor toast-variant)))
|
||||
|
||||
(defun render-toast (toast screen w)
|
||||
(let* ((msg (toast-message toast))
|
||||
(variant (toast-variant toast))
|
||||
(color (case variant
|
||||
(:info :blue) (:success :green)
|
||||
(:warning :yellow) (:error :red)))
|
||||
(max-w (min 60 (1- w)))
|
||||
(x (- w max-w 1))
|
||||
(text (if (> (length msg) (- max-w 2))
|
||||
(concatenate 'string (subseq msg 0 (- max-w 5)) "...")
|
||||
msg)))
|
||||
(draw-rect screen x 0 max-w 1 :bg color)
|
||||
(backend-write screen (1+ x) 0 text :fg :white :bold t)))
|
||||
|
||||
(defun toast (message &key (variant :info) (duration 5000))
|
||||
(let ((toast (make-instance 'toast :message message :variant variant)))
|
||||
(push toast *toasts*)
|
||||
(when (plusp duration)
|
||||
(schedule-event (+ (get-internal-real-time)
|
||||
(* duration 1000))
|
||||
(lambda () (dismiss-toast toast))))
|
||||
toast))
|
||||
|
||||
(defun dismiss-toast (toast)
|
||||
(setf *toasts* (remove toast *toasts*)))
|
||||
@@ -1,5 +1,5 @@
|
||||
;; Dirty tracking tests are in box-tests.lisp (same test suite)
|
||||
(in-package :cl-tui-box-test)
|
||||
(in-package :cl-tty-box-test)
|
||||
(in-suite box-suite)
|
||||
|
||||
(test dirty-mixin-default-is-dirty
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
;; ── Dirty Tracking ─────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
(defpackage :cl-tui.input
|
||||
(:use :cl :cl-tui.backend :cl-tui.box :cl-tui.layout)
|
||||
(defpackage :cl-tty.input
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout)
|
||||
(:export
|
||||
;; Key events
|
||||
#:key-event #:make-key-event
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-input-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input)
|
||||
(defpackage :cl-tty-input-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-input-test)
|
||||
(in-package :cl-tty-input-test)
|
||||
|
||||
(def-suite input-suite :description "Text input and keybinding tests")
|
||||
(in-suite input-suite)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Utility: split-string (avoids external dependency)
|
||||
@@ -301,7 +301,7 @@
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Backend integration
|
||||
;;; ---------------------------------------------------------------------------
|
||||
(defmethod read-event ((b cl-tui.backend:backend) &key timeout)
|
||||
(defmethod read-event ((b cl-tty.backend:backend) &key timeout)
|
||||
(declare (ignore b))
|
||||
(when (probe-file "/dev/stdin")
|
||||
(%read-event :timeout timeout)))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Key map struct
|
||||
|
||||
65
src/components/markdown-package.lisp
Normal file
65
src/components/markdown-package.lisp
Normal file
@@ -0,0 +1,65 @@
|
||||
;;; markdown-package.lisp — Package definition for cl-tty.markdown
|
||||
|
||||
(defpackage :cl-tty.markdown
|
||||
(:use :cl :fiveam)
|
||||
(:export
|
||||
;; Data structures
|
||||
#:make-md-node
|
||||
#:md-node-p
|
||||
#:md-node-text
|
||||
;; Parsing
|
||||
#:parse-blocks
|
||||
#:parse-inline
|
||||
;; Highlighting
|
||||
#:*syntax-highlighters*
|
||||
#:highlight-code
|
||||
;; Diff
|
||||
#:classify-diff-line
|
||||
;; Rendering
|
||||
#:render-md
|
||||
#:render-md-node
|
||||
#:render-markdown
|
||||
#:render-inline
|
||||
;; Styles
|
||||
#:apply-style
|
||||
#:apply-styles
|
||||
;; Tests (exported test symbols for ASDF integration)
|
||||
#:heading-parsing
|
||||
#:heading-levels
|
||||
#:heading-with-inline-formatting
|
||||
#:paragraph-parsing
|
||||
#:paragraph-multi-line
|
||||
#:bold-parsing
|
||||
#:italic-parsing
|
||||
#:bold-italic-combined
|
||||
#:inline-code-parsing
|
||||
#:link-parsing
|
||||
#:code-block-parsing
|
||||
#:code-block-unknown-language
|
||||
#:blockquote-parsing
|
||||
#:list-item-parsing
|
||||
#:ordered-list-parsing
|
||||
#:thematic-break-parsing
|
||||
#:highlight-lisp-keyword
|
||||
#:highlight-lisp-builtin
|
||||
#:highlight-unknown-language
|
||||
#:highlight-comment
|
||||
#:classify-diff-added
|
||||
#:classify-diff-removed
|
||||
#:classify-diff-hunk
|
||||
#:classify-diff-context
|
||||
#:render-heading-output
|
||||
#:render-paragraph-output
|
||||
#:render-thematic-break-output
|
||||
#:render-code-block-output
|
||||
#:render-diff-block-output
|
||||
#:markdown-integration
|
||||
#:render-markdown-string
|
||||
;; Internal (for testability)
|
||||
#:classify-line
|
||||
#:split-string-into-lines
|
||||
#:find-closing-marker
|
||||
#:string-prefix-p
|
||||
#:tokenize-line
|
||||
#:apply-highlight-token
|
||||
#:apply-highlight-style))
|
||||
681
src/components/markdown.lisp
Normal file
681
src/components/markdown.lisp
Normal file
@@ -0,0 +1,681 @@
|
||||
;;; markdown.lisp — Markdown + Code + Diff rendering for cl-tty
|
||||
|
||||
(in-package :cl-tty.markdown)
|
||||
|
||||
;; ─── Node constructors ────────────────────────────────────────────────────────
|
||||
|
||||
(defun make-md-node (type &key children properties content url)
|
||||
(let ((node (list :type type)))
|
||||
(when children (setf (getf node :children) children))
|
||||
(when properties (setf (getf node :properties) properties))
|
||||
(when content (setf (getf node :content) content))
|
||||
(when url (setf (getf node :url) url))
|
||||
node))
|
||||
|
||||
(defun md-node-p (thing)
|
||||
(and (listp thing) (getf thing :type)))
|
||||
|
||||
(defun md-node-text (node)
|
||||
(let ((type (getf node :type)))
|
||||
(cond ((eql type :text) (or (getf node :content) ""))
|
||||
((eql type :link)
|
||||
(concatenate 'string
|
||||
(md-node-text (first (getf node :children)))
|
||||
(format nil " (~a)" (or (getf node :url) ""))))
|
||||
((eql type :inline-code) (or (getf node :content) ""))
|
||||
((getf node :children)
|
||||
(apply #'concatenate 'string
|
||||
(mapcar #'md-node-text (getf node :children))))
|
||||
(t ""))))
|
||||
|
||||
;; ─── Block-level parser ───────────────────────────────────────────────────────
|
||||
|
||||
(defun split-string-into-lines (string)
|
||||
(let ((result nil) (start 0))
|
||||
(flet ((add-line (end) (push (subseq string start end) result)))
|
||||
(loop for i from 0 below (length string)
|
||||
do (let ((c (char string i)))
|
||||
(cond ((char= c #\Newline) (add-line i) (setf start (1+ i)))
|
||||
((and (char= c #\Return) (< (1+ i) (length string))
|
||||
(char= (char string (1+ i)) #\Newline))
|
||||
(add-line i) (setf start (+ i 2)) (incf i)))))
|
||||
(when (< start (length string)) (add-line (length string)))
|
||||
(coerce (nreverse result) 'vector))))
|
||||
|
||||
(defun classify-line (line)
|
||||
(cond
|
||||
((string= line "") (cons :blank nil))
|
||||
((and (>= (length line) 3)
|
||||
(let ((c0 (char line 0)))
|
||||
(and (find c0 "-*")
|
||||
(every (lambda (c) (or (char= c c0) (char= c #\Space) (char= c #\Tab)))
|
||||
line))))
|
||||
(cons :thematic-break nil))
|
||||
((and (char= (char line 0) #\#)
|
||||
(let ((count 0))
|
||||
(loop for c across line while (char= c #\#) do (incf count))
|
||||
(and (<= 1 count 6)
|
||||
(or (>= (length line) (1+ count))
|
||||
(member (char line count) '(#\Space #\Tab))))))
|
||||
(let* ((hash-count (loop for c across line while (char= c #\#) count c))
|
||||
(content (string-trim (list #\Space #\Tab) (subseq line hash-count))))
|
||||
(cons :heading (cons hash-count content))))
|
||||
((char= (char line 0) #\>)
|
||||
(cons :blockquote (string-trim (list #\Space #\Tab) (subseq line 1))))
|
||||
((and (>= (length line) 2) (find (char line 0) "-*+")
|
||||
(char= (char line 1) #\Space))
|
||||
(cons :list-item (string-trim (list #\Space #\Tab) (subseq line 2))))
|
||||
((and (>= (length line) 3) (digit-char-p (char line 0))
|
||||
(loop for c across line while (digit-char-p c)
|
||||
finally (return (find c ". )"))))
|
||||
(let ((dot-pos (position-if (lambda (c) (find c ". )")) line)))
|
||||
(if (and dot-pos (find (char line dot-pos) ". )"))
|
||||
(cons :ordered-item (string-trim (list #\Space #\Tab)
|
||||
(subseq line (1+ dot-pos))))
|
||||
(cons :paragraph line))))
|
||||
((and (>= (length line) 4) (find (char line 0) "-+")
|
||||
(char= (char line 1) (char line 0))
|
||||
(char= (char line 2) (char line 0))
|
||||
(char= (char line 3) #\Space))
|
||||
(cons :diff-header line))
|
||||
((and (>= (length line) 1) (find (char line 0) "-+")
|
||||
(not (and (>= (length line) 3)
|
||||
(char= (char line 1) (char line 0))
|
||||
(char= (char line 2) (char line 0)))))
|
||||
(cons :diff-line (cons (char line 0) (subseq line 1))))
|
||||
((and (>= (length line) 3) (find (char line 0) "`~")
|
||||
(let ((fence-len (loop for c across line
|
||||
while (char= c (char line 0)) count c)))
|
||||
(and (>= fence-len 3)
|
||||
(let ((rest (string-trim (list #\Space #\Tab)
|
||||
(subseq line fence-len))))
|
||||
(cons :code-start rest))))))
|
||||
(t (cons :paragraph line))))
|
||||
|
||||
(defun find-closing-marker (text start marker)
|
||||
(let ((marker-len (length marker)) (len (length text)))
|
||||
(loop for j from start to (- len marker-len)
|
||||
do (when (and (char= (char text j) (char marker 0))
|
||||
(string= marker (subseq text j (+ j marker-len)))
|
||||
(or (= j 0) (not (char= (char text (1- j)) #\\))))
|
||||
(return j))
|
||||
finally (return nil))))
|
||||
|
||||
(defun parse-paragraph (lines start)
|
||||
(let ((text-parts nil) (i start))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
((:paragraph) (push (cdr class) text-parts) (incf i))
|
||||
(:blank (incf i) (loop-finish))
|
||||
(t (loop-finish)))))
|
||||
(values (make-md-node :paragraph :children
|
||||
(parse-inline
|
||||
(with-output-to-string (s)
|
||||
(loop for part in (nreverse text-parts)
|
||||
for first = t then nil
|
||||
do (unless first (write-char #\Space s))
|
||||
(princ part s)))))
|
||||
i)))
|
||||
|
||||
(defun parse-blockquote (lines start)
|
||||
(let ((text-parts nil) (i start))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
(:blockquote (push (cdr class) text-parts) (incf i))
|
||||
(:blank (incf i) (loop-finish))
|
||||
(t (loop-finish)))))
|
||||
(values (make-md-node :blockquote :children
|
||||
(parse-inline
|
||||
(with-output-to-string (s)
|
||||
(loop for part in (nreverse text-parts)
|
||||
for first = t then nil
|
||||
do (unless first (write-char #\Space s))
|
||||
(princ part s)))))
|
||||
i)))
|
||||
|
||||
(defun parse-list (lines start)
|
||||
(declare (ignore start))
|
||||
(let ((items nil) (i start))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
((:list-item :ordered-item)
|
||||
(push (cons (car class) (cdr class)) items) (incf i))
|
||||
(:blank
|
||||
(if (and (< (1+ i) (length lines))
|
||||
(let ((nc (classify-line
|
||||
(string-trim (list #\return)
|
||||
(aref lines (1+ i))))))
|
||||
(member (car nc) '(:list-item :ordered-item))))
|
||||
(progn (push (cons :blank-sep nil) items) (incf i))
|
||||
(progn (incf i) (loop-finish))))
|
||||
(t (loop-finish)))))
|
||||
(let ((nodes nil))
|
||||
(dolist (item (nreverse items))
|
||||
(let ((type (car item)) (content (cdr item)))
|
||||
(when (and content (not (string= content "")))
|
||||
(push (make-md-node type :children (parse-inline content)) nodes))))
|
||||
(values (nreverse nodes) i))))
|
||||
|
||||
(defun parse-code-block (lines start lang)
|
||||
(let ((code-lines nil)
|
||||
(i (1+ start))
|
||||
(fence-char (char (aref lines start) 0))
|
||||
(fence-len (loop for c across (aref lines start)
|
||||
while (char= c (char (aref lines start) 0)) count c)))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line)))
|
||||
(when (and (>= (length line) fence-len)
|
||||
(every (lambda (c) (char= c fence-char))
|
||||
(subseq line 0 fence-len))
|
||||
(or (= (length line) fence-len)
|
||||
(every (lambda (c) (find c " \t"))
|
||||
(subseq line fence-len))))
|
||||
(incf i) (loop-finish))
|
||||
(push line code-lines)
|
||||
(incf i)))
|
||||
(values (make-md-node :code-block
|
||||
:properties (list :language (and lang (not (string= lang "")) lang))
|
||||
:content
|
||||
(with-output-to-string (s)
|
||||
(loop for cl in (nreverse code-lines)
|
||||
for first = t then nil
|
||||
do (unless first (terpri s)) (princ cl s))))
|
||||
i)))
|
||||
|
||||
(defun parse-diff-block (lines start)
|
||||
(let ((diff-lines nil) (i start))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((raw-line (aref lines i))
|
||||
(line (string-trim (list #\return) raw-line))
|
||||
(class (classify-line line)))
|
||||
(case (car class)
|
||||
((:diff-header :diff-line) (push line diff-lines) (incf i))
|
||||
(:blank (incf i) (loop-finish))
|
||||
(t (loop-finish)))))
|
||||
(let ((lines-list (nreverse diff-lines)))
|
||||
(values (make-md-node :diff-block
|
||||
:content
|
||||
(with-output-to-string (s)
|
||||
(loop for dl in lines-list
|
||||
for first = t then nil
|
||||
do (unless first (terpri s)) (princ dl s)))
|
||||
:properties (list :lines lines-list))
|
||||
i))))
|
||||
|
||||
(defun parse-blocks (text)
|
||||
(let ((lines (split-string-into-lines text)) (nodes nil) (i 0))
|
||||
(loop while (< i (length lines))
|
||||
do (let* ((line (string-trim (list #\return) (aref lines i)))
|
||||
(classification (classify-line line)))
|
||||
(case (car classification)
|
||||
(:blank (incf i))
|
||||
(:thematic-break (push (make-md-node :thematic-break) nodes) (incf i))
|
||||
(:paragraph
|
||||
(multiple-value-bind (node consumed) (parse-paragraph lines i)
|
||||
(push node nodes) (setf i consumed)))
|
||||
(:heading
|
||||
(let* ((level+content (cdr classification))
|
||||
(level (car level+content))
|
||||
(content (cdr level+content)))
|
||||
(push (make-md-node :heading :properties (list :level level)
|
||||
:children (parse-inline content)) nodes)
|
||||
(incf i)))
|
||||
(:blockquote
|
||||
(multiple-value-bind (node consumed) (parse-blockquote lines i)
|
||||
(push node nodes) (setf i consumed)))
|
||||
(:list-item
|
||||
(multiple-value-bind (node consumed) (parse-list lines i)
|
||||
(dolist (n node) (push n nodes)) (setf i consumed)))
|
||||
(:ordered-item
|
||||
(multiple-value-bind (node consumed) (parse-list lines i)
|
||||
(dolist (n node) (push n nodes)) (setf i consumed)))
|
||||
(:code-start
|
||||
(multiple-value-bind (node consumed)
|
||||
(parse-code-block lines i (cdr classification))
|
||||
(push node nodes) (setf i consumed)))
|
||||
(:diff-header
|
||||
(multiple-value-bind (node consumed) (parse-diff-block lines i)
|
||||
(push node nodes) (setf i consumed)))
|
||||
(t (incf i)))))
|
||||
(nreverse nodes)))
|
||||
|
||||
;; ─── Inline parser ────────────────────────────────────────────────────────────
|
||||
|
||||
(defun parse-inline (text)
|
||||
(unless (and text (> (length text) 0)) (return-from parse-inline nil))
|
||||
(let ((nodes nil) (i 0) (len (length text)))
|
||||
(loop while (< i len)
|
||||
do (let ((c (char text i)))
|
||||
(case c
|
||||
(#\*
|
||||
(multiple-value-bind (node consumed) (parse-star-emphasis text i len)
|
||||
(if node (progn (push node nodes) (setf i consumed))
|
||||
(progn (push (make-md-node :text :content (string c)) nodes) (incf i)))))
|
||||
(#\_
|
||||
(multiple-value-bind (node consumed) (parse-underscore-emphasis text i len)
|
||||
(if node (progn (push node nodes) (setf i consumed))
|
||||
(progn (push (make-md-node :text :content (string c)) nodes) (incf i)))))
|
||||
(#\`
|
||||
(multiple-value-bind (node consumed) (parse-inline-code text i len)
|
||||
(if node (progn (push node nodes) (setf i consumed))
|
||||
(progn (push (make-md-node :text :content (string c)) nodes) (incf i)))))
|
||||
(#\[
|
||||
(multiple-value-bind (node consumed) (parse-link text i len)
|
||||
(if node (progn (push node nodes) (setf i consumed))
|
||||
(progn (push (make-md-node :text :content (string c)) nodes) (incf i)))))
|
||||
(t (let ((start i))
|
||||
(incf i)
|
||||
(loop while (< i len)
|
||||
do (let ((nc (char text i)))
|
||||
(if (find nc "*_`[") (loop-finish)
|
||||
(progn
|
||||
(when (and (< (1+ i) len)
|
||||
(find nc "*_")
|
||||
(char= nc (char text (1+ i))))
|
||||
(loop-finish))
|
||||
(incf i)))))
|
||||
(push (make-md-node :text :content (subseq text start i)) nodes))))))
|
||||
(nreverse nodes)))
|
||||
|
||||
(defun parse-star-emphasis (text i len)
|
||||
(when (>= i len) (return-from parse-star-emphasis (values nil i)))
|
||||
(if (and (< (1+ i) len) (char= (char text (1+ i)) #\*))
|
||||
(let ((close (find-closing-marker text (+ i 2) "**")))
|
||||
(if close
|
||||
(values (make-md-node :bold :children (parse-inline (subseq text (+ i 2) close)))
|
||||
(+ close 2))
|
||||
(values nil i)))
|
||||
(let ((close (find-closing-marker text (1+ i) "*")))
|
||||
(if close
|
||||
(values (make-md-node :italic :children (parse-inline (subseq text (1+ i) close)))
|
||||
(1+ close))
|
||||
(values nil i)))))
|
||||
|
||||
(defun parse-underscore-emphasis (text i len)
|
||||
(when (>= i len) (return-from parse-underscore-emphasis (values nil i)))
|
||||
(when (and (> i 0) (not (find (char text (1- i)) " \t\n\r")))
|
||||
(return-from parse-underscore-emphasis (values nil i)))
|
||||
(if (and (< (1+ i) len) (char= (char text (1+ i)) #\_))
|
||||
(let ((close (find-closing-marker text (+ i 2) "__")))
|
||||
(if close
|
||||
(values (make-md-node :bold :children (parse-inline (subseq text (+ i 2) close)))
|
||||
(+ close 2))
|
||||
(values nil i)))
|
||||
(let ((close (find-closing-marker text (1+ i) "_")))
|
||||
(if (and close
|
||||
(or (>= (1+ close) len)
|
||||
(find (char text (1+ close)) " \t\n\r.,;:!?")))
|
||||
(values (make-md-node :italic :children (parse-inline (subseq text (1+ i) close)))
|
||||
(1+ close))
|
||||
(values nil i)))))
|
||||
|
||||
(defun parse-inline-code (text i len)
|
||||
(when (or (>= i len) (not (char= (char text i) #\`)))
|
||||
(return-from parse-inline-code (values nil i)))
|
||||
(let ((bt-count (loop for j from i below (min len (+ i 3))
|
||||
while (char= (char text j) #\`) count j)))
|
||||
(let ((close (find-closing-marker text (+ i bt-count)
|
||||
(make-string bt-count :initial-element #\`))))
|
||||
(if close
|
||||
(values (make-md-node :inline-code
|
||||
:content (subseq text (+ i bt-count) close))
|
||||
(+ close bt-count))
|
||||
(values nil i)))))
|
||||
|
||||
(defun parse-link (text i len)
|
||||
(when (or (>= i len) (not (char= (char text i) #\[)))
|
||||
(return-from parse-link (values nil i)))
|
||||
(let ((close-bracket (find-closing-marker text (1+ i) "]")))
|
||||
(unless close-bracket (return-from parse-link (values nil i)))
|
||||
(when (or (>= (1+ close-bracket) len)
|
||||
(not (char= (char text (1+ close-bracket)) #\()))
|
||||
(return-from parse-link (values nil i)))
|
||||
(let ((close-paren (find-closing-marker text (+ close-bracket 2) ")")))
|
||||
(unless close-paren (return-from parse-link (values nil i)))
|
||||
(values (make-md-node :link
|
||||
:children (parse-inline (subseq text (1+ i) close-bracket))
|
||||
:url (subseq text (+ close-bracket 2) close-paren))
|
||||
(1+ close-paren)))))
|
||||
|
||||
;; ─── Syntax highlighting ──────────────────────────────────────────────────────
|
||||
|
||||
(defun get-highlighter (lang)
|
||||
(cdr (assoc lang
|
||||
'(("lisp" . (:comment (";" "#|" ";;") :string ("\"")
|
||||
:keyword ("defun" "defmacro" "defmethod" "defgeneric"
|
||||
"defvar" "defparameter" "defconstant" "defstruct"
|
||||
"defclass" "deftype" "define-condition"
|
||||
"let" "let*" "flet" "labels" "macrolet"
|
||||
"if" "when" "unless" "cond" "case" "ecase" "typecase"
|
||||
"loop" "do" "dolist" "dotimes" "tagbody" "go"
|
||||
"block" "return" "return-from"
|
||||
"progn" "prog1" "prog2"
|
||||
"lambda" "function" "quote"
|
||||
"setf" "setq" "push" "pop" "incf" "decf"
|
||||
"in-package" "defpackage" "export" "import"
|
||||
"handler-case" "handler-bind" "ignore-errors"
|
||||
"multiple-value-bind" "multiple-value-call"
|
||||
"destructuring-bind"
|
||||
"declare" "the" "values"
|
||||
"and" "or" "not" "null"
|
||||
"car" "cdr" "first" "rest" "second"
|
||||
"cons" "list" "append" "nconc"
|
||||
"mapcar" "mapc" "reduce"
|
||||
"find" "position" "count" "subseq"
|
||||
"format" "princ" "print" "write" "read"
|
||||
"load" "compile" "eval"
|
||||
"make-instance" "slot-value"
|
||||
"type-of" "class-of")
|
||||
:builtin ("t" "nil"
|
||||
"*standard-output*" "*standard-input*"
|
||||
"*error-output*" "*debug-io*"
|
||||
"*package*" "*print-circle*")))
|
||||
|
||||
("common-lisp" . (:comment (";" "#|" ";;") :string ("\"")
|
||||
:keyword ("defun" "defmacro" "defmethod" "defgeneric"
|
||||
"let" "if" "when" "unless" "cond" "case"
|
||||
"loop" "do" "dolist" "dotimes"
|
||||
"return" "return-from" "block"
|
||||
"lambda" "function" "quote"
|
||||
"setf" "setq" "push" "pop" "incf" "decf"
|
||||
"handler-case" "handler-bind"
|
||||
"declare" "the" "values"
|
||||
"defpackage" "in-package" "export" "import"
|
||||
"error" "warn" "assert"
|
||||
"car" "cdr" "first" "rest"
|
||||
"cons" "list" "append" "mapcar" "reduce"
|
||||
"format" "princ" "print" "read" "load"
|
||||
"make-instance")
|
||||
:builtin ("t" "nil")))
|
||||
|
||||
("python" . (:comment ("#") :string ("\"" "'" "\"\"\"" "'''")
|
||||
:keyword ("def" "class" "return" "yield" "import" "from"
|
||||
"if" "elif" "else" "for" "while" "in" "not"
|
||||
"try" "except" "finally" "raise" "with" "pass"
|
||||
"break" "continue" "lambda" "global"
|
||||
"assert" "del" "is"
|
||||
"self" "cls" "async" "await")
|
||||
:builtin ("None" "True" "False")))
|
||||
|
||||
("javascript" . (:comment ("//" "/*") :string ("\"" "'" "`")
|
||||
:keyword ("function" "class" "const" "let" "var"
|
||||
"if" "else" "for" "while" "do" "switch"
|
||||
"return" "break" "continue"
|
||||
"try" "catch" "finally" "throw"
|
||||
"new" "this" "super" "delete" "typeof"
|
||||
"import" "export" "from" "default"
|
||||
"async" "await" "yield" "of")
|
||||
:builtin ("true" "false" "null" "undefined" "NaN")))
|
||||
|
||||
("bash" . (:comment ("#") :string ("\"" "'")
|
||||
:keyword ("if" "then" "else" "elif" "fi" "for" "while"
|
||||
"done" "case" "esac" "in" "function" "return"
|
||||
"export" "local" "unset" "source"
|
||||
"echo" "printf" "read" "test" "let" "declare")
|
||||
:builtin ("true" "false" "cd" "ls" "cat" "grep" "sed"
|
||||
"mv" "cp" "rm" "mkdir" "touch" "find" "wc"
|
||||
"head" "tail" "date" "sleep" "kill")))
|
||||
|
||||
("shell" . (:comment ("#") :string ("\"" "'")
|
||||
:keyword ("if" "then" "else" "elif" "fi" "for" "while"
|
||||
"done" "case" "esac" "in" "function" "return"
|
||||
"export" "local" "unset" "source"
|
||||
"echo" "printf" "read" "test")
|
||||
:builtin ("true" "false" "cd" "ls" "grep" "sed"
|
||||
"mv" "cp" "rm" "mkdir" "touch" "find"))))
|
||||
:test #'string=)))
|
||||
|
||||
(defun tokenize-line (line highlighter)
|
||||
(let ((tokens nil) (i 0) (len (length line))
|
||||
(comment-chars (getf highlighter :comment))
|
||||
(string-chars (getf highlighter :string))
|
||||
(keywords (getf highlighter :keyword))
|
||||
(builtins (getf highlighter :builtin)))
|
||||
(loop while (< i len)
|
||||
do (let ((c (char line i)))
|
||||
(cond
|
||||
((find c " \t")
|
||||
(let ((start i))
|
||||
(loop while (and (< i len) (find (char line i) " \t")) do (incf i))
|
||||
(push (cons (subseq line start i) :plain) tokens)))
|
||||
((and comment-chars
|
||||
(some (lambda (cc)
|
||||
(and (<= (+ i (length cc)) len)
|
||||
(string= cc (subseq line i (+ i (length cc))))))
|
||||
comment-chars))
|
||||
(push (cons (subseq line i) :comment) tokens) (setf i len))
|
||||
((and string-chars (some (lambda (s) (find c s)) string-chars))
|
||||
(let ((start i))
|
||||
(incf i)
|
||||
(let ((triple (and (< i (1- len)) (char= (char line i) c)
|
||||
(char= (char line (1+ i)) c))))
|
||||
(if triple
|
||||
(progn (incf i 2)
|
||||
(loop while (and (< i len)
|
||||
(not (and (char= (char line i) c)
|
||||
(< (1+ i) len)
|
||||
(char= (char line (1+ i)) c)
|
||||
(< (+ i 2) len)
|
||||
(char= (char line (+ i 2)) c))))
|
||||
do (incf i))
|
||||
(incf i 3))
|
||||
(progn (loop while (and (< i len) (char/= (char line i) c))
|
||||
do (incf i))
|
||||
(when (< i len) (incf i)))))
|
||||
(push (cons (subseq line start i) :string) tokens)))
|
||||
((or (digit-char-p c)
|
||||
(and (find c "+-") (< (1+ i) len) (digit-char-p (char line (1+ i)))))
|
||||
(let ((start i))
|
||||
(loop while (and (< i len) (not (find (char line i) " \t()[]{}'\";:#")))
|
||||
do (incf i))
|
||||
(let ((token (subseq line start i)))
|
||||
(if (digit-char-p (char token 0))
|
||||
(push (cons token :number) tokens)
|
||||
(push (cons token :plain) tokens)))))
|
||||
((or (alpha-char-p c)
|
||||
(and (find c "-_?!*<>=") (> len 1)))
|
||||
(let ((start i))
|
||||
(loop while (and (< i len)
|
||||
(or (alphanumericp (char line i))
|
||||
(find (char line i) "-_?!*<>=")))
|
||||
do (incf i))
|
||||
(let* ((token (subseq line start i))
|
||||
(down (string-downcase token)))
|
||||
(cond
|
||||
((find down keywords :test #'string=)
|
||||
(push (cons token :keyword) tokens))
|
||||
((find down builtins :test #'string=)
|
||||
(push (cons token :builtin) tokens))
|
||||
(t (if (and (< i len) (char= (char line i) #\())
|
||||
(push (cons token :function) tokens)
|
||||
(push (cons token :plain) tokens)))))))
|
||||
(t (push (cons (string c) :plain) tokens) (incf i)))))
|
||||
(nreverse tokens)))
|
||||
|
||||
(defun highlight-code (code language)
|
||||
(let ((highlighter (get-highlighter (and language (string-downcase language)))))
|
||||
(unless highlighter (return-from highlight-code (list (cons code :plain))))
|
||||
(let ((tokens nil))
|
||||
(with-input-from-string (stream code)
|
||||
(loop for line = (read-line stream nil nil) while line
|
||||
do (let ((line-tokens (tokenize-line line highlighter)))
|
||||
(when tokens (push (cons (string #\Newline) :plain) tokens))
|
||||
(setf tokens (nconc (nreverse line-tokens) tokens)))))
|
||||
(nreverse tokens))))
|
||||
|
||||
(defun apply-highlight-token (token category)
|
||||
(let ((code (case category
|
||||
(:keyword "33") (:builtin "36")
|
||||
(:function "34") (:comment "2") (:string "32") (:number "35")
|
||||
(t nil))))
|
||||
(if code (format nil "~c[~am~a~c[0m" #\Escape code token #\Escape) token)))
|
||||
|
||||
(defun apply-highlight-style (char-vector)
|
||||
(coerce char-vector 'string))
|
||||
|
||||
;; ─── Diff rendering ───────────────────────────────────────────────────────────
|
||||
|
||||
(defun string-prefix-p (prefix string)
|
||||
(and (>= (length string) (length prefix))
|
||||
(string= prefix (subseq string 0 (length prefix)))))
|
||||
|
||||
(defun classify-diff-line (line)
|
||||
(cond ((string-prefix-p "+++ " line) :file-header)
|
||||
((string-prefix-p "--- " line) :file-header)
|
||||
((string-prefix-p "@@" line) :hunk-header)
|
||||
((string-prefix-p "+" line) :added)
|
||||
((string-prefix-p "-" line) :removed)
|
||||
(t :context)))
|
||||
|
||||
;; ─── Rendering ────────────────────────────────────────────────────────────────
|
||||
|
||||
(defun apply-style (style text)
|
||||
(let ((code (cond
|
||||
((eql style :bold) "1") ((eql style :italic) "3")
|
||||
((eql style :dim) "2") ((eql style :code) "0")
|
||||
((eql style :link) "4;36") ((eql style :url) "4;2")
|
||||
((eql style :underline) "4") ((eql style :strike) "9")
|
||||
((eql style :black) "30") ((eql style :red) "31")
|
||||
((eql style :green) "32") ((eql style :yellow) "33")
|
||||
((eql style :blue) "34") ((eql style :magenta) "35")
|
||||
((eql style :cyan) "36") ((eql style :white) "37")
|
||||
((eql style :bright-black) "90") ((eql style :bright-red) "91")
|
||||
((eql style :bright-green) "92") ((eql style :bright-yellow) "93")
|
||||
((eql style :bright-blue) "94") ((eql style :bright-magenta) "95")
|
||||
((eql style :bright-cyan) "96") ((eql style :bright-white) "97")
|
||||
((string= style "bold") "1") ((string= style "italic") "3")
|
||||
((string= style "dim") "2") ((string= style "code") "0")
|
||||
((string= style "link") "4;36") ((string= style "url") "4;2")
|
||||
((string= style "bright-cyan") "96")
|
||||
((string= style "bright-yellow") "93")
|
||||
((string= style "bright-white") "97")
|
||||
((string= style "bright-red") "91")
|
||||
((string= style "bright-green") "92")
|
||||
((string= style "bright-blue") "94")
|
||||
((string= style "bright-magenta") "95")
|
||||
((string= style "cyan") "36") ((string= style "yellow") "33")
|
||||
((string= style "red") "31") ((string= style "green") "32")
|
||||
((string= style "blue") "34") ((string= style "magenta") "35")
|
||||
((string= style "white") "37") ((string= style "black") "30")
|
||||
(t nil))))
|
||||
(if code (format nil "~c[~am~a~c[0m" #\Escape code text #\Escape) text)))
|
||||
|
||||
(defun render-inline (children)
|
||||
(if (null children) ""
|
||||
(with-output-to-string (s)
|
||||
(dolist (child children)
|
||||
(let ((type (getf child :type)))
|
||||
(case type
|
||||
(:text (princ (or (getf child :content) "") s))
|
||||
(:bold (princ (apply-style :bold (render-inline (getf child :children))) s))
|
||||
(:italic (princ (apply-style :italic (render-inline (getf child :children))) s))
|
||||
(:inline-code (princ (apply-style :code (or (getf child :content) "")) s))
|
||||
(:link (let ((text (render-inline (getf child :children)))
|
||||
(url (or (getf child :url) "")))
|
||||
(princ (apply-style :link text) s)
|
||||
(when (and url (not (string= url "")))
|
||||
(princ " " s)
|
||||
(princ (apply-style :url (format nil "(~a)" url)) s))))
|
||||
(t (princ (or (getf child :content) "") s))))))))
|
||||
|
||||
(defun render-heading (node)
|
||||
(let* ((level (or (getf (getf node :properties) :level) 1))
|
||||
(prefix (make-string (min level 6) :initial-element #\#))
|
||||
(text (render-inline (getf node :children)))
|
||||
(color (cond ((= level 1) :bright-cyan) ((= level 2) :bright-yellow)
|
||||
(t :bright-white))))
|
||||
(list (apply-style color (concatenate 'string prefix " " text)))))
|
||||
|
||||
(defun render-paragraph (node)
|
||||
(list (render-inline (getf node :children))))
|
||||
|
||||
(defun render-blockquote (node)
|
||||
(list (apply-style :dim (concatenate 'string "> " (render-inline (getf node :children))))))
|
||||
|
||||
(defun render-code-block (node)
|
||||
(let* ((language (or (getf (getf node :properties) :language) ""))
|
||||
(content (or (getf node :content) ""))
|
||||
(highlighted (unless (or (null language) (string= language ""))
|
||||
(highlight-code content language)))
|
||||
(lines nil))
|
||||
(when (and language (not (string= language "")))
|
||||
(push (apply-style :dim (format nil " ~~~~~~ ~a" language)) lines))
|
||||
(if highlighted
|
||||
(let ((cl (make-array 0 :element-type 'character
|
||||
:fill-pointer 0 :adjustable t))
|
||||
(output nil))
|
||||
(dolist (pair highlighted)
|
||||
(let ((token (car pair)) (category (cdr pair)))
|
||||
(cond ((string= token (string #\Newline))
|
||||
(push (apply-highlight-style cl) output)
|
||||
(setf cl (make-array 0 :element-type 'character
|
||||
:fill-pointer 0 :adjustable t)))
|
||||
(t (let ((colored (apply-highlight-token token category)))
|
||||
(loop for ch across colored
|
||||
do (vector-push-extend ch cl)))))))
|
||||
(when (> (length cl) 0) (push (apply-highlight-style cl) output))
|
||||
(setf lines (nconc lines (nreverse output))))
|
||||
(with-input-from-string (s content)
|
||||
(loop for line = (read-line s nil nil) while line
|
||||
do (push (apply-style :code line) lines))))
|
||||
(nreverse lines)))
|
||||
|
||||
(defun render-diff-block (node)
|
||||
(let* ((lines (getf (getf node :properties) :lines)) (result nil))
|
||||
(dolist (line (or lines
|
||||
(and (getf node :content)
|
||||
(let ((l (split-string-into-lines (getf node :content))))
|
||||
(loop for i from 0 below (length l) collect (aref l i))))))
|
||||
(let* ((class (classify-diff-line line))
|
||||
(color (case class
|
||||
(:added "32") (:removed "31")
|
||||
(:hunk-header "36") (:file-header "1;36") (t nil))))
|
||||
(if color
|
||||
(push (format nil "~c[~am~a~c[0m" #\Escape color line #\Escape) result)
|
||||
(push line result))))
|
||||
(nreverse result)))
|
||||
|
||||
(defun render-thematic-break (node)
|
||||
(declare (ignore node))
|
||||
(list (apply-style :dim "──────────────────────────────────────────────")))
|
||||
|
||||
(defun render-list-item (node)
|
||||
(list (concatenate 'string
|
||||
(if (eql (getf node :type) :ordered-item) " 1." " * ")
|
||||
(render-inline (getf node :children)))))
|
||||
|
||||
(defun render-md-node (node)
|
||||
(let ((type (getf node :type)))
|
||||
(case type
|
||||
(:heading (render-heading node))
|
||||
(:paragraph (render-paragraph node))
|
||||
(:blockquote (render-blockquote node))
|
||||
(:code-block (render-code-block node))
|
||||
(:diff-block (render-diff-block node))
|
||||
(:thematic-break (render-thematic-break node))
|
||||
(:list-item (render-list-item node))
|
||||
(:ordered-item (render-list-item node))
|
||||
(t (list "")))))
|
||||
|
||||
(defun render-md (nodes)
|
||||
(let ((lines nil))
|
||||
(dolist (node nodes) (setf lines (nconc lines (render-md-node node))))
|
||||
lines))
|
||||
|
||||
(defun render-markdown (text)
|
||||
(let ((nodes (parse-blocks text)) (parts nil))
|
||||
(dolist (line (render-md nodes)) (push line parts))
|
||||
(with-output-to-string (s)
|
||||
(loop for part in (nreverse parts)
|
||||
for first = t then nil
|
||||
do (unless first (terpri s)) (princ part s)))))
|
||||
@@ -1,5 +1,5 @@
|
||||
(defpackage :cl-tui.box
|
||||
(:use :cl :cl-tui.backend :cl-tui.layout)
|
||||
(defpackage :cl-tty.box
|
||||
(:use :cl :cl-tty.backend :cl-tty.layout)
|
||||
(:export
|
||||
;; Box
|
||||
#:box #:make-box
|
||||
@@ -28,4 +28,4 @@
|
||||
;; Theme engine
|
||||
#:theme #:make-theme #:theme-mode
|
||||
#:theme-color #:load-preset #:define-preset))
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui-box-test)
|
||||
(in-package :cl-tty-box-test)
|
||||
(in-suite box-suite)
|
||||
|
||||
(defun make-capturing-backend ()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
;; ── Component Protocol ────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package #:cl-tui.container)
|
||||
(in-package #:cl-tty.container)
|
||||
|
||||
(defclass scroll-box (dirty-mixin)
|
||||
((children :initform nil :initarg :children :accessor scroll-box-children :type list)
|
||||
|
||||
13
src/components/select-package.lisp
Normal file
13
src/components/select-package.lisp
Normal file
@@ -0,0 +1,13 @@
|
||||
(defpackage :cl-tty.select
|
||||
(:use :cl :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export
|
||||
#:select #:make-select
|
||||
#:select-options #:select-filter
|
||||
#:select-selected-index #:select-on-select
|
||||
#:select-layout-node
|
||||
#:select-filtered-options
|
||||
#:select-next #:select-prev
|
||||
#:select-visible-options
|
||||
#:select-handle-key
|
||||
#:render
|
||||
#:fuzzy-match-p))
|
||||
94
src/components/select.lisp
Normal file
94
src/components/select.lisp
Normal file
@@ -0,0 +1,94 @@
|
||||
(in-package #:cl-tty.select)
|
||||
|
||||
(defclass select (dirty-mixin)
|
||||
((options :initform nil :initarg :options :accessor select-options :type list)
|
||||
(filter :initform nil :initarg :filter :accessor select-filter :type (or string null))
|
||||
(selected-index :initform 0 :initarg :selected-index :accessor select-selected-index :type fixnum)
|
||||
(on-select :initform nil :initarg :on-select :accessor select-on-select)
|
||||
(layout-node :initform (make-layout-node) :initarg :layout-node :accessor select-layout-node)))
|
||||
|
||||
(defun make-select (&key options filter on-select)
|
||||
(make-instance 'select :options (or options nil) :filter filter :on-select on-select))
|
||||
|
||||
(defmethod component-layout-node ((sel select)) (select-layout-node sel))
|
||||
|
||||
(defun select-filtered-options (sel)
|
||||
(let* ((filter (select-filter sel)) (all-options (select-options sel))
|
||||
(filtered (if (null filter) all-options
|
||||
(let ((lower (string-downcase filter)))
|
||||
(remove-if-not
|
||||
(lambda (opt)
|
||||
(or (getf opt :category)
|
||||
(let ((title (string-downcase (getf opt :title))))
|
||||
(or (search lower title) (fuzzy-match-p lower title)))))
|
||||
all-options)))))
|
||||
(loop for opt in filtered for i from 0
|
||||
collect (list i (position opt all-options) opt))))
|
||||
|
||||
(defun fuzzy-match-p (query target)
|
||||
(let* ((q (remove-duplicates (coerce (string-downcase query) 'list)))
|
||||
(tg (remove-duplicates (coerce (string-downcase target) 'list)))
|
||||
(intersection (length (intersection q tg)))
|
||||
(union (length (union q tg))))
|
||||
(if (zerop union) nil (> (/ (float intersection) union) 0.3))))
|
||||
|
||||
(defun select-clamp-index (sel)
|
||||
(let* ((filtered (select-filtered-options sel)) (count (length filtered)))
|
||||
(if (zerop count) (setf (select-selected-index sel) 0)
|
||||
(setf (select-selected-index sel) (max 0 (min (select-selected-index sel) (1- count)))))))
|
||||
|
||||
(defun select-next (sel)
|
||||
(let* ((filtered (select-filtered-options sel)) (count (length filtered))
|
||||
(current (select-selected-index sel)))
|
||||
(when (plusp count)
|
||||
(loop for i from 1 below count
|
||||
for idx = (mod (+ current i) count)
|
||||
for opt = (third (nth idx filtered))
|
||||
when (not (getf opt :category))
|
||||
do (setf (select-selected-index sel) idx) (mark-dirty sel) (return)))))
|
||||
|
||||
(defun select-prev (sel)
|
||||
(let* ((filtered (select-filtered-options sel)) (count (length filtered))
|
||||
(current (select-selected-index sel)))
|
||||
(when (plusp count)
|
||||
(loop for i from 1 below count
|
||||
for idx = (mod (- current i) count)
|
||||
for opt = (third (nth idx filtered))
|
||||
when (not (getf opt :category))
|
||||
do (setf (select-selected-index sel) idx) (mark-dirty sel) (return)))))
|
||||
|
||||
(defun select-handle-key (sel event)
|
||||
(let ((key (key-event-key event)) (ctrl (key-event-ctrl event)))
|
||||
(cond
|
||||
((or (eql key :down) (and ctrl (eql key :n))) (select-next sel) t)
|
||||
((or (eql key :up) (and ctrl (eql key :p))) (select-prev sel) t)
|
||||
((eql key :enter)
|
||||
(let* ((filtered (select-filtered-options sel)) (idx (select-selected-index sel))
|
||||
(item (when (< idx (length filtered)) (third (nth idx filtered)))))
|
||||
(when item (let ((cb (select-on-select sel))) (when cb (funcall cb item)))) t))
|
||||
((eql key :escape) nil) (t nil))))
|
||||
|
||||
(defun select-visible-options (sel)
|
||||
(let* ((ln (select-layout-node sel)) (height (if ln (layout-node-height ln) 80))
|
||||
(filtered (select-filtered-options sel)) (sel-idx (select-selected-index sel))
|
||||
(half (floor (1- height) 2)) (start (max 0 (- sel-idx half)))
|
||||
(end (min (length filtered) (+ start height))))
|
||||
(subseq filtered start end)))
|
||||
|
||||
(defmethod render ((sel select) backend)
|
||||
(let* ((ln (select-layout-node sel)) (x 0) (y 0)
|
||||
(w (if ln (layout-node-width ln) 80))
|
||||
(visible (select-visible-options sel)) (sel-idx (select-selected-index sel)))
|
||||
(dolist (item visible)
|
||||
(let* ((display-idx (first item)) (option (third item))
|
||||
(title (getf option :title)) (cat (getf option :category))
|
||||
(selected (eql display-idx sel-idx))
|
||||
(display (if (> (length title) (1- w))
|
||||
(concatenate 'string (subseq title 0 (1- w)) "…") title)))
|
||||
(cond (cat (draw-text backend x y display :text-muted nil))
|
||||
(selected
|
||||
(draw-rect backend x y w 1 :bg :accent)
|
||||
(draw-text backend x y display :background :accent))
|
||||
(t (draw-text backend x y display nil nil)))
|
||||
(incf y 1)))
|
||||
(values)))
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package #:cl-tui.container)
|
||||
(in-package #:cl-tty.container)
|
||||
|
||||
(defclass tab-bar (dirty-mixin)
|
||||
((tabs :initform nil :initarg :tabs :accessor tab-bar-tabs :type list)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; TextInput class
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
;; ── Text Renderable ────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package #:cl-tui.input)
|
||||
(in-package #:cl-tty.input)
|
||||
|
||||
;;; ---------------------------------------------------------------------------
|
||||
;;; Utility: split string (local copy for dependency-free operation)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui-box-test)
|
||||
(in-package :cl-tty-box-test)
|
||||
(in-suite box-suite)
|
||||
|
||||
(test theme-create-default
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(in-package :cl-tui.box)
|
||||
(in-package :cl-tty.box)
|
||||
|
||||
;; ── Theme Engine ──────────────────────────────────────────────
|
||||
|
||||
|
||||
43
tests/dialog-tests.lisp
Normal file
43
tests/dialog-tests.lisp
Normal file
@@ -0,0 +1,43 @@
|
||||
;;; dialog-tests.lisp — Tests for cl-tty.dialog
|
||||
|
||||
(defpackage :cl-tty-dialog-test
|
||||
(:use :cl :cl-tty.dialog :fiveam))
|
||||
|
||||
(in-package :cl-tty-dialog-test)
|
||||
|
||||
(def-suite dialog-suite :description "Dialog + Toast tests for cl-tty.dialog")
|
||||
(in-suite dialog-suite)
|
||||
|
||||
(def-test dialog-create ()
|
||||
(let ((d (make-instance 'dialog :title "Test")))
|
||||
(is-true (typep d 'dialog))
|
||||
(is (equal "Test" (dialog-title d)))))
|
||||
|
||||
(def-test dialog-size-small ()
|
||||
(multiple-value-bind (w h) (dialog-size-pixels :small)
|
||||
(is (= 40 w))
|
||||
(is (= 8 h))))
|
||||
|
||||
(def-test dialog-size-medium ()
|
||||
(multiple-value-bind (w h) (dialog-size-pixels :medium)
|
||||
(is (= 60 w))
|
||||
(is (= 16 h))))
|
||||
|
||||
(def-test dialog-push-pop ()
|
||||
(let ((*dialog-stack* nil))
|
||||
(push-dialog (make-instance 'dialog :title "D1"))
|
||||
(is (= 1 (length *dialog-stack*)))
|
||||
(push-dialog (make-instance 'dialog :title "D2"))
|
||||
(is (= 2 (length *dialog-stack*)))
|
||||
(pop-dialog)
|
||||
(is (= 1 (length *dialog-stack*)))))
|
||||
|
||||
(def-test toast-create ()
|
||||
(let ((*toasts* nil))
|
||||
(toast "Hello" :variant :info :duration 0)
|
||||
(is (= 1 (length *toasts*)))))
|
||||
|
||||
(def-test toast-dismiss ()
|
||||
(let ((*toasts* (list (make-instance 'toast :message "T" :variant :info))))
|
||||
(dismiss-toast (first *toasts*))
|
||||
(is (= 0 (length *toasts*)))))
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-input-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input)
|
||||
(defpackage :cl-tty-input-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input)
|
||||
(:export #:run-tests))
|
||||
(in-package :cl-tui-input-test)
|
||||
(in-package :cl-tty-input-test)
|
||||
|
||||
(def-suite input-suite :description "Text input and keybinding tests")
|
||||
(in-suite input-suite)
|
||||
|
||||
205
tests/markdown-tests.lisp
Normal file
205
tests/markdown-tests.lisp
Normal file
@@ -0,0 +1,205 @@
|
||||
;;; markdown-tests.lisp — Tests for cl-tty.markdown
|
||||
|
||||
(defpackage :cl-tty-markdown-test
|
||||
(:use :cl :cl-tty.markdown :fiveam))
|
||||
|
||||
(in-package :cl-tty-markdown-test)
|
||||
|
||||
;; Test suite
|
||||
(def-suite :cl-tty-markdown-test
|
||||
:description "Markdown parser/renderer tests for cl-tty.markdown")
|
||||
|
||||
(in-suite :cl-tty-markdown-test)
|
||||
|
||||
;; ─── Parser tests ─────────────────────────────────────────────────────────────
|
||||
|
||||
(def-test heading-parsing ()
|
||||
(let* ((result (parse-blocks "# Hello World")) (node (first result)))
|
||||
(is-true (eql :heading (getf node :type)))
|
||||
(is (= 1 (getf (getf node :properties) :level)))))
|
||||
|
||||
(def-test heading-levels ()
|
||||
(loop for level from 1 to 6
|
||||
do (let* ((hashes (make-string level :initial-element #\#))
|
||||
(text (format nil "~a Heading ~d" hashes level))
|
||||
(result (parse-blocks text))
|
||||
(node (first result)))
|
||||
(is-true (eql :heading (getf node :type)))
|
||||
(is (= level (getf (getf node :properties) :level))))))
|
||||
|
||||
(def-test heading-with-inline-formatting ()
|
||||
(let* ((result (parse-blocks "# Hello **World**"))
|
||||
(node (first result)) (children (getf node :children)))
|
||||
(is-true (eql :heading (getf node :type)))
|
||||
(is (= 2 (length children)))
|
||||
(is-true (eql :text (getf (first children) :type)))
|
||||
(is-true (eql :bold (getf (second children) :type)))))
|
||||
|
||||
(def-test paragraph-parsing ()
|
||||
(let* ((result (parse-blocks "This is a paragraph.")) (node (first result)))
|
||||
(is-true (eql :paragraph (getf node :type)))))
|
||||
|
||||
(def-test paragraph-multi-line ()
|
||||
(let* ((result (parse-blocks "Line one\nLine two")) (node (first result)))
|
||||
(is-true (eql :paragraph (getf node :type)))))
|
||||
|
||||
(def-test bold-parsing ()
|
||||
(let* ((children (parse-inline "hello **world** here"))
|
||||
(bold-node (second children)))
|
||||
(is (= 3 (length children)))
|
||||
(is-true (eql :bold (getf bold-node :type)))))
|
||||
|
||||
(def-test italic-parsing ()
|
||||
(let* ((children (parse-inline "hello *world* here"))
|
||||
(italic-node (second children)))
|
||||
(is (= 3 (length children)))
|
||||
(is-true (eql :italic (getf italic-node :type)))))
|
||||
|
||||
(def-test bold-italic-combined ()
|
||||
(let ((children (parse-inline "**bold** and *italic*")))
|
||||
(is (= 3 (length children)))
|
||||
(is-true (eql :bold (getf (first children) :type)))
|
||||
(is-true (eql :italic (getf (third children) :type)))))
|
||||
|
||||
(def-test inline-code-parsing ()
|
||||
(let* ((children (parse-inline "use `foo` here"))
|
||||
(code-node (second children)))
|
||||
(is (= 3 (length children)))
|
||||
(is-true (eql :inline-code (getf code-node :type)))
|
||||
(is (equal "foo" (getf code-node :content)))))
|
||||
|
||||
(def-test link-parsing ()
|
||||
(let* ((children (parse-inline "click [here](https://x.com)"))
|
||||
(link-node (second children)))
|
||||
(is (= 2 (length children)))
|
||||
(is-true (eql :link (getf link-node :type)))
|
||||
(is (equal "https://x.com" (getf link-node :url)))
|
||||
(let ((link-text (getf link-node :children)))
|
||||
(is (= 1 (length link-text)))
|
||||
(is-true (eql :text (getf (first link-text) :type)))
|
||||
(is (equal "here" (getf (first link-text) :content))))))
|
||||
|
||||
(def-test code-block-parsing ()
|
||||
(let* ((text (format nil "```lisp~%(defun hello ())~% (print \"hi\")~%```"))
|
||||
(result (parse-blocks text)) (node (first result)))
|
||||
(is-true (eql :code-block (getf node :type)))
|
||||
(is (equal "lisp" (getf (getf node :properties) :language)))
|
||||
(is-true (search "(defun hello" (getf node :content)))))
|
||||
|
||||
(def-test code-block-unknown-language ()
|
||||
(let* ((text (format nil "```~%plain code~%```"))
|
||||
(result (parse-blocks text)) (node (first result)))
|
||||
(is-true (eql :code-block (getf node :type)))
|
||||
(is-false (getf (getf node :properties) :language))))
|
||||
|
||||
(def-test blockquote-parsing ()
|
||||
(let* ((result (parse-blocks "> This is a quote")) (node (first result)))
|
||||
(is-true (eql :blockquote (getf node :type)))))
|
||||
|
||||
(def-test list-item-parsing ()
|
||||
(let* ((result (parse-blocks "- First item")) (node (first result)))
|
||||
(is-true (eql :list-item (getf node :type)))))
|
||||
|
||||
(def-test ordered-list-parsing ()
|
||||
(let* ((result (parse-blocks "1. First item")) (node (first result)))
|
||||
(is-true (eql :ordered-item (getf node :type)))))
|
||||
|
||||
(def-test thematic-break-parsing ()
|
||||
(let* ((result (parse-blocks "---")) (node (first result)))
|
||||
(is-true (eql :thematic-break (getf node :type)))))
|
||||
|
||||
;; ─── Diff tests ───────────────────────────────────────────────────────────────
|
||||
|
||||
(def-test classify-diff-added ()
|
||||
(is (eql :added (classify-diff-line "+this is added"))))
|
||||
|
||||
(def-test classify-diff-removed ()
|
||||
(is (eql :removed (classify-diff-line "-this is removed"))))
|
||||
|
||||
(def-test classify-diff-hunk ()
|
||||
(is (eql :hunk-header (classify-diff-line "@@ -1,3 +1,4 @@"))))
|
||||
|
||||
(def-test classify-diff-context ()
|
||||
(is (eql :context (classify-diff-line " normal context"))))
|
||||
|
||||
;; ─── Syntax highlighting tests ────────────────────────────────────────────────
|
||||
(def-test highlight-lisp-keyword ()
|
||||
(let ((tokens (highlight-code "(defun hello ()" "lisp")))
|
||||
(is-true (some (lambda (pair) (and (search "defun" (car pair))
|
||||
(eql :keyword (cdr pair))))
|
||||
tokens))))
|
||||
|
||||
(def-test highlight-lisp-builtin ()
|
||||
"Test that a Lisp builtin like nil is highlighted as :builtin."
|
||||
(let ((tokens (highlight-code "(if t nil)" "lisp")))
|
||||
(is-true (some (lambda (pair) (and (string= (car pair) "nil")
|
||||
(eql :builtin (cdr pair))))
|
||||
tokens))))
|
||||
|
||||
(def-test highlight-unknown-language ()
|
||||
(let ((tokens (highlight-code "hello world" "unknown-xyz")))
|
||||
(every (lambda (pair) (eql :plain (cdr pair))) tokens)))
|
||||
|
||||
(def-test highlight-comment ()
|
||||
(let ((tokens (highlight-code "; this is a comment" "lisp")))
|
||||
(is-true (some (lambda (pair) (eql :comment (cdr pair))) tokens))))
|
||||
|
||||
;; ─── Render tests ─────────────────────────────────────────────────────────────
|
||||
|
||||
(def-test render-heading-output ()
|
||||
(let* ((node (make-md-node :heading :properties (list :level 2)
|
||||
:children (list (make-md-node :text :content "Test"))))
|
||||
(lines (render-md-node node)))
|
||||
(is (= 1 (length lines)))
|
||||
(is-true (> (length (first lines)) 0))))
|
||||
|
||||
(def-test render-paragraph-output ()
|
||||
(let* ((node (make-md-node :paragraph
|
||||
:children (list (make-md-node :text :content "Hello"))))
|
||||
(lines (render-md-node node)))
|
||||
(is (= 1 (length lines)))
|
||||
(is-true (search "Hello" (first lines)))))
|
||||
|
||||
(def-test render-thematic-break-output ()
|
||||
(let* ((node (make-md-node :thematic-break)) (lines (render-md-node node)))
|
||||
(is (= 1 (length lines)))))
|
||||
|
||||
(def-test render-code-block-output ()
|
||||
(let* ((node (make-md-node :code-block :content "(print \"hello\")"
|
||||
:properties (list :language "lisp")))
|
||||
(lines (render-md-node node)))
|
||||
(is-true (> (length lines) 0))))
|
||||
|
||||
(def-test render-diff-block-output ()
|
||||
(let* ((node (make-md-node :diff-block :properties
|
||||
(list :lines
|
||||
'("--- a/file" "+++ b/file" "@@ -1 +1 @@"
|
||||
"+added" "-removed" " context"))))
|
||||
(lines (render-md-node node)))
|
||||
(is (= 6 (length lines)))
|
||||
(is (search "added" (fourth lines)))
|
||||
(is (search "removed" (fifth lines)))))
|
||||
|
||||
;; ─── Integration tests ────────────────────────────────────────────────────────
|
||||
|
||||
(def-test markdown-integration ()
|
||||
(let* ((md (format nil "# Title~%~%This is **bold** and `code`.~%~%- Item 1~%- Item 2~%~%> A quote~%~%```lisp~%(defun hello ())~% (print \"hi\")~%```~%~%---"))
|
||||
(nodes (parse-blocks md)) (lines (render-md nodes)))
|
||||
(is-true (> (length lines) 5))
|
||||
(is-true (search "# Title" (first lines)))))
|
||||
|
||||
(def-test render-markdown-string ()
|
||||
(let ((result (render-markdown "**bold** text")))
|
||||
(is-true (stringp result))
|
||||
(is-true (> (length result) 0))))
|
||||
|
||||
(def-test md-node-text-simple ()
|
||||
(let ((node (make-md-node :text :content "hello")))
|
||||
(is (equal "hello" (md-node-text node)))))
|
||||
|
||||
(def-test md-node-text-nested ()
|
||||
(let ((node (make-md-node :paragraph :children
|
||||
(list (make-md-node :text :content "hello")
|
||||
(make-md-node :bold :children
|
||||
(list (make-md-node :text :content "world")))))))
|
||||
(is (equal "helloworld" (md-node-text node)))))
|
||||
@@ -1,7 +1,7 @@
|
||||
(defpackage :cl-tui-scrollbox-test
|
||||
(:use :cl :fiveam :cl-tui.backend :cl-tui.box :cl-tui.layout :cl-tui.input :cl-tui.container)
|
||||
(defpackage :cl-tty-scrollbox-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input :cl-tty.container)
|
||||
(:export #:run-tests))
|
||||
(in-package #:cl-tui-scrollbox-test)
|
||||
(in-package #:cl-tty-scrollbox-test)
|
||||
|
||||
(def-suite scrollbox-suite :description "ScrollBox + TabBar tests")
|
||||
(in-suite scrollbox-suite)
|
||||
|
||||
120
tests/select-tests.lisp
Normal file
120
tests/select-tests.lisp
Normal file
@@ -0,0 +1,120 @@
|
||||
(defpackage :cl-tty-select-test
|
||||
(:use :cl :fiveam :cl-tty.backend :cl-tty.box :cl-tty.layout :cl-tty.input :cl-tty.select)
|
||||
(:export #:run-tests))
|
||||
(in-package #:cl-tty-select-test)
|
||||
|
||||
(def-suite select-suite :description "Select widget tests")
|
||||
(in-suite select-suite)
|
||||
|
||||
(defun run-tests ()
|
||||
(let ((result (run 'select-suite)))
|
||||
(fiveam:explain! result)
|
||||
(uiop:quit 0)))
|
||||
|
||||
(test select-creates
|
||||
"A Select can be created with defaults."
|
||||
(let ((sel (make-select)))
|
||||
(is (typep sel 'select))
|
||||
(is-false (select-options sel))
|
||||
(is-false (select-filter sel))
|
||||
(is (= (select-selected-index sel) 0))))
|
||||
|
||||
(test select-with-options
|
||||
"A Select stores options."
|
||||
(let ((sel (make-select :options '((:title "Red" :value :red)
|
||||
(:title "Blue" :value :blue)))))
|
||||
(is (= (length (select-options sel)) 2))))
|
||||
|
||||
(test select-filtered-exact
|
||||
"Filter returns case-insensitive substring matches."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Red" :value :red)
|
||||
(:title "Green" :value :green)
|
||||
(:title "Blue" :value :blue)))))
|
||||
(setf (select-filter sel) "bl")
|
||||
(let ((filtered (select-filtered-options sel)))
|
||||
(is (= (length filtered) 1))
|
||||
(is (eql (getf (third (first filtered)) :value) :blue)))))
|
||||
|
||||
(test select-filtered-all
|
||||
"Nil filter returns all options."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Red" :value :red)
|
||||
(:title "Blue" :value :blue)))))
|
||||
(let ((filtered (select-filtered-options sel)))
|
||||
(is (= (length filtered) 2)))))
|
||||
|
||||
(test select-navigation
|
||||
"Select-next and select-prev navigate through options."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "A" :value :a)
|
||||
(:title "B" :value :b)
|
||||
(:title "C" :value :c)))))
|
||||
(is (= (select-selected-index sel) 0))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 1))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 2))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 0) "wraps forward")
|
||||
(select-prev sel)
|
||||
(is (= (select-selected-index sel) 2) "wraps backward")))
|
||||
|
||||
(test select-navigation-skips-categories
|
||||
"Navigation skips category header options."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Colors" :category t)
|
||||
(:title "Red" :value :red)
|
||||
(:title "Green" :value :green)
|
||||
(:title "Shapes" :category t)
|
||||
(:title "Circle" :value :circle)))))
|
||||
(is (= (select-selected-index sel) 0))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 1) "skipped category header at 0")
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 2))
|
||||
(select-next sel)
|
||||
(is (= (select-selected-index sel) 4) "skipped category header at 3")))
|
||||
|
||||
(test select-handle-key
|
||||
"Select handle-key dispatches navigation and selection."
|
||||
(let* ((result (list nil))
|
||||
(sel (make-select
|
||||
:options '((:title "A" :value :a) (:title "B" :value :b))
|
||||
:on-select (lambda (opt) (setf (car result) (getf opt :value))))))
|
||||
(select-handle-key sel (make-key-event :key :down))
|
||||
(is (= (select-selected-index sel) 1))
|
||||
(select-handle-key sel (make-key-event :key :up))
|
||||
(is (= (select-selected-index sel) 0))
|
||||
(select-handle-key sel (make-key-event :key :enter))
|
||||
(is (eql (car result) :a))))
|
||||
|
||||
(test select-handle-key-ctrl
|
||||
"Ctrl+N and Ctrl+P navigate like down/up."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "A" :value :a) (:title "B" :value :b) (:title "C" :value :c)))))
|
||||
(select-handle-key sel (make-key-event :key :n :ctrl t))
|
||||
(is (= (select-selected-index sel) 1))
|
||||
(select-handle-key sel (make-key-event :key :p :ctrl t))
|
||||
(is (= (select-selected-index sel) 0))))
|
||||
|
||||
(test select-visible-count
|
||||
"Visible options respects viewport height."
|
||||
(let* ((ln (make-layout-node))
|
||||
(sel (make-select
|
||||
:options (loop for i below 20 collect (list :title (format nil "Item ~D" i) :value i)))))
|
||||
(setf (select-layout-node sel) ln)
|
||||
(setf (layout-node-height ln) 5)
|
||||
(let ((visible (select-visible-options sel)))
|
||||
(is (<= (length visible) 5)))))
|
||||
|
||||
(test select-fuzzy-fallback
|
||||
"Fuzzy filter catches near-misses."
|
||||
(let ((sel (make-select
|
||||
:options '((:title "Nord" :value :nord)
|
||||
(:title "Tokyo Night" :value :tokyo)
|
||||
(:title "Catppuccin" :value :cat)))))
|
||||
(setf (select-filter sel) "nrd")
|
||||
(let ((filtered (select-filtered-options sel)))
|
||||
(is (= (length filtered) 1))
|
||||
(is (eql (getf (third (first filtered)) :value) :nord)))))
|
||||
Reference in New Issue
Block a user