AtlatestRepositorysigil-vt

sigil-vt / tree / src / sigilvt.sgl

1;;; (sigil vt) — the native VT/ANSI terminal core (Scheme surface).
2;;;
3;;; A thin wrapper over the %vt-* native builtins (native/vt.c). The emulator
4;;; is a pure bytes->grid state machine: feed pty output, drain damage /
5;;; replies / events, read the grid as cells or style-merged row-runs. All
6;;; state leaves through drained queues; the core never calls back into Scheme.
7;;;
8;;; Cells are immutable 4-vectors #(codepoint attr fg bg):
9;;; attr bitmask (see vt-attr-*); fg/bg: -1 default, 0..255 indexed,
10;;; #x1000000 + #xRRGGBB truecolor.
11;;;
12;;; This is the shared foundation slate's (slate term) façade rides on (M2);
13;;; the sigil-tui grid façade + vt-diff come in M3.
15(define-library (sigil vt)
16 (import (sigil core))
18 (export
19 ;; lifecycle
20 vt-make vt?
21 vt-feed! vt-feed-bytes!
22 vt-resize! vt-reset! vt-invalidate!
23 ;; geometry / cursor / modes
24 vt-cols vt-rows
25 vt-cursor-row vt-cursor-col
26 vt-cursor-visible? vt-cursor-style
27 vt-alt? vt-title
28 vt-bracketed-paste? vt-app-cursor?
29 vt-mouse-flags
30 vt-mouse-1000 vt-mouse-1002 vt-mouse-1003 vt-mouse-1006 vt-mouse-1007
31 ;; pending replies (DSR/DA) the consumer writes back to the pty
32 vt-take-output!
33 ;; damage (drained by the renderer)
34 vt-take-damage! vt-damaged?
35 ;; out-of-band events (title / bell / OSC-52 clipboard)
36 vt-take-events!
37 ;; grid access
38 vt-row-cells vt-row-text
39 vt-scrollback-count vt-scrollback-row
40 ;; the render seam: style-merged row runs #(text attr fg bg cursor?)
41 vt-row-runs vt-scrollback-runs
42 ;; cells
43 vt-cell-ch vt-cell-attr vt-cell-fg vt-cell-bg vt-blank-cell
44 vt-attr-bold vt-attr-dim vt-attr-italic vt-attr-underline
45 vt-attr-blink vt-attr-inverse vt-attr-hidden vt-attr-strike
46 vt-attr-wide vt-attr-continuation
47 ;; 256-color palette (index -> #xRRGGBB), for renderers
48 vt-color-256->rgb)
50 (begin
52 ;; ---- attribute bits (slate's superset) --------------------------------
53 (define vt-attr-bold 1)
54 (define vt-attr-dim 2)
55 (define vt-attr-italic 4)
56 (define vt-attr-underline 8)
57 (define vt-attr-blink 16)
58 (define vt-attr-inverse 32)
59 (define vt-attr-hidden 64)
60 (define vt-attr-strike 128)
61 ;; reserved for CJK double-width (wcwidth deferred to M3/M4)
62 (define vt-attr-wide 256)
63 (define vt-attr-continuation 512)
65 ;; ---- mouse-mode flag bits (DECSET) ------------------------------------
66 (define vt-mouse-1000 1)
67 (define vt-mouse-1002 2)
68 (define vt-mouse-1003 4)
69 (define vt-mouse-1006 8)
70 (define vt-mouse-1007 16)
72 ;; ---- cells (immutable 4-vectors) --------------------------------------
73 (define (vt-cell-ch c) (vector-ref c 0)) ; a CODEPOINT (int)
74 (define (vt-cell-attr c) (vector-ref c 1))
75 (define (vt-cell-fg c) (vector-ref c 2))
76 (define (vt-cell-bg c) (vector-ref c 3))
77 (define vt-blank-cell (vector 32 0 -1 -1))
79 ;; ---- lifecycle --------------------------------------------------------
80 (define (vt-make cols rows . rest)
81 (if (pair? rest)
82 (%vt-make cols rows (car rest))
83 (%vt-make cols rows 1000)))
84 (define (vt? x) (%vt? x))
85 (define (vt-feed! t s) (%vt-feed! t s))
86 (define (vt-feed-bytes! t bytes) (%vt-feed-bytes! t bytes))
87 (define (vt-resize! t cols rows) (%vt-resize! t cols rows))
88 (define (vt-reset! t) (%vt-reset! t))
89 (define (vt-invalidate! t) (%vt-invalidate! t))
91 ;; ---- accessors --------------------------------------------------------
92 (define (vt-cols t) (%vt-cols t))
93 (define (vt-rows t) (%vt-rows t))
94 (define (vt-cursor-row t) (%vt-cursor-row t))
95 (define (vt-cursor-col t) (%vt-cursor-col t))
96 (define (vt-cursor-visible? t) (%vt-cursor-visible? t))
97 (define (vt-cursor-style t) (%vt-cursor-style t))
98 (define (vt-alt? t) (%vt-alt? t))
99 (define (vt-title t) (%vt-title t))
100 (define (vt-bracketed-paste? t) (%vt-bracketed-paste? t))
101 (define (vt-app-cursor? t) (%vt-app-cursor? t))
102 (define (vt-mouse-flags t) (%vt-mouse-flags t))
104 ;; ---- pending replies --------------------------------------------------
105 (define (vt-take-output! t) (%vt-take-output! t))
107 ;; ---- damage -----------------------------------------------------------
108 ;; Drain -> #{ all?: bool rows: (ascending list of dirty row indices) }.
109 ;; rows is '() when all? is #t (the renderer repaints everything).
110 (define (vt-take-damage! t)
111 (let* ((v (%vt-take-damage! t))
112 (all? (vector-ref v 0)))
113 (if all?
114 #{ all?: #t rows: '() }
115 (let loop ((i (- (vector-length v) 1)) (acc '()))
116 (if (< i 1)
117 #{ all?: #f rows: acc }
118 (loop (- i 1) (cons (vector-ref v i) acc)))))))
119 (define (vt-damaged? t) (%vt-damaged? t))
121 ;; ---- events -----------------------------------------------------------
122 ;; Drain -> a list of #{ type: <'title|'bell|'clipboard> payload: <string|#f> }
123 ;; in arrival order. Title also updates vt-title; OSC-52 clipboard is queued
124 ;; only (never acted on — the consumer decides).
125 (define (vt-take-events! t)
126 (map (lambda (e)
127 #{ type: (vector-ref e 0) payload: (vector-ref e 1) })
128 (%vt-take-events! t)))
130 ;; ---- grid access ------------------------------------------------------
131 (define (vt-row-cells t i) (%vt-row-cells t i))
132 (define (vt-row-text t i) (%vt-row-text t i))
133 (define (vt-scrollback-count t) (%vt-scrollback-count t))
134 (define (vt-scrollback-row t k) (%vt-scrollback-row t k))
136 ;; ---- the render seam --------------------------------------------------
137 ;; Style-merged, right-trimmed runs for a visible row (or a scrollback row).
138 ;; cursor-col (optional) forces a run break at that cell. Each run is
139 ;; #(text attr fg bg cursor?).
140 (define (vt-row-runs t i . rest)
141 (if (pair? rest) (%vt-row-runs t i (car rest)) (%vt-row-runs t i #f)))
142 (define (vt-scrollback-runs t k . rest)
143 (if (pair? rest) (%vt-scrollback-runs t k (car rest)) (%vt-scrollback-runs t k #f)))
145 ;; ---- palette ----------------------------------------------------------
146 (define (vt-color-256->rgb i) (%vt-color-256->rgb i))))