@opentui/core 0.5.3 → 0.5.6

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.
package/index.bun.js CHANGED
@@ -44,7 +44,7 @@ import {
44
44
  mergeKeyAliases,
45
45
  mergeKeyBindings,
46
46
  wrapWithDelegates
47
- } from "./chunk-bun-t68f2fmr.js";
47
+ } from "./chunk-bun-da1keqyp.js";
48
48
  import {
49
49
  ASCIIFontSelectionHelper,
50
50
  ATTRIBUTE_BASE_BITS,
@@ -204,7 +204,7 @@ import {
204
204
  visualizeRenderableTree,
205
205
  white,
206
206
  yellow
207
- } from "./chunk-bun-26r5c5w5.js";
207
+ } from "./chunk-bun-9335djz2.js";
208
208
  // src/post/effects.ts
209
209
  function toU8(value) {
210
210
  return Math.round(Math.max(0, Math.min(1, Number.isFinite(value) ? value : 0)) * 255);
@@ -8223,6 +8223,378 @@ class DiffRenderable extends Renderable {
8223
8223
  return offsets;
8224
8224
  }
8225
8225
  }
8226
+ // src/renderables/EmbeddedTerminal.ts
8227
+ var MOD_SHIFT = 1 << 0;
8228
+ var MOD_CTRL = 1 << 1;
8229
+ var MOD_ALT = 1 << 2;
8230
+ var MOD_SUPER = 1 << 3;
8231
+ var MOD_CAPS_LOCK = 1 << 4;
8232
+ var MOD_NUM_LOCK = 1 << 5;
8233
+
8234
+ class EmbeddedTerminalRenderable extends Renderable {
8235
+ selectable = true;
8236
+ lib;
8237
+ handle = null;
8238
+ _onData;
8239
+ _onTerminalResize;
8240
+ _onScreenChange;
8241
+ keyreleaseHandler = null;
8242
+ hadRenderHooks = false;
8243
+ selection = false;
8244
+ constructor(ctx, options) {
8245
+ const cols = options.cols ?? (typeof options.width === "number" ? options.width : 80);
8246
+ const rows = options.rows ?? (typeof options.height === "number" ? options.height : 24);
8247
+ super(ctx, {
8248
+ ...options,
8249
+ width: options.width ?? cols,
8250
+ height: options.height ?? rows,
8251
+ buffered: true
8252
+ });
8253
+ this._focusable = true;
8254
+ this._onData = options.onData;
8255
+ this._onTerminalResize = options.onTerminalResize;
8256
+ this._onScreenChange = options.onScreenChange;
8257
+ this.selectable = options.selectable ?? true;
8258
+ this.lib = resolveRenderLib();
8259
+ try {
8260
+ this.handle = this.lib.createEmbeddedTerminal({ cols, rows, maxScrollback: options.maxScrollback });
8261
+ this.setupMouse(options);
8262
+ } catch (error) {
8263
+ this.destroy();
8264
+ throw error;
8265
+ }
8266
+ }
8267
+ get onData() {
8268
+ return this._onData;
8269
+ }
8270
+ set onData(value) {
8271
+ this._onData = value;
8272
+ }
8273
+ get onTerminalResize() {
8274
+ return this._onTerminalResize;
8275
+ }
8276
+ set onTerminalResize(value) {
8277
+ this._onTerminalResize = value;
8278
+ }
8279
+ get onScreenChange() {
8280
+ return this._onScreenChange;
8281
+ }
8282
+ set onScreenChange(value) {
8283
+ this._onScreenChange = value;
8284
+ }
8285
+ screen() {
8286
+ const cursor = this.handle ? this.lib.embeddedTerminalCursor(this.handle) : { x: 0, y: 0, visible: false, hasValue: false };
8287
+ const lines = this.frameBuffer ? new TextDecoder().decode(this.frameBuffer.getRealCharBytes(true)).split(`
8288
+ `).slice(0, this.height).map((line) => line.trimEnd()) : [];
8289
+ while (lines.at(-1) === "")
8290
+ lines.pop();
8291
+ return {
8292
+ text: lines.join(`
8293
+ `),
8294
+ lines,
8295
+ columns: this.width,
8296
+ rows: this.height,
8297
+ cursor: {
8298
+ x: cursor.x,
8299
+ y: cursor.y,
8300
+ visible: cursor.hasValue && cursor.visible
8301
+ }
8302
+ };
8303
+ }
8304
+ write(data) {
8305
+ if (!this.handle)
8306
+ return;
8307
+ this.lib.embeddedTerminalWrite(this.handle, data);
8308
+ try {
8309
+ this.flushResponses();
8310
+ } finally {
8311
+ this.requestRender();
8312
+ }
8313
+ }
8314
+ invalidate() {
8315
+ if (!this.handle)
8316
+ return;
8317
+ this.lib.embeddedTerminalInvalidate(this.handle);
8318
+ this.requestRender();
8319
+ }
8320
+ encodeKey(key) {
8321
+ if (!this.handle)
8322
+ return new Uint8Array;
8323
+ const text = textualKey(key);
8324
+ return this.lib.embeddedTerminalEncodeKey(this.handle, {
8325
+ action: key.eventType === "release" ? "release" : key.repeated ? "repeat" : "press",
8326
+ key: physicalKey(key),
8327
+ mods: modifiers(key),
8328
+ text,
8329
+ unshiftedCodepoint: key.baseCode ?? physicalUnshiftedCodepoint(key.code)
8330
+ });
8331
+ }
8332
+ encodePaste(bytes) {
8333
+ if (!this.handle)
8334
+ return new Uint8Array;
8335
+ return this.lib.embeddedTerminalEncodePaste(this.handle, bytes);
8336
+ }
8337
+ shouldStartSelection(x, y) {
8338
+ if (!this.selectable)
8339
+ return false;
8340
+ const localX = x - this.x;
8341
+ const localY = y - this.y;
8342
+ return localX >= 0 && localX < this.width && localY >= 0 && localY < this.height;
8343
+ }
8344
+ onSelectionChanged(selection) {
8345
+ if (!this.handle)
8346
+ return false;
8347
+ const local = convertGlobalToLocalSelection(selection, this.x, this.y);
8348
+ if (!local?.isActive) {
8349
+ if (!this.selection)
8350
+ return false;
8351
+ this.lib.embeddedTerminalClearSelection(this.handle);
8352
+ this.selection = false;
8353
+ this.requestRender();
8354
+ return false;
8355
+ }
8356
+ const point = (x, y) => {
8357
+ if (y < 0)
8358
+ return { x: 0, y: 0 };
8359
+ if (y >= this.height)
8360
+ return { x: this.width - 1, y: this.height - 1 };
8361
+ return { x: Math.max(0, Math.min(this.width - 1, x)), y };
8362
+ };
8363
+ this.lib.embeddedTerminalSetSelection(this.handle, point(local.anchorX, local.anchorY), point(local.focusX, local.focusY));
8364
+ this.selection = true;
8365
+ this.requestRender();
8366
+ return true;
8367
+ }
8368
+ hasSelection() {
8369
+ return this.selection;
8370
+ }
8371
+ getSelectedText() {
8372
+ if (!this.handle || !this.selection)
8373
+ return "";
8374
+ return new TextDecoder().decode(this.lib.embeddedTerminalGetSelectedText(this.handle));
8375
+ }
8376
+ focus() {
8377
+ if (this.focused)
8378
+ return;
8379
+ super.focus();
8380
+ if (!this.focused)
8381
+ return;
8382
+ this.keyreleaseHandler = (key) => this.handleKeyPress(key);
8383
+ this.ctx._internalKeyInput.onInternal("keyrelease", this.keyreleaseHandler);
8384
+ try {
8385
+ this.send(this.handle ? this.lib.embeddedTerminalEncodeFocus(this.handle, true) : new Uint8Array, "input");
8386
+ } catch (error) {
8387
+ this.removeKeyreleaseHandler();
8388
+ super.blur();
8389
+ throw error;
8390
+ }
8391
+ }
8392
+ blur() {
8393
+ if (!this.focused)
8394
+ return;
8395
+ try {
8396
+ this.send(this.handle ? this.lib.embeddedTerminalEncodeFocus(this.handle, false) : new Uint8Array, "input");
8397
+ } catch {} finally {
8398
+ this.removeKeyreleaseHandler();
8399
+ super.blur();
8400
+ this._ctx.setCursorPosition(0, 0, false);
8401
+ }
8402
+ }
8403
+ handleKeyPress(key) {
8404
+ const output = this.encodeKey(key);
8405
+ this.send(output, "input");
8406
+ return output.byteLength > 0;
8407
+ }
8408
+ handlePaste(event) {
8409
+ this.send(this.encodePaste(event.bytes), "input");
8410
+ }
8411
+ render(buffer, deltaTime) {
8412
+ const hasRenderHooks = Boolean(this.renderBefore || this.renderAfter);
8413
+ if (this.handle && (hasRenderHooks || this.hadRenderHooks))
8414
+ this.lib.embeddedTerminalInvalidate(this.handle);
8415
+ this.hadRenderHooks = hasRenderHooks;
8416
+ super.render(buffer, deltaTime);
8417
+ }
8418
+ onResize(width, height) {
8419
+ super.onResize(width, height);
8420
+ if (!this.handle || !Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0)
8421
+ return;
8422
+ const cols = Math.min(Math.floor(width), 65535);
8423
+ const rows = Math.min(Math.floor(height), 65535);
8424
+ this.lib.embeddedTerminalResize(this.handle, cols, rows);
8425
+ this.lib.embeddedTerminalInvalidate(this.handle);
8426
+ this.flushResponses();
8427
+ this._onTerminalResize?.(cols, rows);
8428
+ }
8429
+ renderSelf(buffer) {
8430
+ if (!this.handle || !this.frameBuffer || !this.visible || this.isDestroyed)
8431
+ return;
8432
+ this.lib.embeddedTerminalCompose(this.handle, buffer.ptr, 0, 0);
8433
+ this._onScreenChange?.();
8434
+ if (!this.focused)
8435
+ return;
8436
+ const cursor = this.lib.embeddedTerminalCursor(this.handle);
8437
+ const visible = cursor.visible && cursor.hasValue;
8438
+ const cursorX = cursor.wideTail && cursor.x > 0 ? cursor.x - 1 : cursor.x;
8439
+ this._ctx.setCursorPosition(this._screenX + cursorX + 1, this._screenY + cursor.y + 1, visible);
8440
+ if (!visible)
8441
+ return;
8442
+ this._ctx.setCursorStyle({
8443
+ style: cursor.style === "bar" ? "line" : cursor.style === "underline" ? "underline" : "block",
8444
+ blinking: cursor.blinking
8445
+ });
8446
+ if (cursor.color)
8447
+ this._ctx.setCursorColor(RGBA.fromInts(cursor.color.r, cursor.color.g, cursor.color.b, 255));
8448
+ }
8449
+ destroySelf() {
8450
+ if (this.handle) {
8451
+ this.lib.destroyEmbeddedTerminal(this.handle);
8452
+ this.handle = null;
8453
+ }
8454
+ this._ctx.setCursorPosition(0, 0, false);
8455
+ super.destroySelf();
8456
+ }
8457
+ onRemove() {
8458
+ if (this.focused)
8459
+ this.blur();
8460
+ }
8461
+ setupMouse(options) {
8462
+ const { onMouseDown, onMouseUp, onMouseMove, onMouseDrag, onMouseScroll } = options;
8463
+ this.onMouseDown = (event) => {
8464
+ this.forwardMouse(event, "press");
8465
+ onMouseDown?.call(this, event);
8466
+ };
8467
+ this.onMouseUp = (event) => {
8468
+ this.forwardMouse(event, "release");
8469
+ onMouseUp?.call(this, event);
8470
+ };
8471
+ this.onMouseMove = (event) => {
8472
+ this.forwardMouse(event, "motion");
8473
+ onMouseMove?.call(this, event);
8474
+ };
8475
+ this.onMouseDrag = (event) => {
8476
+ this.forwardMouse(event, "motion");
8477
+ onMouseDrag?.call(this, event);
8478
+ };
8479
+ this.onMouseScroll = (event) => {
8480
+ this.forwardMouse(event, "press");
8481
+ onMouseScroll?.call(this, event);
8482
+ };
8483
+ }
8484
+ forwardMouse(event, action) {
8485
+ if (!this.handle)
8486
+ return;
8487
+ if (event.type === "down" && event.button === 0)
8488
+ this.focus();
8489
+ const output = this.lib.embeddedTerminalEncodeMouse(this.handle, {
8490
+ action,
8491
+ button: event.type === "move" && !event.isDragging ? undefined : mouseButton(event),
8492
+ mods: modifiers(event.modifiers),
8493
+ x: event.x - this._screenX,
8494
+ y: event.y - this._screenY,
8495
+ anyButtonPressed: event.isDragging === true || event.type === "down"
8496
+ });
8497
+ if (event.type === "scroll" && output.byteLength === 0) {
8498
+ const direction = event.scroll?.direction;
8499
+ if (direction !== "up" && direction !== "down")
8500
+ return;
8501
+ this.lib.embeddedTerminalScroll(this.handle, direction === "up" ? -3 : 3);
8502
+ this.requestRender();
8503
+ event.preventDefault();
8504
+ event.stopPropagation();
8505
+ return;
8506
+ }
8507
+ if (output.byteLength === 0)
8508
+ return;
8509
+ event.preventDefault();
8510
+ event.stopPropagation();
8511
+ this.send(output, "input");
8512
+ }
8513
+ flushResponses() {
8514
+ if (!this.handle)
8515
+ return;
8516
+ this.send(this.lib.embeddedTerminalDrainResponses(this.handle), "response");
8517
+ }
8518
+ removeKeyreleaseHandler() {
8519
+ if (!this.keyreleaseHandler)
8520
+ return;
8521
+ this.ctx._internalKeyInput.offInternal("keyrelease", this.keyreleaseHandler);
8522
+ this.keyreleaseHandler = null;
8523
+ }
8524
+ send(data, source) {
8525
+ if (data.byteLength > 0)
8526
+ this._onData?.(data, source);
8527
+ }
8528
+ }
8529
+ function modifiers(input) {
8530
+ let value = 0;
8531
+ if (input.shift)
8532
+ value |= MOD_SHIFT;
8533
+ if (input.ctrl)
8534
+ value |= MOD_CTRL;
8535
+ if (input.alt || input.option)
8536
+ value |= MOD_ALT;
8537
+ if (input.meta || input.super)
8538
+ value |= MOD_SUPER;
8539
+ if (input.capsLock)
8540
+ value |= MOD_CAPS_LOCK;
8541
+ if (input.numLock)
8542
+ value |= MOD_NUM_LOCK;
8543
+ return value;
8544
+ }
8545
+ function physicalKey(key) {
8546
+ if (key.code)
8547
+ return key.code;
8548
+ return {
8549
+ backspace: "Backspace",
8550
+ enter: "Enter",
8551
+ return: "Enter",
8552
+ space: "Space",
8553
+ tab: "Tab",
8554
+ delete: "Delete",
8555
+ end: "End",
8556
+ home: "Home",
8557
+ insert: "Insert",
8558
+ pagedown: "PageDown",
8559
+ pageup: "PageUp",
8560
+ down: "ArrowDown",
8561
+ left: "ArrowLeft",
8562
+ right: "ArrowRight",
8563
+ up: "ArrowUp",
8564
+ escape: "Escape"
8565
+ }[key.name.toLowerCase()] ?? "";
8566
+ }
8567
+ function textualKey(key) {
8568
+ if (key.sequence.length > 0 && !/[\p{Cc}]/u.test(key.sequence))
8569
+ return key.sequence;
8570
+ if (key.name === "space")
8571
+ return " ";
8572
+ if (key.name.length === 0 || /[\p{Cc}]/u.test(key.name))
8573
+ return;
8574
+ if ([...key.name].length === 1 || /[^\x00-\x7f]/.test(key.name))
8575
+ return key.name;
8576
+ }
8577
+ function physicalUnshiftedCodepoint(code) {
8578
+ if (code?.startsWith("Key") && code.length === 4)
8579
+ return code.charCodeAt(3) + 32;
8580
+ if (code?.startsWith("Digit") && code.length === 6)
8581
+ return code.charCodeAt(5);
8582
+ return 0;
8583
+ }
8584
+ function mouseButton(event) {
8585
+ if (event.type === "scroll") {
8586
+ if (event.scroll?.direction === "up")
8587
+ return "four";
8588
+ if (event.scroll?.direction === "down")
8589
+ return "five";
8590
+ if (event.scroll?.direction === "left")
8591
+ return "six";
8592
+ if (event.scroll?.direction === "right")
8593
+ return "seven";
8594
+ return;
8595
+ }
8596
+ return { 0: "left", 1: "middle", 2: "right", 4: "four", 5: "five" }[event.button];
8597
+ }
8226
8598
  // src/renderables/Textarea.ts
8227
8599
  var defaultTextareaKeyBindings = [
8228
8600
  { name: "left", action: "move-left" },
@@ -14831,6 +15203,30 @@ class TabSelectRenderable extends Renderable {
14831
15203
  }
14832
15204
  }
14833
15205
  // src/renderables/TimeToFirstDraw.ts
15206
+ var graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
15207
+ function measureCellWidth(buffer, text) {
15208
+ const encoded = buffer.encodeUnicode(text);
15209
+ if (!encoded)
15210
+ return 0;
15211
+ try {
15212
+ return encoded.data.reduce((width, glyph) => width + glyph.width, 0);
15213
+ } finally {
15214
+ buffer.freeUnicode(encoded);
15215
+ }
15216
+ }
15217
+ function truncateToCellWidth(buffer, text, maxWidth) {
15218
+ let visibleText = "";
15219
+ let visibleWidth = 0;
15220
+ for (const { segment } of graphemeSegmenter.segment(text)) {
15221
+ const segmentWidth = measureCellWidth(buffer, segment);
15222
+ if (visibleWidth + segmentWidth > maxWidth)
15223
+ break;
15224
+ visibleText += segment;
15225
+ visibleWidth += segmentWidth;
15226
+ }
15227
+ return { text: visibleText, width: visibleWidth };
15228
+ }
15229
+
14834
15230
  class TimeToFirstDrawRenderable extends Renderable {
14835
15231
  _runtimeMs = null;
14836
15232
  textColor;
@@ -14883,9 +15279,9 @@ class TimeToFirstDrawRenderable extends Renderable {
14883
15279
  }
14884
15280
  const content = `${this.label}: ${this._runtimeMs.toFixed(this.precision)}ms`;
14885
15281
  const maxWidth = Math.max(this.width, 1);
14886
- const visibleContent = content.length > maxWidth ? content.slice(0, maxWidth) : content;
14887
- const centeredX = this.x + Math.max(0, Math.floor((maxWidth - visibleContent.length) / 2));
14888
- buffer.drawText(visibleContent, centeredX, this.y, this.textColor);
15282
+ const visibleContent = truncateToCellWidth(buffer, content, maxWidth);
15283
+ const centeredX = this.x + Math.max(0, Math.floor((maxWidth - visibleContent.width) / 2));
15284
+ buffer.drawText(visibleContent.text, centeredX, this.y, this.textColor);
14889
15285
  }
14890
15286
  normalizePrecision(value) {
14891
15287
  if (!Number.isFinite(value)) {
@@ -15129,6 +15525,7 @@ export {
15129
15525
  FrameBuffer,
15130
15526
  FlamesEffect,
15131
15527
  ExtmarksController,
15528
+ EmbeddedTerminalRenderable,
15132
15529
  EditorView,
15133
15530
  EditBufferRenderableEvents,
15134
15531
  EditBufferRenderable,
@@ -15173,5 +15570,5 @@ export {
15173
15570
  ACHROMATOPSIA_MATRIX
15174
15571
  };
15175
15572
 
15176
- //# debugId=7EDEA741834B885264756E2164756E21
15573
+ //# debugId=0F3B0F5C55684C0564756E2164756E21
15177
15574
  //# sourceMappingURL=index.bun.js.map