@gjermundgaraba/clankermux 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ /** Pixels per cell in the gesture's geometry: positions are in cells,
2
+ * scaled to whole numbers. */
3
+ const SCALE = 1000;
4
+ /** Clicks closer together than this, and within a cell of each other,
5
+ * count up to a word (two) and a line (three). */
6
+ const REPEAT_MS = 500;
7
+ /**
8
+ * Mouse selection through the engine's gesture: a press, then drags, then
9
+ * a release. A single press selects nothing; a drag selects from it by
10
+ * cells, a double press a word and a triple press a line, dragging by
11
+ * words or lines. The gesture tracks its anchor in the terminal, so
12
+ * output between press and drag does not move it.
13
+ */
14
+ export class SelectionGesture {
15
+ engine;
16
+ gesture;
17
+ events;
18
+ /** The terminal the gesture tracks its anchor in. */
19
+ term;
20
+ constructor(engine) {
21
+ this.engine = engine;
22
+ this.gesture = engine.construct("ghostty_selection_gesture_new");
23
+ const event = (type) => engine.construct("ghostty_selection_gesture_event_new", engine.enumValue("GhosttySelectionGestureEventType", type));
24
+ this.events = { press: event("PRESS"), drag: event("DRAG"), release: event("RELEASE") };
25
+ const f64 = new Uint8Array(8);
26
+ new DataView(f64.buffer).setFloat64(0, SCALE, true);
27
+ this.set(this.events.press, "REPEAT_DISTANCE", f64);
28
+ this.set(this.events.press, "REPEAT_INTERVAL_NS", u64(REPEAT_MS * 1e6));
29
+ }
30
+ /** Presses at `at` (`time` in ms); the selection to install, or
31
+ * undefined for none (a single press). */
32
+ press(term, at, time) {
33
+ this.follow(term);
34
+ this.set(this.events.press, "REF", term.gridRef(at));
35
+ this.set(this.events.press, "POSITION", position(at));
36
+ this.set(this.events.press, "TIME_NS", u64(time * 1e6));
37
+ return this.apply(term, this.events.press);
38
+ }
39
+ /** Drags to `at`; the selection from the press, or undefined when the
40
+ * pointer has not left the pressed cell. */
41
+ drag(term, at) {
42
+ if (this.term !== term.handle)
43
+ return undefined;
44
+ const geometry = new Uint8Array(16);
45
+ const view = new DataView(geometry.buffer);
46
+ view.setUint32(0, term.cols, true);
47
+ view.setUint32(4, SCALE, true);
48
+ view.setUint32(8, 0, true);
49
+ view.setUint32(12, term.rows * SCALE, true);
50
+ this.set(this.events.drag, "REF", term.gridRef(at));
51
+ this.set(this.events.drag, "POSITION", position(at));
52
+ this.set(this.events.drag, "GEOMETRY", geometry);
53
+ return this.apply(term, this.events.drag);
54
+ }
55
+ release(term, at) {
56
+ if (this.term !== term.handle)
57
+ return;
58
+ this.set(this.events.release, "REF", term.gridRef(at));
59
+ this.apply(term, this.events.release);
60
+ }
61
+ /** Starts afresh, for a terminal freed or about to be: `term` when it
62
+ * is still alive, to release the tracked anchor in it, else none. The
63
+ * caller says so, since a new terminal may have the handle of a freed
64
+ * one. */
65
+ forget(term) {
66
+ if (this.term === undefined)
67
+ return;
68
+ this.engine.call("ghostty_selection_gesture_free", this.gesture, term?.handle ?? 0);
69
+ this.gesture = this.engine.construct("ghostty_selection_gesture_new");
70
+ this.term = undefined;
71
+ }
72
+ /** A gesture is in one terminal. */
73
+ follow(term) {
74
+ if (this.term !== term.handle)
75
+ this.forget();
76
+ this.term = term.handle;
77
+ }
78
+ set(event, option, value) {
79
+ const e = this.engine;
80
+ e.withBytes(value, (ptr) => e.check("ghostty_selection_gesture_event_set", event, e.enumValue("GhosttySelectionGestureEventOption", option), ptr));
81
+ }
82
+ apply(term, event) {
83
+ const e = this.engine;
84
+ const size = e.sizeOf("GhosttySelection");
85
+ return e.scratch(size, (out) => {
86
+ e.memory.fill(0, out, out + size);
87
+ e.view.setUint32(out, size, true);
88
+ const code = e.call("ghostty_selection_gesture_event", this.gesture, term.handle, event, out);
89
+ if (code === e.result.NO_VALUE)
90
+ return undefined;
91
+ if (code !== e.result.SUCCESS)
92
+ throw new Error(`selection gesture: ${code}`);
93
+ return e.memory.slice(out, out + size);
94
+ });
95
+ }
96
+ free() {
97
+ for (const event of Object.values(this.events))
98
+ this.engine.call("ghostty_selection_gesture_event_free", event);
99
+ this.engine.call("ghostty_selection_gesture_free", this.gesture, 0);
100
+ }
101
+ }
102
+ function u64(value) {
103
+ const bytes = new Uint8Array(8);
104
+ new DataView(bytes.buffer).setBigUint64(0, BigInt(Math.round(value)), true);
105
+ return bytes;
106
+ }
107
+ /** A `GhosttySurfacePosition` for `at`, in the gesture's scaled pixels. */
108
+ function position(at) {
109
+ const bytes = new Uint8Array(16);
110
+ const view = new DataView(bytes.buffer);
111
+ view.setFloat64(0, at.x * SCALE, true);
112
+ view.setFloat64(8, at.y * SCALE, true);
113
+ return bytes;
114
+ }
@@ -41,7 +41,7 @@ export interface Reply {
41
41
  id: number;
42
42
  result: unknown;
43
43
  }
44
- export type ErrorCode = "protocol_error" | "version_mismatch" | "invalid_request" | "limit_exceeded" | "no_such_session" | "not_running" | "running" | "signal_failed" | "engine_mismatch" | "channel_in_use" | "read_only" | "input_overflow" | "too_slow" | "internal";
44
+ export type ErrorCode = "protocol_error" | "version_mismatch" | "invalid_request" | "limit_exceeded" | "no_such_session" | "not_running" | "running" | "signal_failed" | "engine_mismatch" | "channel_in_use" | "read_only" | "input_overflow" | "too_slow" | "cancelled" | "internal";
45
45
  export interface ErrorMessage {
46
46
  id?: number;
47
47
  code: ErrorCode;
@@ -110,23 +110,88 @@ export interface SignalParams {
110
110
  session: string;
111
111
  signal: string;
112
112
  }
113
- export interface SetLabelParams {
113
+ export interface LabelParams {
114
114
  session: string;
115
115
  label: string;
116
116
  }
117
- export interface SetSizeParams {
117
+ /** `resize`: a side left out keeps the session's. */
118
+ export interface ResizeParams {
118
119
  session: string;
119
- cols: number;
120
- rows: number;
120
+ cols?: number;
121
+ rows?: number;
121
122
  }
122
123
  export interface ReadParams {
123
124
  session: string;
124
125
  scrollback?: number;
125
126
  }
126
- export interface WriteParams {
127
+ /** `send`: exactly one of `text`, `keys` and `data`. */
128
+ export interface SendParams {
129
+ session: string;
130
+ /** UTF-8, written as typed. */
131
+ text?: string;
132
+ /** Key names, encoded by the session's terminal. */
133
+ keys?: string[];
134
+ /** Base64 bytes. */
135
+ data?: string;
136
+ /** `text` or `data` as a paste, bracketed when mode 2004 is set. */
137
+ paste?: boolean;
138
+ /** A wait armed before the bytes are written. */
139
+ wait?: SendWait;
140
+ }
141
+ export interface SendWait {
142
+ until: Until;
143
+ timeout_ms?: number;
144
+ }
145
+ /** What a wait waits for: one or more facts, the first to hold wins. */
146
+ export interface Until {
147
+ exited?: true;
148
+ /** Exactly one of `text` and `regex`. */
149
+ output?: {
150
+ text?: string;
151
+ regex?: string;
152
+ };
153
+ status?: {
154
+ states: string[];
155
+ record?: string;
156
+ };
157
+ prompt?: true;
158
+ quiet_ms?: number;
159
+ }
160
+ /** A stream cursor: the next byte's offset and the latest marker reflected. */
161
+ export interface StreamCursor {
162
+ offset: number;
163
+ marker: number;
164
+ }
165
+ export interface WaitParams {
127
166
  session: string;
128
- /** Base64. */
129
- data: string;
167
+ until: Until;
168
+ after?: StreamCursor;
169
+ timeout_ms?: number;
170
+ }
171
+ export interface CancelParams {
172
+ id: number;
173
+ }
174
+ export type WaitReason = "exited" | "matched" | "status" | "prompt" | "quiet" | "timeout";
175
+ /** The `wait` result. */
176
+ export interface Waited {
177
+ reason: WaitReason;
178
+ at: StreamCursor;
179
+ exit?: Exit;
180
+ match?: {
181
+ text: string;
182
+ offset: number;
183
+ };
184
+ record?: StatusRecord;
185
+ }
186
+ /** The `send` result. */
187
+ export interface Sent {
188
+ /** The cursor once the PTY took the bytes. */
189
+ at: StreamCursor;
190
+ wait?: Waited;
191
+ }
192
+ /** The `cancel` result. */
193
+ export interface Cancelled {
194
+ cancelled: boolean;
130
195
  }
131
196
  export interface CreateParams {
132
197
  label?: string;
@@ -24,8 +24,9 @@ export interface Context2D {
24
24
  fillText(text: string, x: number, y: number): void;
25
25
  }
26
26
  /**
27
- * Draws frames onto a 2D context: changed rows only, then the cursor.
28
- * Rows are cached so the row the cursor leaves can be redrawn.
27
+ * Draws frames onto a 2D context: changed rows only, then the cursor, then
28
+ * the scrollback mark. Rows are cached so the rows the cursor or the mark
29
+ * leaves can be redrawn.
29
30
  */
30
31
  export declare class CanvasRenderer {
31
32
  private readonly ctx;
@@ -33,11 +34,16 @@ export declare class CanvasRenderer {
33
34
  private readonly palette;
34
35
  private rows;
35
36
  private cursorRow;
37
+ /** The scrollback mark drawn last, in pixels from the top. */
38
+ private mark;
36
39
  private cols;
37
40
  private background;
38
41
  private foreground;
39
42
  constructor(ctx: Context2D, metrics: Metrics, palette: Palette);
40
43
  draw(frame: Frame): void;
44
+ /** The scrollback mark for `frame`: the viewport's share of the history
45
+ * at the right edge, while the view is scrolled back. */
46
+ private markOf;
41
47
  private drawRow;
42
48
  private font;
43
49
  private drawCursor;
@@ -2,9 +2,13 @@ import { FLAG, NO_COLOR } from "../engine/index.js";
2
2
  const hex = (color) => `#${color.toString(16).padStart(6, "0")}`;
3
3
  /** A cell or cursor colour, or `fallback` when it has none. */
4
4
  const css = (color, fallback) => (color === NO_COLOR ? fallback : hex(color));
5
+ /** Width in CSS pixels of the mark at the right edge that shows the view
6
+ * scrolled back, and where it is in the history. */
7
+ const MARK_WIDTH = 3;
5
8
  /**
6
- * Draws frames onto a 2D context: changed rows only, then the cursor.
7
- * Rows are cached so the row the cursor leaves can be redrawn.
9
+ * Draws frames onto a 2D context: changed rows only, then the cursor, then
10
+ * the scrollback mark. Rows are cached so the rows the cursor or the mark
11
+ * leaves can be redrawn.
8
12
  */
9
13
  export class CanvasRenderer {
10
14
  ctx;
@@ -12,6 +16,8 @@ export class CanvasRenderer {
12
16
  palette;
13
17
  rows = new Map();
14
18
  cursorRow;
19
+ /** The scrollback mark drawn last, in pixels from the top. */
20
+ mark;
15
21
  cols = 0;
16
22
  background = "";
17
23
  foreground = "";
@@ -27,34 +33,67 @@ export class CanvasRenderer {
27
33
  if (frame.full || frame.cols !== this.cols) {
28
34
  this.cols = frame.cols;
29
35
  this.rows.clear();
36
+ this.mark = undefined;
30
37
  this.ctx.globalAlpha = 1;
31
38
  this.ctx.fillStyle = this.background;
32
39
  this.ctx.fillRect(0, 0, frame.cols * this.metrics.cellWidth, frame.rows * this.metrics.cellHeight);
33
40
  }
34
41
  const drawn = new Set();
35
42
  for (const row of frame.lines) {
36
- this.rows.set(row.y, row.cells);
43
+ this.rows.set(row.y, row);
37
44
  this.drawRow(row);
38
45
  drawn.add(row.y);
39
46
  }
47
+ // The last mark is erased with the rows under it, every frame: it is
48
+ // translucent, so one drawn over itself would darken.
49
+ const mark = this.markOf(frame);
50
+ if (this.mark) {
51
+ const { cellHeight } = this.metrics;
52
+ const last = Math.ceil((this.mark.top + this.mark.height) / cellHeight);
53
+ for (let y = Math.floor(this.mark.top / cellHeight); y < last; y++) {
54
+ const row = this.rows.get(y);
55
+ if (row && !drawn.has(y)) {
56
+ this.drawRow(row);
57
+ drawn.add(y);
58
+ }
59
+ }
60
+ }
40
61
  // The cursor's old row is redrawn from the cache when the cursor
41
62
  // moved or went away; the engine marks no row dirty for either.
42
63
  const cursor = frame.cursor?.visible ? frame.cursor : undefined;
43
64
  if (this.cursorRow !== undefined && this.cursorRow !== cursor?.y && !drawn.has(this.cursorRow)) {
44
- const cells = this.rows.get(this.cursorRow);
45
- if (cells)
46
- this.drawRow({ y: this.cursorRow, cells });
65
+ const row = this.rows.get(this.cursorRow);
66
+ if (row)
67
+ this.drawRow(row);
47
68
  }
48
69
  this.cursorRow = undefined;
49
- if (!cursor)
50
- return;
51
- if (!drawn.has(cursor.y)) {
52
- const cells = this.rows.get(cursor.y);
53
- if (cells)
54
- this.drawRow({ y: cursor.y, cells });
70
+ if (cursor) {
71
+ if (!drawn.has(cursor.y)) {
72
+ const row = this.rows.get(cursor.y);
73
+ if (row)
74
+ this.drawRow(row);
75
+ }
76
+ this.drawCursor(cursor);
77
+ this.cursorRow = cursor.y;
55
78
  }
56
- this.drawCursor(cursor);
57
- this.cursorRow = cursor.y;
79
+ // Over everything, on rows just drawn.
80
+ this.mark = mark;
81
+ if (mark) {
82
+ this.ctx.globalAlpha = 0.5;
83
+ this.ctx.fillStyle = this.foreground;
84
+ this.ctx.fillRect(this.cols * this.metrics.cellWidth - MARK_WIDTH, mark.top, MARK_WIDTH, mark.height);
85
+ this.ctx.globalAlpha = 1;
86
+ }
87
+ }
88
+ /** The scrollback mark for `frame`: the viewport's share of the history
89
+ * at the right edge, while the view is scrolled back. */
90
+ markOf(frame) {
91
+ const { total, offset, len } = frame.scrollbar;
92
+ if (offset + len >= total)
93
+ return undefined;
94
+ const height = frame.rows * this.metrics.cellHeight;
95
+ const size = Math.max(Math.round((height * len) / total), Math.min(height, this.metrics.cellHeight));
96
+ return { top: Math.min(Math.round((height * offset) / total), height - size), height: size };
58
97
  }
59
98
  drawRow(row) {
60
99
  const { cellWidth, cellHeight, baseline } = this.metrics;
@@ -69,7 +108,9 @@ export class CanvasRenderer {
69
108
  const width = cell.flags & FLAG.WIDE ? 2 : 1;
70
109
  let fg = css(cell.fg, this.foreground);
71
110
  let bg = css(cell.bg, this.background);
72
- if (cell.flags & FLAG.INVERSE)
111
+ // A selected cell is drawn inverted, an inverse one the other way.
112
+ const selected = row.selection !== undefined && x >= row.selection[0] && x <= row.selection[1];
113
+ if (((cell.flags & FLAG.INVERSE) !== 0) !== selected)
73
114
  [fg, bg] = [bg, fg];
74
115
  if (bg !== this.background) {
75
116
  this.ctx.fillStyle = bg;
@@ -126,7 +167,7 @@ export class CanvasRenderer {
126
167
  default: {
127
168
  this.ctx.fillRect(x, y, cellWidth, cellHeight);
128
169
  // The cell's text in the background colour, over the block.
129
- const cell = this.rows.get(cursor.y)?.[cursor.x];
170
+ const cell = this.rows.get(cursor.y)?.cells[cursor.x];
130
171
  if (cell && cell.text !== "" && cell.text !== " ") {
131
172
  this.ctx.fillStyle = this.background;
132
173
  this.ctx.font = this.font(cell.flags);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjermundgaraba/clankermux",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",