shortcutxl 0.2.12 → 0.2.13

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.
Files changed (110) hide show
  1. package/README.md +26 -26
  2. package/agent-docs/README.md +397 -397
  3. package/agent-docs/docs/compaction.md +390 -390
  4. package/agent-docs/docs/custom-provider.md +580 -580
  5. package/agent-docs/docs/extensions.md +1971 -1971
  6. package/agent-docs/docs/packages.md +209 -209
  7. package/agent-docs/docs/rpc.md +1317 -1317
  8. package/agent-docs/docs/sdk.md +962 -962
  9. package/agent-docs/docs/session.md +412 -412
  10. package/agent-docs/docs/termux.md +127 -127
  11. package/agent-docs/docs/tui.md +887 -887
  12. package/agent-docs/examples/README.md +25 -25
  13. package/agent-docs/examples/extensions/README.md +205 -205
  14. package/agent-docs/examples/extensions/antigravity-image-gen.ts +447 -447
  15. package/agent-docs/examples/extensions/auto-commit-on-exit.ts +49 -49
  16. package/agent-docs/examples/extensions/bash-spawn-hook.ts +30 -30
  17. package/agent-docs/examples/extensions/bookmark.ts +50 -50
  18. package/agent-docs/examples/extensions/built-in-tool-renderer.ts +256 -256
  19. package/agent-docs/examples/extensions/claude-rules.ts +86 -86
  20. package/agent-docs/examples/extensions/commands.ts +75 -75
  21. package/agent-docs/examples/extensions/confirm-destructive.ts +59 -59
  22. package/agent-docs/examples/extensions/custom-compaction.ts +126 -126
  23. package/agent-docs/examples/extensions/custom-footer.ts +63 -63
  24. package/agent-docs/examples/extensions/custom-header.ts +73 -73
  25. package/agent-docs/examples/extensions/custom-provider-anthropic/index.ts +660 -660
  26. package/agent-docs/examples/extensions/custom-provider-gitlab-duo/index.ts +362 -362
  27. package/agent-docs/examples/extensions/custom-provider-gitlab-duo/test.ts +88 -88
  28. package/agent-docs/examples/extensions/custom-provider-qwen-cli/index.ts +349 -349
  29. package/agent-docs/examples/extensions/dirty-repo-guard.ts +56 -56
  30. package/agent-docs/examples/extensions/doom-overlay/doom-component.ts +133 -133
  31. package/agent-docs/examples/extensions/doom-overlay/doom-keys.ts +108 -108
  32. package/agent-docs/examples/extensions/doom-overlay/index.ts +74 -74
  33. package/agent-docs/examples/extensions/dynamic-resources/index.ts +15 -15
  34. package/agent-docs/examples/extensions/dynamic-tools.ts +77 -77
  35. package/agent-docs/examples/extensions/event-bus.ts +43 -43
  36. package/agent-docs/examples/extensions/file-trigger.ts +41 -41
  37. package/agent-docs/examples/extensions/git-checkpoint.ts +53 -53
  38. package/agent-docs/examples/extensions/handoff.ts +155 -155
  39. package/agent-docs/examples/extensions/hello.ts +25 -25
  40. package/agent-docs/examples/extensions/inline-bash.ts +94 -94
  41. package/agent-docs/examples/extensions/input-transform.ts +43 -43
  42. package/agent-docs/examples/extensions/interactive-shell.ts +209 -209
  43. package/agent-docs/examples/extensions/mac-system-theme.ts +47 -47
  44. package/agent-docs/examples/extensions/message-renderer.ts +59 -59
  45. package/agent-docs/examples/extensions/minimal-mode.ts +430 -430
  46. package/agent-docs/examples/extensions/modal-editor.ts +90 -90
  47. package/agent-docs/examples/extensions/model-status.ts +31 -31
  48. package/agent-docs/examples/extensions/notify.ts +55 -55
  49. package/agent-docs/examples/extensions/overlay-qa-tests.ts +936 -936
  50. package/agent-docs/examples/extensions/overlay-test.ts +159 -159
  51. package/agent-docs/examples/extensions/permission-gate.ts +37 -37
  52. package/agent-docs/examples/extensions/pirate.ts +47 -47
  53. package/agent-docs/examples/extensions/plan-mode/index.ts +363 -363
  54. package/agent-docs/examples/extensions/preset.ts +418 -418
  55. package/agent-docs/examples/extensions/protected-paths.ts +30 -30
  56. package/agent-docs/examples/extensions/qna.ts +122 -122
  57. package/agent-docs/examples/extensions/question.ts +278 -278
  58. package/agent-docs/examples/extensions/questionnaire.ts +440 -440
  59. package/agent-docs/examples/extensions/rainbow-editor.ts +90 -90
  60. package/agent-docs/examples/extensions/reload-runtime.ts +37 -37
  61. package/agent-docs/examples/extensions/rpc-demo.ts +124 -124
  62. package/agent-docs/examples/extensions/sandbox/index.ts +324 -324
  63. package/agent-docs/examples/extensions/send-user-message.ts +97 -97
  64. package/agent-docs/examples/extensions/session-name.ts +27 -27
  65. package/agent-docs/examples/extensions/shutdown-command.ts +69 -69
  66. package/agent-docs/examples/extensions/snake.ts +343 -343
  67. package/agent-docs/examples/extensions/space-invaders.ts +566 -566
  68. package/agent-docs/examples/extensions/ssh.ts +233 -233
  69. package/agent-docs/examples/extensions/status-line.ts +40 -40
  70. package/agent-docs/examples/extensions/subagent/agents.ts +130 -130
  71. package/agent-docs/examples/extensions/subagent/index.ts +1068 -1068
  72. package/agent-docs/examples/extensions/summarize.ts +206 -206
  73. package/agent-docs/examples/extensions/system-prompt-header.ts +17 -17
  74. package/agent-docs/examples/extensions/timed-confirm.ts +72 -72
  75. package/agent-docs/examples/extensions/titlebar-spinner.ts +58 -58
  76. package/agent-docs/examples/extensions/todo.ts +314 -314
  77. package/agent-docs/examples/extensions/tool-override.ts +146 -146
  78. package/agent-docs/examples/extensions/tools.ts +145 -145
  79. package/agent-docs/examples/extensions/trigger-compact.ts +40 -40
  80. package/agent-docs/examples/extensions/truncated-tool.ts +194 -194
  81. package/agent-docs/examples/extensions/widget-placement.ts +17 -17
  82. package/agent-docs/examples/extensions/with-deps/index.ts +37 -37
  83. package/agent-docs/examples/rpc-extension-ui.ts +654 -654
  84. package/agent-docs/examples/sdk/01-minimal.ts +22 -22
  85. package/agent-docs/examples/sdk/02-custom-model.ts +48 -48
  86. package/agent-docs/examples/sdk/03-custom-prompt.ts +55 -55
  87. package/agent-docs/examples/sdk/04-skills.ts +53 -53
  88. package/agent-docs/examples/sdk/05-tools.ts +56 -56
  89. package/agent-docs/examples/sdk/06-extensions.ts +88 -88
  90. package/agent-docs/examples/sdk/07-context-files.ts +40 -40
  91. package/agent-docs/examples/sdk/08-prompt-templates.ts +47 -47
  92. package/agent-docs/examples/sdk/09-api-keys-and-oauth.ts +48 -48
  93. package/agent-docs/examples/sdk/10-settings.ts +54 -54
  94. package/agent-docs/examples/sdk/11-sessions.ts +48 -48
  95. package/agent-docs/examples/sdk/12-full-control.ts +82 -82
  96. package/agent-docs/examples/sdk/README.md +144 -144
  97. package/agent-docs/xll-spec.md +110 -110
  98. package/dist/core/auth-storage.js +21 -2
  99. package/package.json +1 -1
  100. package/xll/ShortcutXL.xll +0 -0
  101. package/xll/modules/debug_render.py +272 -272
  102. package/xll/modules/gameboy.py +241 -241
  103. package/xll/modules/pong.py +188 -188
  104. package/xll/modules/shortcut_xl/_diff_highlight.py +176 -0
  105. package/xll/modules/shortcut_xl/_log.py +12 -12
  106. package/xll/modules/shortcut_xl/_registry.py +44 -44
  107. package/xll/modules/stocks.py +100 -100
  108. /package/skills/{com-advanced-api → COM-advanced-api}/SKILL.md +0 -0
  109. /package/skills/{com-advanced-api → COM-advanced-api}/excel-type-library.py +0 -0
  110. /package/skills/{com-advanced-api → COM-advanced-api}/office-type-library.py +0 -0
@@ -1,343 +1,343 @@
1
- /**
2
- * Snake game extension - play snake with /snake command
3
- */
4
-
5
- import type { ExtensionAPI } from 'shortcutxl';
6
- import { matchesKey, visibleWidth } from 'shortcutxl';
7
-
8
- const GAME_WIDTH = 40;
9
- const GAME_HEIGHT = 15;
10
- const TICK_MS = 100;
11
-
12
- type Direction = 'up' | 'down' | 'left' | 'right';
13
- type Point = { x: number; y: number };
14
-
15
- interface GameState {
16
- snake: Point[];
17
- food: Point;
18
- direction: Direction;
19
- nextDirection: Direction;
20
- score: number;
21
- gameOver: boolean;
22
- highScore: number;
23
- }
24
-
25
- function createInitialState(): GameState {
26
- const startX = Math.floor(GAME_WIDTH / 2);
27
- const startY = Math.floor(GAME_HEIGHT / 2);
28
- return {
29
- snake: [
30
- { x: startX, y: startY },
31
- { x: startX - 1, y: startY },
32
- { x: startX - 2, y: startY }
33
- ],
34
- food: spawnFood([{ x: startX, y: startY }]),
35
- direction: 'right',
36
- nextDirection: 'right',
37
- score: 0,
38
- gameOver: false,
39
- highScore: 0
40
- };
41
- }
42
-
43
- function spawnFood(snake: Point[]): Point {
44
- let food: Point;
45
- do {
46
- food = {
47
- x: Math.floor(Math.random() * GAME_WIDTH),
48
- y: Math.floor(Math.random() * GAME_HEIGHT)
49
- };
50
- } while (snake.some((s) => s.x === food.x && s.y === food.y));
51
- return food;
52
- }
53
-
54
- class SnakeComponent {
55
- private state: GameState;
56
- private interval: ReturnType<typeof setInterval> | null = null;
57
- private onClose: () => void;
58
- private onSave: (state: GameState | null) => void;
59
- private tui: { requestRender: () => void };
60
- private cachedLines: string[] = [];
61
- private cachedWidth = 0;
62
- private version = 0;
63
- private cachedVersion = -1;
64
- private paused: boolean;
65
-
66
- constructor(
67
- tui: { requestRender: () => void },
68
- onClose: () => void,
69
- onSave: (state: GameState | null) => void,
70
- savedState?: GameState
71
- ) {
72
- this.tui = tui;
73
- if (savedState && !savedState.gameOver) {
74
- // Resume from saved state, start paused
75
- this.state = savedState;
76
- this.paused = true;
77
- } else {
78
- // New game or saved game was over
79
- this.state = createInitialState();
80
- if (savedState) {
81
- this.state.highScore = savedState.highScore;
82
- }
83
- this.paused = false;
84
- this.startGame();
85
- }
86
- this.onClose = onClose;
87
- this.onSave = onSave;
88
- }
89
-
90
- private startGame(): void {
91
- this.interval = setInterval(() => {
92
- if (!this.state.gameOver) {
93
- this.tick();
94
- this.version++;
95
- this.tui.requestRender();
96
- }
97
- }, TICK_MS);
98
- }
99
-
100
- private tick(): void {
101
- // Apply queued direction change
102
- this.state.direction = this.state.nextDirection;
103
-
104
- // Calculate new head position
105
- const head = this.state.snake[0];
106
- let newHead: Point;
107
-
108
- switch (this.state.direction) {
109
- case 'up':
110
- newHead = { x: head.x, y: head.y - 1 };
111
- break;
112
- case 'down':
113
- newHead = { x: head.x, y: head.y + 1 };
114
- break;
115
- case 'left':
116
- newHead = { x: head.x - 1, y: head.y };
117
- break;
118
- case 'right':
119
- newHead = { x: head.x + 1, y: head.y };
120
- break;
121
- }
122
-
123
- // Check wall collision
124
- if (newHead.x < 0 || newHead.x >= GAME_WIDTH || newHead.y < 0 || newHead.y >= GAME_HEIGHT) {
125
- this.state.gameOver = true;
126
- return;
127
- }
128
-
129
- // Check self collision
130
- if (this.state.snake.some((s) => s.x === newHead.x && s.y === newHead.y)) {
131
- this.state.gameOver = true;
132
- return;
133
- }
134
-
135
- // Move snake
136
- this.state.snake.unshift(newHead);
137
-
138
- // Check food collision
139
- if (newHead.x === this.state.food.x && newHead.y === this.state.food.y) {
140
- this.state.score += 10;
141
- if (this.state.score > this.state.highScore) {
142
- this.state.highScore = this.state.score;
143
- }
144
- this.state.food = spawnFood(this.state.snake);
145
- } else {
146
- this.state.snake.pop();
147
- }
148
- }
149
-
150
- handleInput(data: string): void {
151
- // If paused (resuming), wait for any key
152
- if (this.paused) {
153
- if (matchesKey(data, 'escape') || data === 'q' || data === 'Q') {
154
- // Quit without clearing save
155
- this.dispose();
156
- this.onClose();
157
- return;
158
- }
159
- // Any other key resumes
160
- this.paused = false;
161
- this.startGame();
162
- return;
163
- }
164
-
165
- // ESC to pause and save
166
- if (matchesKey(data, 'escape')) {
167
- this.dispose();
168
- this.onSave(this.state);
169
- this.onClose();
170
- return;
171
- }
172
-
173
- // Q to quit without saving (clears saved state)
174
- if (data === 'q' || data === 'Q') {
175
- this.dispose();
176
- this.onSave(null); // Clear saved state
177
- this.onClose();
178
- return;
179
- }
180
-
181
- // Arrow keys or WASD
182
- if (matchesKey(data, 'up') || data === 'w' || data === 'W') {
183
- if (this.state.direction !== 'down') this.state.nextDirection = 'up';
184
- } else if (matchesKey(data, 'down') || data === 's' || data === 'S') {
185
- if (this.state.direction !== 'up') this.state.nextDirection = 'down';
186
- } else if (matchesKey(data, 'right') || data === 'd' || data === 'D') {
187
- if (this.state.direction !== 'left') this.state.nextDirection = 'right';
188
- } else if (matchesKey(data, 'left') || data === 'a' || data === 'A') {
189
- if (this.state.direction !== 'right') this.state.nextDirection = 'left';
190
- }
191
-
192
- // Restart on game over
193
- if (this.state.gameOver && (data === 'r' || data === 'R' || data === ' ')) {
194
- const highScore = this.state.highScore;
195
- this.state = createInitialState();
196
- this.state.highScore = highScore;
197
- this.onSave(null); // Clear saved state on restart
198
- this.version++;
199
- this.tui.requestRender();
200
- }
201
- }
202
-
203
- invalidate(): void {
204
- this.cachedWidth = 0;
205
- }
206
-
207
- render(width: number): string[] {
208
- if (width === this.cachedWidth && this.cachedVersion === this.version) {
209
- return this.cachedLines;
210
- }
211
-
212
- const lines: string[] = [];
213
-
214
- // Each game cell is 2 chars wide to appear square (terminal cells are ~2:1 aspect)
215
- const cellWidth = 2;
216
- const effectiveWidth = Math.min(GAME_WIDTH, Math.floor((width - 4) / cellWidth));
217
- const effectiveHeight = GAME_HEIGHT;
218
-
219
- // Colors
220
- const dim = (s: string) => `\x1b[2m${s}\x1b[22m`;
221
- const green = (s: string) => `\x1b[32m${s}\x1b[0m`;
222
- const red = (s: string) => `\x1b[31m${s}\x1b[0m`;
223
- const yellow = (s: string) => `\x1b[33m${s}\x1b[0m`;
224
- const bold = (s: string) => `\x1b[1m${s}\x1b[22m`;
225
-
226
- const boxWidth = effectiveWidth * cellWidth;
227
-
228
- // Helper to pad content inside box
229
- const boxLine = (content: string) => {
230
- const contentLen = visibleWidth(content);
231
- const padding = Math.max(0, boxWidth - contentLen);
232
- return dim(' │') + content + ' '.repeat(padding) + dim('│');
233
- };
234
-
235
- // Top border
236
- lines.push(this.padLine(dim(` ╭${'─'.repeat(boxWidth)}╮`), width));
237
-
238
- // Header with score
239
- const scoreText = `Score: ${bold(yellow(String(this.state.score)))}`;
240
- const highText = `High: ${bold(yellow(String(this.state.highScore)))}`;
241
- const title = `${bold(green('SNAKE'))} │ ${scoreText} │ ${highText}`;
242
- lines.push(this.padLine(boxLine(title), width));
243
-
244
- // Separator
245
- lines.push(this.padLine(dim(` ├${'─'.repeat(boxWidth)}┤`), width));
246
-
247
- // Game grid
248
- for (let y = 0; y < effectiveHeight; y++) {
249
- let row = '';
250
- for (let x = 0; x < effectiveWidth; x++) {
251
- const isHead = this.state.snake[0].x === x && this.state.snake[0].y === y;
252
- const isBody = this.state.snake.slice(1).some((s) => s.x === x && s.y === y);
253
- const isFood = this.state.food.x === x && this.state.food.y === y;
254
-
255
- if (isHead) {
256
- row += green('██'); // Snake head (2 chars)
257
- } else if (isBody) {
258
- row += green('▓▓'); // Snake body (2 chars)
259
- } else if (isFood) {
260
- row += red('◆ '); // Food (2 chars)
261
- } else {
262
- row += ' '; // Empty cell (2 spaces)
263
- }
264
- }
265
- lines.push(this.padLine(dim(' │') + row + dim('│'), width));
266
- }
267
-
268
- // Separator
269
- lines.push(this.padLine(dim(` ├${'─'.repeat(boxWidth)}┤`), width));
270
-
271
- // Footer
272
- let footer: string;
273
- if (this.paused) {
274
- footer = `${yellow(bold('PAUSED'))} Press any key to continue, ${bold('Q')} to quit`;
275
- } else if (this.state.gameOver) {
276
- footer = `${red(bold('GAME OVER!'))} Press ${bold('R')} to restart, ${bold('Q')} to quit`;
277
- } else {
278
- footer = `↑↓←→ or WASD to move, ${bold('ESC')} pause, ${bold('Q')} quit`;
279
- }
280
- lines.push(this.padLine(boxLine(footer), width));
281
-
282
- // Bottom border
283
- lines.push(this.padLine(dim(` ╰${'─'.repeat(boxWidth)}╯`), width));
284
-
285
- this.cachedLines = lines;
286
- this.cachedWidth = width;
287
- this.cachedVersion = this.version;
288
-
289
- return lines;
290
- }
291
-
292
- private padLine(line: string, width: number): string {
293
- // Calculate visible length (strip ANSI codes)
294
- const visibleLen = line.replace(/\x1b\[[0-9;]*m/g, '').length;
295
- const padding = Math.max(0, width - visibleLen);
296
- return line + ' '.repeat(padding);
297
- }
298
-
299
- dispose(): void {
300
- if (this.interval) {
301
- clearInterval(this.interval);
302
- this.interval = null;
303
- }
304
- }
305
- }
306
-
307
- const SNAKE_SAVE_TYPE = 'snake-save';
308
-
309
- export default function (shortcut: ExtensionAPI) {
310
- shortcut.registerCommand('snake', {
311
- description: 'Play Snake!',
312
-
313
- handler: async (_args, ctx) => {
314
- if (!ctx.hasUI) {
315
- ctx.ui.notify('Snake requires interactive mode', 'error');
316
- return;
317
- }
318
-
319
- // Load saved state from session
320
- const entries = ctx.sessionManager.getEntries();
321
- let savedState: GameState | undefined;
322
- for (let i = entries.length - 1; i >= 0; i--) {
323
- const entry = entries[i];
324
- if (entry.type === 'custom' && entry.customType === SNAKE_SAVE_TYPE) {
325
- savedState = entry.data as GameState;
326
- break;
327
- }
328
- }
329
-
330
- await ctx.ui.custom((tui, _theme, _kb, done) => {
331
- return new SnakeComponent(
332
- tui,
333
- () => done(undefined),
334
- (state) => {
335
- // Save or clear state
336
- shortcut.appendEntry(SNAKE_SAVE_TYPE, state);
337
- },
338
- savedState
339
- );
340
- });
341
- }
342
- });
343
- }
1
+ /**
2
+ * Snake game extension - play snake with /snake command
3
+ */
4
+
5
+ import type { ExtensionAPI } from 'shortcutxl';
6
+ import { matchesKey, visibleWidth } from 'shortcutxl';
7
+
8
+ const GAME_WIDTH = 40;
9
+ const GAME_HEIGHT = 15;
10
+ const TICK_MS = 100;
11
+
12
+ type Direction = 'up' | 'down' | 'left' | 'right';
13
+ type Point = { x: number; y: number };
14
+
15
+ interface GameState {
16
+ snake: Point[];
17
+ food: Point;
18
+ direction: Direction;
19
+ nextDirection: Direction;
20
+ score: number;
21
+ gameOver: boolean;
22
+ highScore: number;
23
+ }
24
+
25
+ function createInitialState(): GameState {
26
+ const startX = Math.floor(GAME_WIDTH / 2);
27
+ const startY = Math.floor(GAME_HEIGHT / 2);
28
+ return {
29
+ snake: [
30
+ { x: startX, y: startY },
31
+ { x: startX - 1, y: startY },
32
+ { x: startX - 2, y: startY }
33
+ ],
34
+ food: spawnFood([{ x: startX, y: startY }]),
35
+ direction: 'right',
36
+ nextDirection: 'right',
37
+ score: 0,
38
+ gameOver: false,
39
+ highScore: 0
40
+ };
41
+ }
42
+
43
+ function spawnFood(snake: Point[]): Point {
44
+ let food: Point;
45
+ do {
46
+ food = {
47
+ x: Math.floor(Math.random() * GAME_WIDTH),
48
+ y: Math.floor(Math.random() * GAME_HEIGHT)
49
+ };
50
+ } while (snake.some((s) => s.x === food.x && s.y === food.y));
51
+ return food;
52
+ }
53
+
54
+ class SnakeComponent {
55
+ private state: GameState;
56
+ private interval: ReturnType<typeof setInterval> | null = null;
57
+ private onClose: () => void;
58
+ private onSave: (state: GameState | null) => void;
59
+ private tui: { requestRender: () => void };
60
+ private cachedLines: string[] = [];
61
+ private cachedWidth = 0;
62
+ private version = 0;
63
+ private cachedVersion = -1;
64
+ private paused: boolean;
65
+
66
+ constructor(
67
+ tui: { requestRender: () => void },
68
+ onClose: () => void,
69
+ onSave: (state: GameState | null) => void,
70
+ savedState?: GameState
71
+ ) {
72
+ this.tui = tui;
73
+ if (savedState && !savedState.gameOver) {
74
+ // Resume from saved state, start paused
75
+ this.state = savedState;
76
+ this.paused = true;
77
+ } else {
78
+ // New game or saved game was over
79
+ this.state = createInitialState();
80
+ if (savedState) {
81
+ this.state.highScore = savedState.highScore;
82
+ }
83
+ this.paused = false;
84
+ this.startGame();
85
+ }
86
+ this.onClose = onClose;
87
+ this.onSave = onSave;
88
+ }
89
+
90
+ private startGame(): void {
91
+ this.interval = setInterval(() => {
92
+ if (!this.state.gameOver) {
93
+ this.tick();
94
+ this.version++;
95
+ this.tui.requestRender();
96
+ }
97
+ }, TICK_MS);
98
+ }
99
+
100
+ private tick(): void {
101
+ // Apply queued direction change
102
+ this.state.direction = this.state.nextDirection;
103
+
104
+ // Calculate new head position
105
+ const head = this.state.snake[0];
106
+ let newHead: Point;
107
+
108
+ switch (this.state.direction) {
109
+ case 'up':
110
+ newHead = { x: head.x, y: head.y - 1 };
111
+ break;
112
+ case 'down':
113
+ newHead = { x: head.x, y: head.y + 1 };
114
+ break;
115
+ case 'left':
116
+ newHead = { x: head.x - 1, y: head.y };
117
+ break;
118
+ case 'right':
119
+ newHead = { x: head.x + 1, y: head.y };
120
+ break;
121
+ }
122
+
123
+ // Check wall collision
124
+ if (newHead.x < 0 || newHead.x >= GAME_WIDTH || newHead.y < 0 || newHead.y >= GAME_HEIGHT) {
125
+ this.state.gameOver = true;
126
+ return;
127
+ }
128
+
129
+ // Check self collision
130
+ if (this.state.snake.some((s) => s.x === newHead.x && s.y === newHead.y)) {
131
+ this.state.gameOver = true;
132
+ return;
133
+ }
134
+
135
+ // Move snake
136
+ this.state.snake.unshift(newHead);
137
+
138
+ // Check food collision
139
+ if (newHead.x === this.state.food.x && newHead.y === this.state.food.y) {
140
+ this.state.score += 10;
141
+ if (this.state.score > this.state.highScore) {
142
+ this.state.highScore = this.state.score;
143
+ }
144
+ this.state.food = spawnFood(this.state.snake);
145
+ } else {
146
+ this.state.snake.pop();
147
+ }
148
+ }
149
+
150
+ handleInput(data: string): void {
151
+ // If paused (resuming), wait for any key
152
+ if (this.paused) {
153
+ if (matchesKey(data, 'escape') || data === 'q' || data === 'Q') {
154
+ // Quit without clearing save
155
+ this.dispose();
156
+ this.onClose();
157
+ return;
158
+ }
159
+ // Any other key resumes
160
+ this.paused = false;
161
+ this.startGame();
162
+ return;
163
+ }
164
+
165
+ // ESC to pause and save
166
+ if (matchesKey(data, 'escape')) {
167
+ this.dispose();
168
+ this.onSave(this.state);
169
+ this.onClose();
170
+ return;
171
+ }
172
+
173
+ // Q to quit without saving (clears saved state)
174
+ if (data === 'q' || data === 'Q') {
175
+ this.dispose();
176
+ this.onSave(null); // Clear saved state
177
+ this.onClose();
178
+ return;
179
+ }
180
+
181
+ // Arrow keys or WASD
182
+ if (matchesKey(data, 'up') || data === 'w' || data === 'W') {
183
+ if (this.state.direction !== 'down') this.state.nextDirection = 'up';
184
+ } else if (matchesKey(data, 'down') || data === 's' || data === 'S') {
185
+ if (this.state.direction !== 'up') this.state.nextDirection = 'down';
186
+ } else if (matchesKey(data, 'right') || data === 'd' || data === 'D') {
187
+ if (this.state.direction !== 'left') this.state.nextDirection = 'right';
188
+ } else if (matchesKey(data, 'left') || data === 'a' || data === 'A') {
189
+ if (this.state.direction !== 'right') this.state.nextDirection = 'left';
190
+ }
191
+
192
+ // Restart on game over
193
+ if (this.state.gameOver && (data === 'r' || data === 'R' || data === ' ')) {
194
+ const highScore = this.state.highScore;
195
+ this.state = createInitialState();
196
+ this.state.highScore = highScore;
197
+ this.onSave(null); // Clear saved state on restart
198
+ this.version++;
199
+ this.tui.requestRender();
200
+ }
201
+ }
202
+
203
+ invalidate(): void {
204
+ this.cachedWidth = 0;
205
+ }
206
+
207
+ render(width: number): string[] {
208
+ if (width === this.cachedWidth && this.cachedVersion === this.version) {
209
+ return this.cachedLines;
210
+ }
211
+
212
+ const lines: string[] = [];
213
+
214
+ // Each game cell is 2 chars wide to appear square (terminal cells are ~2:1 aspect)
215
+ const cellWidth = 2;
216
+ const effectiveWidth = Math.min(GAME_WIDTH, Math.floor((width - 4) / cellWidth));
217
+ const effectiveHeight = GAME_HEIGHT;
218
+
219
+ // Colors
220
+ const dim = (s: string) => `\x1b[2m${s}\x1b[22m`;
221
+ const green = (s: string) => `\x1b[32m${s}\x1b[0m`;
222
+ const red = (s: string) => `\x1b[31m${s}\x1b[0m`;
223
+ const yellow = (s: string) => `\x1b[33m${s}\x1b[0m`;
224
+ const bold = (s: string) => `\x1b[1m${s}\x1b[22m`;
225
+
226
+ const boxWidth = effectiveWidth * cellWidth;
227
+
228
+ // Helper to pad content inside box
229
+ const boxLine = (content: string) => {
230
+ const contentLen = visibleWidth(content);
231
+ const padding = Math.max(0, boxWidth - contentLen);
232
+ return dim(' │') + content + ' '.repeat(padding) + dim('│');
233
+ };
234
+
235
+ // Top border
236
+ lines.push(this.padLine(dim(` ╭${'─'.repeat(boxWidth)}╮`), width));
237
+
238
+ // Header with score
239
+ const scoreText = `Score: ${bold(yellow(String(this.state.score)))}`;
240
+ const highText = `High: ${bold(yellow(String(this.state.highScore)))}`;
241
+ const title = `${bold(green('SNAKE'))} │ ${scoreText} │ ${highText}`;
242
+ lines.push(this.padLine(boxLine(title), width));
243
+
244
+ // Separator
245
+ lines.push(this.padLine(dim(` ├${'─'.repeat(boxWidth)}┤`), width));
246
+
247
+ // Game grid
248
+ for (let y = 0; y < effectiveHeight; y++) {
249
+ let row = '';
250
+ for (let x = 0; x < effectiveWidth; x++) {
251
+ const isHead = this.state.snake[0].x === x && this.state.snake[0].y === y;
252
+ const isBody = this.state.snake.slice(1).some((s) => s.x === x && s.y === y);
253
+ const isFood = this.state.food.x === x && this.state.food.y === y;
254
+
255
+ if (isHead) {
256
+ row += green('██'); // Snake head (2 chars)
257
+ } else if (isBody) {
258
+ row += green('▓▓'); // Snake body (2 chars)
259
+ } else if (isFood) {
260
+ row += red('◆ '); // Food (2 chars)
261
+ } else {
262
+ row += ' '; // Empty cell (2 spaces)
263
+ }
264
+ }
265
+ lines.push(this.padLine(dim(' │') + row + dim('│'), width));
266
+ }
267
+
268
+ // Separator
269
+ lines.push(this.padLine(dim(` ├${'─'.repeat(boxWidth)}┤`), width));
270
+
271
+ // Footer
272
+ let footer: string;
273
+ if (this.paused) {
274
+ footer = `${yellow(bold('PAUSED'))} Press any key to continue, ${bold('Q')} to quit`;
275
+ } else if (this.state.gameOver) {
276
+ footer = `${red(bold('GAME OVER!'))} Press ${bold('R')} to restart, ${bold('Q')} to quit`;
277
+ } else {
278
+ footer = `↑↓←→ or WASD to move, ${bold('ESC')} pause, ${bold('Q')} quit`;
279
+ }
280
+ lines.push(this.padLine(boxLine(footer), width));
281
+
282
+ // Bottom border
283
+ lines.push(this.padLine(dim(` ╰${'─'.repeat(boxWidth)}╯`), width));
284
+
285
+ this.cachedLines = lines;
286
+ this.cachedWidth = width;
287
+ this.cachedVersion = this.version;
288
+
289
+ return lines;
290
+ }
291
+
292
+ private padLine(line: string, width: number): string {
293
+ // Calculate visible length (strip ANSI codes)
294
+ const visibleLen = line.replace(/\x1b\[[0-9;]*m/g, '').length;
295
+ const padding = Math.max(0, width - visibleLen);
296
+ return line + ' '.repeat(padding);
297
+ }
298
+
299
+ dispose(): void {
300
+ if (this.interval) {
301
+ clearInterval(this.interval);
302
+ this.interval = null;
303
+ }
304
+ }
305
+ }
306
+
307
+ const SNAKE_SAVE_TYPE = 'snake-save';
308
+
309
+ export default function (shortcut: ExtensionAPI) {
310
+ shortcut.registerCommand('snake', {
311
+ description: 'Play Snake!',
312
+
313
+ handler: async (_args, ctx) => {
314
+ if (!ctx.hasUI) {
315
+ ctx.ui.notify('Snake requires interactive mode', 'error');
316
+ return;
317
+ }
318
+
319
+ // Load saved state from session
320
+ const entries = ctx.sessionManager.getEntries();
321
+ let savedState: GameState | undefined;
322
+ for (let i = entries.length - 1; i >= 0; i--) {
323
+ const entry = entries[i];
324
+ if (entry.type === 'custom' && entry.customType === SNAKE_SAVE_TYPE) {
325
+ savedState = entry.data as GameState;
326
+ break;
327
+ }
328
+ }
329
+
330
+ await ctx.ui.custom((tui, _theme, _kb, done) => {
331
+ return new SnakeComponent(
332
+ tui,
333
+ () => done(undefined),
334
+ (state) => {
335
+ // Save or clear state
336
+ shortcut.appendEntry(SNAKE_SAVE_TYPE, state);
337
+ },
338
+ savedState
339
+ );
340
+ });
341
+ }
342
+ });
343
+ }