@gjermundgaraba/clankermux 0.4.1 → 0.6.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.
@@ -1 +1,2 @@
1
+ // Generated from the catalog by `cargo xtask protocol`; do not edit.
1
2
  export const VERSION = 1;
@@ -1,4 +1,5 @@
1
- import { type Frame } from "../engine/index.ts";
1
+ import { type Cell, type Frame, type PackedCells } from "../engine/render.ts";
2
+ type Cells = Cell[] | PackedCells;
2
3
  /** The cell grid in CSS pixels and the fonts to draw with. */
3
4
  export interface Metrics {
4
5
  cellWidth: number;
@@ -34,17 +35,17 @@ export declare class CanvasRenderer {
34
35
  private readonly palette;
35
36
  private rows;
36
37
  private cursorRow;
37
- /** The scrollback mark drawn last, in pixels from the top. */
38
- private mark;
38
+ private lastScrollbackMark;
39
39
  private cols;
40
40
  private background;
41
41
  private foreground;
42
42
  constructor(ctx: Context2D, metrics: Metrics, palette: Palette);
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. */
43
+ draw(frame: Frame<Cells>): void;
44
+ private eraseLastMark;
45
+ private redrawVacatedCursorRow;
46
46
  private markOf;
47
47
  private drawRow;
48
48
  private font;
49
49
  private drawCursor;
50
50
  }
51
+ export {};
@@ -1,10 +1,7 @@
1
- import { FLAG, NO_COLOR } from "../engine/index.js";
1
+ import { FLAG, NO_COLOR, PACKED_BG, PACKED_FG, PACKED_FLAGS_MASK, PACKED_STRIDE, PACKED_STYLE, PACKED_UNDERLINE_SHIFT, packedText, } from "../engine/render.js";
2
2
  const hex = (color) => `#${color.toString(16).padStart(6, "0")}`;
3
- /** A cell or cursor colour, or `fallback` when it has none. */
4
3
  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;
4
+ const SCROLLBACK_MARK_WIDTH_PX = 3;
8
5
  /**
9
6
  * Draws frames onto a 2D context: changed rows only, then the cursor, then
10
7
  * the scrollback mark. Rows are cached so the rows the cursor or the mark
@@ -16,8 +13,7 @@ export class CanvasRenderer {
16
13
  palette;
17
14
  rows = new Map();
18
15
  cursorRow;
19
- /** The scrollback mark drawn last, in pixels from the top. */
20
- mark;
16
+ lastScrollbackMark;
21
17
  cols = 0;
22
18
  background = "";
23
19
  foreground = "";
@@ -27,13 +23,12 @@ export class CanvasRenderer {
27
23
  this.palette = palette;
28
24
  }
29
25
  draw(frame) {
30
- // The frame always carries the default colours.
31
26
  this.background = hex(frame.background);
32
27
  this.foreground = hex(frame.foreground);
33
28
  if (frame.full || frame.cols !== this.cols) {
34
29
  this.cols = frame.cols;
35
30
  this.rows.clear();
36
- this.mark = undefined;
31
+ this.lastScrollbackMark = undefined;
37
32
  this.ctx.globalAlpha = 1;
38
33
  this.ctx.fillStyle = this.background;
39
34
  this.ctx.fillRect(0, 0, frame.cols * this.metrics.cellWidth, frame.rows * this.metrics.cellHeight);
@@ -44,28 +39,10 @@ export class CanvasRenderer {
44
39
  this.drawRow(row);
45
40
  drawn.add(row.y);
46
41
  }
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
42
  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
- }
61
- // The cursor's old row is redrawn from the cache when the cursor
62
- // moved or went away; the engine marks no row dirty for either.
43
+ this.eraseLastMark(drawn);
63
44
  const cursor = frame.cursor?.visible ? frame.cursor : undefined;
64
- if (this.cursorRow !== undefined && this.cursorRow !== cursor?.y && !drawn.has(this.cursorRow)) {
65
- const row = this.rows.get(this.cursorRow);
66
- if (row)
67
- this.drawRow(row);
68
- }
45
+ this.redrawVacatedCursorRow(cursor, drawn);
69
46
  this.cursorRow = undefined;
70
47
  if (cursor) {
71
48
  if (!drawn.has(cursor.y)) {
@@ -76,17 +53,36 @@ export class CanvasRenderer {
76
53
  this.drawCursor(cursor);
77
54
  this.cursorRow = cursor.y;
78
55
  }
79
- // Over everything, on rows just drawn.
80
- this.mark = mark;
56
+ this.lastScrollbackMark = mark;
81
57
  if (mark) {
82
58
  this.ctx.globalAlpha = 0.5;
83
59
  this.ctx.fillStyle = this.foreground;
84
- this.ctx.fillRect(this.cols * this.metrics.cellWidth - MARK_WIDTH, mark.top, MARK_WIDTH, mark.height);
60
+ this.ctx.fillRect(this.cols * this.metrics.cellWidth - SCROLLBACK_MARK_WIDTH_PX, mark.top, SCROLLBACK_MARK_WIDTH_PX, mark.height);
85
61
  this.ctx.globalAlpha = 1;
86
62
  }
87
63
  }
88
- /** The scrollback mark for `frame`: the viewport's share of the history
89
- * at the right edge, while the view is scrolled back. */
64
+ eraseLastMark(drawn) {
65
+ const last = this.lastScrollbackMark;
66
+ if (!last)
67
+ return;
68
+ const { cellHeight } = this.metrics;
69
+ const end = Math.ceil((last.top + last.height) / cellHeight);
70
+ for (let y = Math.floor(last.top / cellHeight); y < end; y++) {
71
+ const row = this.rows.get(y);
72
+ if (row && !drawn.has(y)) {
73
+ this.drawRow(row);
74
+ drawn.add(y);
75
+ }
76
+ }
77
+ }
78
+ redrawVacatedCursorRow(cursor, drawn) {
79
+ const vacated = this.cursorRow;
80
+ if (vacated === undefined || vacated === cursor?.y || drawn.has(vacated))
81
+ return;
82
+ const row = this.rows.get(vacated);
83
+ if (row)
84
+ this.drawRow(row);
85
+ }
90
86
  markOf(frame) {
91
87
  const { total, offset, len } = frame.scrollbar;
92
88
  if (offset + len >= total)
@@ -101,33 +97,47 @@ export class CanvasRenderer {
101
97
  this.ctx.globalAlpha = 1;
102
98
  this.ctx.fillStyle = this.background;
103
99
  this.ctx.fillRect(0, y, this.cols * cellWidth, cellHeight);
104
- for (let x = 0; x < row.cells.length; x++) {
105
- const cell = row.cells[x];
106
- if (cell.flags & FLAG.SPACER)
100
+ const cells = row.cells;
101
+ const count = Array.isArray(cells) ? cells.length : cells.data.length / PACKED_STRIDE;
102
+ for (let x = 0; x < count; x++) {
103
+ let text, foreground, background, flags, underline;
104
+ if (Array.isArray(cells)) {
105
+ ({ text, fg: foreground, bg: background, flags, underline } = cells[x]);
106
+ }
107
+ else {
108
+ const at = x * PACKED_STRIDE;
109
+ text = packedText(cells, x);
110
+ const style = cells.data[at + PACKED_STYLE];
111
+ foreground = cells.data[at + PACKED_FG];
112
+ background = cells.data[at + PACKED_BG];
113
+ flags = style & PACKED_FLAGS_MASK;
114
+ underline = style >>> PACKED_UNDERLINE_SHIFT;
115
+ }
116
+ if (flags & FLAG.SPACER)
107
117
  continue;
108
- const width = cell.flags & FLAG.WIDE ? 2 : 1;
109
- let fg = css(cell.fg, this.foreground);
110
- let bg = css(cell.bg, this.background);
111
- // A selected cell is drawn inverted, an inverse one the other way.
118
+ const width = flags & FLAG.WIDE ? 2 : 1;
119
+ let fg = css(foreground, this.foreground);
120
+ let bg = css(background, this.background);
112
121
  const selected = row.selection !== undefined && x >= row.selection[0] && x <= row.selection[1];
113
- if (((cell.flags & FLAG.INVERSE) !== 0) !== selected)
122
+ const inverted = ((flags & FLAG.INVERSE) !== 0) !== selected;
123
+ if (inverted)
114
124
  [fg, bg] = [bg, fg];
115
125
  if (bg !== this.background) {
116
126
  this.ctx.fillStyle = bg;
117
127
  this.ctx.fillRect(x * cellWidth, y, width * cellWidth, cellHeight);
118
128
  }
119
- if (cell.flags & FLAG.INVISIBLE)
129
+ if (flags & FLAG.INVISIBLE)
120
130
  continue;
121
- this.ctx.globalAlpha = cell.flags & FLAG.FAINT ? 0.6 : 1;
131
+ this.ctx.globalAlpha = flags & FLAG.FAINT ? 0.6 : 1;
122
132
  this.ctx.fillStyle = fg;
123
- if (cell.text !== "" && cell.text !== " ") {
124
- this.ctx.font = this.font(cell.flags);
125
- this.ctx.fillText(cell.text, x * cellWidth, y + baseline);
133
+ if (text !== "" && text !== " ") {
134
+ this.ctx.font = this.font(flags);
135
+ this.ctx.fillText(text, x * cellWidth, y + baseline);
126
136
  }
127
- if (cell.underline !== 0) {
137
+ if (underline !== 0) {
128
138
  this.ctx.fillRect(x * cellWidth, y + cellHeight - 2, width * cellWidth, 1);
129
139
  }
130
- if (cell.flags & FLAG.STRIKETHROUGH) {
140
+ if (flags & FLAG.STRIKETHROUGH) {
131
141
  this.ctx.fillRect(x * cellWidth, y + Math.floor(cellHeight / 2), width * cellWidth, 1);
132
142
  }
133
143
  }
@@ -166,12 +176,21 @@ export class CanvasRenderer {
166
176
  return;
167
177
  default: {
168
178
  this.ctx.fillRect(x, y, cellWidth, cellHeight);
169
- // The cell's text in the background colour, over the block.
170
- const cell = this.rows.get(cursor.y)?.cells[cursor.x];
171
- if (cell && cell.text !== "" && cell.text !== " ") {
179
+ const cells = this.rows.get(cursor.y)?.cells;
180
+ let text = "", flags = 0;
181
+ if (Array.isArray(cells)) {
182
+ const cell = cells[cursor.x];
183
+ if (cell)
184
+ ({ text, flags } = cell);
185
+ }
186
+ else if (cells && cursor.x < cells.data.length / PACKED_STRIDE) {
187
+ text = packedText(cells, cursor.x);
188
+ flags = cells.data[cursor.x * PACKED_STRIDE + PACKED_STYLE] & PACKED_FLAGS_MASK;
189
+ }
190
+ if (text !== "" && text !== " ") {
172
191
  this.ctx.fillStyle = this.background;
173
- this.ctx.font = this.font(cell.flags);
174
- this.ctx.fillText(cell.text, x, y + baseline);
192
+ this.ctx.font = this.font(flags);
193
+ this.ctx.fillText(text, x, y + baseline);
175
194
  }
176
195
  }
177
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjermundgaraba/clankermux",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,10 +22,6 @@
22
22
  "types": "./dist/proto/index.d.ts",
23
23
  "default": "./dist/proto/index.js"
24
24
  },
25
- "./engine": {
26
- "types": "./dist/engine/index.d.ts",
27
- "default": "./dist/engine/index.js"
28
- },
29
25
  "./renderer": {
30
26
  "types": "./dist/renderer/index.d.ts",
31
27
  "default": "./dist/renderer/index.js"