usecomputer 0.1.1 → 0.1.3
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/CHANGELOG.md +42 -0
- package/README.md +33 -13
- package/bin.sh +49 -0
- package/build.zig +12 -0
- package/dist/bridge-contract.test.js +3 -3
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +0 -60
- package/dist/darwin-arm64/usecomputer +0 -0
- package/dist/darwin-arm64/usecomputer.node +0 -0
- package/dist/darwin-x64/usecomputer +0 -0
- package/dist/darwin-x64/usecomputer.node +0 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -3
- package/package.json +15 -23
- package/src/bridge-contract.test.ts +3 -3
- package/src/index.ts +1 -3
- package/zig/src/kitty-graphics.zig +151 -0
- package/zig/src/lib.zig +123 -6
- package/zig/src/main.zig +667 -47
- package/zig/src/table.zig +170 -0
- package/bin.js +0 -4
- package/dist/linux-x64/usecomputer.node +0 -0
- package/src/cli-parsing.test.ts +0 -61
- package/src/cli.ts +0 -710
- package/src/command-parsers.test.ts +0 -50
- package/src/command-parsers.ts +0 -60
- package/src/debug-point-image.test.ts +0 -50
- package/src/debug-point-image.ts +0 -69
- package/src/terminal-table.test.ts +0 -44
- package/src/terminal-table.ts +0 -88
package/src/cli.ts
DELETED
|
@@ -1,710 +0,0 @@
|
|
|
1
|
-
// usecomputer CLI entrypoint and command wiring for desktop automation actions.
|
|
2
|
-
|
|
3
|
-
import { goke } from 'goke'
|
|
4
|
-
import pc from 'picocolors'
|
|
5
|
-
import { z } from 'zod'
|
|
6
|
-
import dedent from 'string-dedent'
|
|
7
|
-
import { createRequire } from 'node:module'
|
|
8
|
-
import fs from 'node:fs'
|
|
9
|
-
import pathModule from 'node:path'
|
|
10
|
-
import url from 'node:url'
|
|
11
|
-
import { createBridge } from './bridge.js'
|
|
12
|
-
import {
|
|
13
|
-
getRegionFromCoordMap,
|
|
14
|
-
mapPointFromCoordMap,
|
|
15
|
-
mapPointToCoordMap,
|
|
16
|
-
parseCoordMapOrThrow,
|
|
17
|
-
} from './coord-map.js'
|
|
18
|
-
import { parseDirection, parseModifiers, parsePoint, parseRegion } from './command-parsers.js'
|
|
19
|
-
import { drawDebugPointOnImage } from './debug-point-image.js'
|
|
20
|
-
import { renderAlignedTable } from './terminal-table.js'
|
|
21
|
-
import type { DisplayInfo, MouseButton, Point, UseComputerBridge, WindowInfo } from './types.js'
|
|
22
|
-
|
|
23
|
-
const require = createRequire(import.meta.url)
|
|
24
|
-
const packageJson = require('../package.json') as { version: string }
|
|
25
|
-
|
|
26
|
-
function printJson(value: unknown): void {
|
|
27
|
-
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function printLine(value: string): void {
|
|
31
|
-
process.stdout.write(`${value}\n`)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function readTextFromStdin(): string {
|
|
35
|
-
return fs.readFileSync(0, 'utf8')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function parsePositiveInteger({
|
|
39
|
-
value,
|
|
40
|
-
option,
|
|
41
|
-
}: {
|
|
42
|
-
value?: number
|
|
43
|
-
option: string
|
|
44
|
-
}): number | undefined {
|
|
45
|
-
if (typeof value !== 'number') {
|
|
46
|
-
return undefined
|
|
47
|
-
}
|
|
48
|
-
if (!Number.isFinite(value) || value <= 0) {
|
|
49
|
-
throw new Error(`Option ${option} must be a positive number`)
|
|
50
|
-
}
|
|
51
|
-
return Math.round(value)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function splitIntoChunks({
|
|
55
|
-
text,
|
|
56
|
-
chunkSize,
|
|
57
|
-
}: {
|
|
58
|
-
text: string
|
|
59
|
-
chunkSize?: number
|
|
60
|
-
}): string[] {
|
|
61
|
-
if (!chunkSize || text.length <= chunkSize) {
|
|
62
|
-
return [text]
|
|
63
|
-
}
|
|
64
|
-
const chunkCount = Math.ceil(text.length / chunkSize)
|
|
65
|
-
return Array.from({ length: chunkCount }, (_, index) => {
|
|
66
|
-
const start = index * chunkSize
|
|
67
|
-
const end = start + chunkSize
|
|
68
|
-
return text.slice(start, end)
|
|
69
|
-
}).filter((chunk) => {
|
|
70
|
-
return chunk.length > 0
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function sleep({
|
|
75
|
-
ms,
|
|
76
|
-
}: {
|
|
77
|
-
ms: number
|
|
78
|
-
}): Promise<void> {
|
|
79
|
-
return new Promise((resolve) => {
|
|
80
|
-
setTimeout(() => {
|
|
81
|
-
resolve()
|
|
82
|
-
}, ms)
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function parsePointOrThrow(input: string): Point {
|
|
87
|
-
const parsed = parsePoint(input)
|
|
88
|
-
if (parsed instanceof Error) {
|
|
89
|
-
throw parsed
|
|
90
|
-
}
|
|
91
|
-
return parsed
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
function resolveOutputPath({ path }: { path?: string }): string | undefined {
|
|
96
|
-
if (!path) {
|
|
97
|
-
return undefined
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return path.startsWith('/')
|
|
101
|
-
? path
|
|
102
|
-
: `${process.cwd()}/${path}`
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function ensureParentDirectory({ filePath }: { filePath?: string }): void {
|
|
106
|
-
if (!filePath) {
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const parentDirectory = pathModule.dirname(filePath)
|
|
111
|
-
fs.mkdirSync(parentDirectory, { recursive: true })
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function resolvePointInput({
|
|
115
|
-
x,
|
|
116
|
-
y,
|
|
117
|
-
target,
|
|
118
|
-
command,
|
|
119
|
-
}: {
|
|
120
|
-
x?: number
|
|
121
|
-
y?: number
|
|
122
|
-
target?: string
|
|
123
|
-
command: string
|
|
124
|
-
}): Point {
|
|
125
|
-
if (typeof x === 'number' || typeof y === 'number') {
|
|
126
|
-
if (typeof x !== 'number' || typeof y !== 'number') {
|
|
127
|
-
throw new Error(`Command \"${command}\" requires both -x and -y when using coordinate flags`)
|
|
128
|
-
}
|
|
129
|
-
return { x, y }
|
|
130
|
-
}
|
|
131
|
-
if (target) {
|
|
132
|
-
return parsePointOrThrow(target)
|
|
133
|
-
}
|
|
134
|
-
throw new Error(`Command \"${command}\" requires coordinates. Use -x <n> -y <n>`)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function parseButton(input?: string): MouseButton {
|
|
138
|
-
if (input === 'right' || input === 'middle') {
|
|
139
|
-
return input
|
|
140
|
-
}
|
|
141
|
-
return 'left'
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function printDesktopList({ displays }: { displays: DisplayInfo[] }) {
|
|
145
|
-
const rows = displays.map((display) => {
|
|
146
|
-
return {
|
|
147
|
-
desktop: `#${display.index}`,
|
|
148
|
-
primary: display.isPrimary ? pc.green('yes') : 'no',
|
|
149
|
-
size: `${display.width}x${display.height}`,
|
|
150
|
-
position: `${display.x},${display.y}`,
|
|
151
|
-
id: String(display.id),
|
|
152
|
-
scale: String(display.scale),
|
|
153
|
-
name: display.name,
|
|
154
|
-
}
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
const lines = renderAlignedTable({
|
|
158
|
-
rows,
|
|
159
|
-
columns: [
|
|
160
|
-
{ header: pc.bold('desktop'), value: (row) => { return row.desktop } },
|
|
161
|
-
{ header: pc.bold('primary'), value: (row) => { return row.primary } },
|
|
162
|
-
{ header: pc.bold('size'), value: (row) => { return row.size }, align: 'right' },
|
|
163
|
-
{ header: pc.bold('position'), value: (row) => { return row.position }, align: 'right' },
|
|
164
|
-
{ header: pc.bold('id'), value: (row) => { return row.id }, align: 'right' },
|
|
165
|
-
{ header: pc.bold('scale'), value: (row) => { return row.scale }, align: 'right' },
|
|
166
|
-
{ header: pc.bold('name'), value: (row) => { return row.name } },
|
|
167
|
-
],
|
|
168
|
-
})
|
|
169
|
-
lines.forEach((line) => {
|
|
170
|
-
printLine(line)
|
|
171
|
-
})
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function mapWindowsByDesktopIndex({
|
|
175
|
-
windows,
|
|
176
|
-
}: {
|
|
177
|
-
windows: WindowInfo[]
|
|
178
|
-
}): Map<number, WindowInfo[]> {
|
|
179
|
-
return windows.reduce((acc, window) => {
|
|
180
|
-
const list = acc.get(window.desktopIndex) ?? []
|
|
181
|
-
list.push(window)
|
|
182
|
-
acc.set(window.desktopIndex, list)
|
|
183
|
-
return acc
|
|
184
|
-
}, new Map<number, WindowInfo[]>())
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function printDesktopListWithWindows({
|
|
188
|
-
displays,
|
|
189
|
-
windows,
|
|
190
|
-
}: {
|
|
191
|
-
displays: DisplayInfo[]
|
|
192
|
-
windows: WindowInfo[]
|
|
193
|
-
}) {
|
|
194
|
-
const windowsByDesktop = mapWindowsByDesktopIndex({ windows })
|
|
195
|
-
printDesktopList({ displays })
|
|
196
|
-
|
|
197
|
-
displays.forEach((display) => {
|
|
198
|
-
printLine('')
|
|
199
|
-
printLine(pc.bold(pc.cyan(`desktop #${display.index} windows`)))
|
|
200
|
-
|
|
201
|
-
const desktopWindows = windowsByDesktop.get(display.index) ?? []
|
|
202
|
-
if (desktopWindows.length === 0) {
|
|
203
|
-
printLine(pc.dim('none'))
|
|
204
|
-
return
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const lines = renderAlignedTable({
|
|
208
|
-
rows: desktopWindows,
|
|
209
|
-
columns: [
|
|
210
|
-
{ header: pc.bold('id'), value: (row) => { return String(row.id) }, align: 'right' },
|
|
211
|
-
{ header: pc.bold('app'), value: (row) => { return row.ownerName } },
|
|
212
|
-
{ header: pc.bold('pid'), value: (row) => { return String(row.ownerPid) }, align: 'right' },
|
|
213
|
-
{ header: pc.bold('size'), value: (row) => { return `${row.width}x${row.height}` }, align: 'right' },
|
|
214
|
-
{ header: pc.bold('position'), value: (row) => { return `${row.x},${row.y}` }, align: 'right' },
|
|
215
|
-
{ header: pc.bold('title'), value: (row) => { return row.title } },
|
|
216
|
-
],
|
|
217
|
-
})
|
|
218
|
-
lines.forEach((line) => {
|
|
219
|
-
printLine(line)
|
|
220
|
-
})
|
|
221
|
-
})
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function printWindowList({ windows }: { windows: WindowInfo[] }) {
|
|
225
|
-
const lines = renderAlignedTable({
|
|
226
|
-
rows: windows,
|
|
227
|
-
columns: [
|
|
228
|
-
{ header: pc.bold('id'), value: (row) => { return String(row.id) }, align: 'right' },
|
|
229
|
-
{ header: pc.bold('desktop'), value: (row) => { return `#${row.desktopIndex}` }, align: 'right' },
|
|
230
|
-
{ header: pc.bold('app'), value: (row) => { return row.ownerName } },
|
|
231
|
-
{ header: pc.bold('pid'), value: (row) => { return String(row.ownerPid) }, align: 'right' },
|
|
232
|
-
{ header: pc.bold('size'), value: (row) => { return `${row.width}x${row.height}` }, align: 'right' },
|
|
233
|
-
{ header: pc.bold('position'), value: (row) => { return `${row.x},${row.y}` }, align: 'right' },
|
|
234
|
-
{ header: pc.bold('title'), value: (row) => { return row.title } },
|
|
235
|
-
],
|
|
236
|
-
})
|
|
237
|
-
lines.forEach((line) => {
|
|
238
|
-
printLine(line)
|
|
239
|
-
})
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function notImplemented({ command }: { command: string }): never {
|
|
243
|
-
throw new Error(`TODO not implemented: ${command}`)
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function createCli({ bridge = createBridge() }: { bridge?: UseComputerBridge } = {}) {
|
|
247
|
-
const cli = goke('usecomputer')
|
|
248
|
-
|
|
249
|
-
cli
|
|
250
|
-
.command(
|
|
251
|
-
'screenshot [path]',
|
|
252
|
-
dedent`
|
|
253
|
-
Take a screenshot of the entire screen or a region.
|
|
254
|
-
|
|
255
|
-
This command uses a native Zig backend over macOS APIs.
|
|
256
|
-
`,
|
|
257
|
-
)
|
|
258
|
-
.option('-r, --region [region]', z.string().describe('Capture region as x,y,width,height'))
|
|
259
|
-
.option(
|
|
260
|
-
'--display [display]',
|
|
261
|
-
z.number().describe('Display index for multi-monitor setups (0-based: first display is index 0)'),
|
|
262
|
-
)
|
|
263
|
-
.option('--window [window]', z.number().describe('Capture a specific window by window id'))
|
|
264
|
-
.option('--annotate', 'Annotate screenshot with labels')
|
|
265
|
-
.option('--json', 'Output as JSON')
|
|
266
|
-
.action(async (path, options) => {
|
|
267
|
-
const outputPath = resolveOutputPath({ path })
|
|
268
|
-
ensureParentDirectory({ filePath: outputPath })
|
|
269
|
-
const region = options.region ? parseRegion(options.region) : undefined
|
|
270
|
-
if (region instanceof Error) {
|
|
271
|
-
throw region
|
|
272
|
-
}
|
|
273
|
-
if (typeof options.window === 'number' && region) {
|
|
274
|
-
throw new Error('Cannot use --window and --region together')
|
|
275
|
-
}
|
|
276
|
-
if (typeof options.window === 'number' && typeof options.display === 'number') {
|
|
277
|
-
throw new Error('Cannot use --window and --display together')
|
|
278
|
-
}
|
|
279
|
-
const result = await bridge.screenshot({
|
|
280
|
-
path: outputPath,
|
|
281
|
-
region,
|
|
282
|
-
display: options.display,
|
|
283
|
-
window: options.window,
|
|
284
|
-
annotate: options.annotate,
|
|
285
|
-
})
|
|
286
|
-
if (options.json) {
|
|
287
|
-
printJson(result)
|
|
288
|
-
return
|
|
289
|
-
}
|
|
290
|
-
printLine(result.path)
|
|
291
|
-
printLine(result.hint)
|
|
292
|
-
printLine(`desktop-index=${String(result.desktopIndex)}`)
|
|
293
|
-
})
|
|
294
|
-
|
|
295
|
-
cli
|
|
296
|
-
.command(
|
|
297
|
-
'click [target]',
|
|
298
|
-
dedent`
|
|
299
|
-
Click at coordinates.
|
|
300
|
-
|
|
301
|
-
When you are clicking from a screenshot, use the exact pixel coordinates
|
|
302
|
-
of the target in that screenshot image and always pass the exact
|
|
303
|
-
--coord-map value printed by usecomputer screenshot. The coord map
|
|
304
|
-
scales screenshot-space pixels back into the real captured desktop or
|
|
305
|
-
window rectangle before sending the native click.
|
|
306
|
-
`,
|
|
307
|
-
)
|
|
308
|
-
.option('-x [x]', z.number().describe('X coordinate. When using --coord-map, this must be the exact pixel from the screenshot image'))
|
|
309
|
-
.option('-y [y]', z.number().describe('Y coordinate. When using --coord-map, this must be the exact pixel from the screenshot image'))
|
|
310
|
-
.option('--button [button]', z.enum(['left', 'right', 'middle']).default('left').describe('Mouse button'))
|
|
311
|
-
.option('--count [count]', z.number().default(1).describe('Number of clicks'))
|
|
312
|
-
.option('--modifiers [modifiers]', z.string().describe('Modifiers as ctrl,shift,alt,meta'))
|
|
313
|
-
.option('--coord-map [coordMap]', z.string().describe('Map exact screenshot-space pixels back into the real captured desktop or window rectangle'))
|
|
314
|
-
.example('# Click the exact pixel you saw in a screenshot')
|
|
315
|
-
.example('usecomputer click -x 155 -y 446 --coord-map "0,0,1720,1440,1568,1313"')
|
|
316
|
-
.action(async (target, options) => {
|
|
317
|
-
const point = resolvePointInput({
|
|
318
|
-
x: options.x,
|
|
319
|
-
y: options.y,
|
|
320
|
-
target,
|
|
321
|
-
command: 'click',
|
|
322
|
-
})
|
|
323
|
-
const coordMap = parseCoordMapOrThrow(options.coordMap)
|
|
324
|
-
await bridge.click({
|
|
325
|
-
point: mapPointFromCoordMap({ point, coordMap }),
|
|
326
|
-
button: options.button,
|
|
327
|
-
count: options.count,
|
|
328
|
-
modifiers: parseModifiers(options.modifiers),
|
|
329
|
-
})
|
|
330
|
-
})
|
|
331
|
-
|
|
332
|
-
cli
|
|
333
|
-
.command(
|
|
334
|
-
'debug-point [target]',
|
|
335
|
-
dedent`
|
|
336
|
-
Capture a screenshot and draw a red marker where a click would land.
|
|
337
|
-
|
|
338
|
-
Pass the same --coord-map you plan to use for click. This validates
|
|
339
|
-
screenshot-space coordinates before you send a real click. When
|
|
340
|
-
--coord-map is present, debug-point captures that same region so the
|
|
341
|
-
overlay matches the screenshot you are targeting.
|
|
342
|
-
`,
|
|
343
|
-
)
|
|
344
|
-
.option('-x [x]', z.number().describe('X coordinate'))
|
|
345
|
-
.option('-y [y]', z.number().describe('Y coordinate'))
|
|
346
|
-
.option('--coord-map [coordMap]', z.string().describe('Map input coordinates from screenshot space'))
|
|
347
|
-
.option('--output [path]', z.string().describe('Write the annotated screenshot to this path'))
|
|
348
|
-
.option('--json', 'Output as JSON')
|
|
349
|
-
.example('# Validate the same coordinates you plan to click')
|
|
350
|
-
.example('usecomputer debug-point -x 210 -y 560 --coord-map "0,0,1720,1440,1568,1313"')
|
|
351
|
-
.action(async (target, options) => {
|
|
352
|
-
const point = resolvePointInput({
|
|
353
|
-
x: options.x,
|
|
354
|
-
y: options.y,
|
|
355
|
-
target,
|
|
356
|
-
command: 'debug-point',
|
|
357
|
-
})
|
|
358
|
-
const inputCoordMap = parseCoordMapOrThrow(options.coordMap)
|
|
359
|
-
const desktopPoint = mapPointFromCoordMap({ point, coordMap: inputCoordMap })
|
|
360
|
-
const outputPath = resolveOutputPath({ path: options.output ?? './tmp/debug-point.png' })
|
|
361
|
-
ensureParentDirectory({ filePath: outputPath })
|
|
362
|
-
const screenshotRegion = getRegionFromCoordMap({ coordMap: inputCoordMap })
|
|
363
|
-
|
|
364
|
-
const screenshot = await bridge.screenshot({
|
|
365
|
-
path: outputPath,
|
|
366
|
-
region: screenshotRegion,
|
|
367
|
-
})
|
|
368
|
-
const screenshotCoordMap = parseCoordMapOrThrow(screenshot.coordMap)
|
|
369
|
-
const screenshotPoint = mapPointToCoordMap({ point: desktopPoint, coordMap: screenshotCoordMap })
|
|
370
|
-
|
|
371
|
-
await drawDebugPointOnImage({
|
|
372
|
-
imagePath: screenshot.path,
|
|
373
|
-
point: screenshotPoint,
|
|
374
|
-
imageWidth: screenshot.imageWidth,
|
|
375
|
-
imageHeight: screenshot.imageHeight,
|
|
376
|
-
})
|
|
377
|
-
|
|
378
|
-
if (options.json) {
|
|
379
|
-
printJson({
|
|
380
|
-
path: screenshot.path,
|
|
381
|
-
inputPoint: point,
|
|
382
|
-
desktopPoint,
|
|
383
|
-
screenshotPoint,
|
|
384
|
-
inputCoordMap: options.coordMap ?? null,
|
|
385
|
-
screenshotCoordMap: screenshot.coordMap,
|
|
386
|
-
hint: screenshot.hint,
|
|
387
|
-
})
|
|
388
|
-
return
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
printLine(screenshot.path)
|
|
392
|
-
printLine(`input-point=${point.x},${point.y}`)
|
|
393
|
-
printLine(`desktop-point=${desktopPoint.x},${desktopPoint.y}`)
|
|
394
|
-
printLine(`screenshot-point=${screenshotPoint.x},${screenshotPoint.y}`)
|
|
395
|
-
printLine(screenshot.hint)
|
|
396
|
-
})
|
|
397
|
-
|
|
398
|
-
cli
|
|
399
|
-
.command(
|
|
400
|
-
'type [text]',
|
|
401
|
-
dedent`
|
|
402
|
-
Type text in the currently focused input.
|
|
403
|
-
|
|
404
|
-
Supports direct text arguments or --stdin for long/multiline content.
|
|
405
|
-
For very long text, use --chunk-size to split input into multiple native
|
|
406
|
-
type calls so shells and apps are less likely to drop input.
|
|
407
|
-
`,
|
|
408
|
-
)
|
|
409
|
-
.option('--stdin', 'Read text from stdin instead of [text] argument')
|
|
410
|
-
.option('--delay [delay]', z.number().describe('Delay in milliseconds between typed characters'))
|
|
411
|
-
.option('--chunk-size [size]', z.number().describe('Split text into fixed-size chunks before typing'))
|
|
412
|
-
.option('--chunk-delay [delay]', z.number().describe('Delay in milliseconds between chunks'))
|
|
413
|
-
.option('--max-length [length]', z.number().describe('Fail when input text exceeds this maximum length'))
|
|
414
|
-
.example('# Type a short string')
|
|
415
|
-
.example('usecomputer type "hello"')
|
|
416
|
-
.example('# Type multiline text from a file')
|
|
417
|
-
.example('cat ./notes.txt | usecomputer type --stdin --chunk-size 4000 --chunk-delay 15')
|
|
418
|
-
.action(async (text, options) => {
|
|
419
|
-
const fromStdin = Boolean(options.stdin)
|
|
420
|
-
if (fromStdin && text) {
|
|
421
|
-
throw new Error('Use either [text] or --stdin, not both')
|
|
422
|
-
}
|
|
423
|
-
if (!fromStdin && !text) {
|
|
424
|
-
throw new Error('Command "type" requires [text] or --stdin')
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
const sourceText = fromStdin ? readTextFromStdin() : text ?? ''
|
|
428
|
-
const chunkSize = parsePositiveInteger({
|
|
429
|
-
value: options.chunkSize,
|
|
430
|
-
option: '--chunk-size',
|
|
431
|
-
})
|
|
432
|
-
const maxLength = parsePositiveInteger({
|
|
433
|
-
value: options.maxLength,
|
|
434
|
-
option: '--max-length',
|
|
435
|
-
})
|
|
436
|
-
const chunkDelay = parsePositiveInteger({
|
|
437
|
-
value: options.chunkDelay,
|
|
438
|
-
option: '--chunk-delay',
|
|
439
|
-
})
|
|
440
|
-
|
|
441
|
-
if (typeof maxLength === 'number' && sourceText.length > maxLength) {
|
|
442
|
-
throw new Error(`Input text length ${String(sourceText.length)} exceeds --max-length ${String(maxLength)}`)
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
const chunks = splitIntoChunks({
|
|
446
|
-
text: sourceText,
|
|
447
|
-
chunkSize,
|
|
448
|
-
})
|
|
449
|
-
await chunks.reduce(async (previousChunk, chunk, index) => {
|
|
450
|
-
await previousChunk
|
|
451
|
-
await bridge.typeText({
|
|
452
|
-
text: chunk,
|
|
453
|
-
delayMs: options.delay,
|
|
454
|
-
})
|
|
455
|
-
if (typeof chunkDelay === 'number' && index < chunks.length - 1) {
|
|
456
|
-
await sleep({ ms: chunkDelay })
|
|
457
|
-
}
|
|
458
|
-
}, Promise.resolve())
|
|
459
|
-
})
|
|
460
|
-
|
|
461
|
-
cli
|
|
462
|
-
.command(
|
|
463
|
-
'press <key>',
|
|
464
|
-
dedent`
|
|
465
|
-
Press a key or key combo in the focused app.
|
|
466
|
-
|
|
467
|
-
Key combos use plus syntax such as cmd+s or ctrl+shift+p.
|
|
468
|
-
Platform behavior: cmd maps to Command on macOS, Win/Super on
|
|
469
|
-
Windows/Linux. For cross-platform app shortcuts, prefer ctrl+... .
|
|
470
|
-
`,
|
|
471
|
-
)
|
|
472
|
-
.option('--count [count]', z.number().default(1).describe('How many times to press'))
|
|
473
|
-
.option('--delay [delay]', z.number().describe('Delay between presses in milliseconds'))
|
|
474
|
-
.example('# Save in the current app on macOS')
|
|
475
|
-
.example('usecomputer press "cmd+s"')
|
|
476
|
-
.example('# Portable save shortcut across most apps')
|
|
477
|
-
.example('usecomputer press "ctrl+s"')
|
|
478
|
-
.example('# Open command palette in many editors')
|
|
479
|
-
.example('usecomputer press "cmd+shift+p"')
|
|
480
|
-
.action(async (key, options) => {
|
|
481
|
-
await bridge.press({ key, count: options.count, delayMs: options.delay })
|
|
482
|
-
})
|
|
483
|
-
|
|
484
|
-
cli
|
|
485
|
-
.command('scroll <direction> [amount]', 'Scroll in a direction')
|
|
486
|
-
.option('--at [at]', z.string().describe('Coordinates x,y where scroll happens'))
|
|
487
|
-
.action(async (direction, amount, options) => {
|
|
488
|
-
const parsedDirection = parseDirection(direction)
|
|
489
|
-
if (parsedDirection instanceof Error) {
|
|
490
|
-
throw parsedDirection
|
|
491
|
-
}
|
|
492
|
-
const at = options.at ? parsePointOrThrow(options.at) : undefined
|
|
493
|
-
const scrollAmount = amount ? Number(amount) : 300
|
|
494
|
-
if (!Number.isFinite(scrollAmount)) {
|
|
495
|
-
throw new Error(`Invalid amount \"${amount}\"`)
|
|
496
|
-
}
|
|
497
|
-
await bridge.scroll({
|
|
498
|
-
direction: parsedDirection,
|
|
499
|
-
amount: scrollAmount,
|
|
500
|
-
at,
|
|
501
|
-
})
|
|
502
|
-
})
|
|
503
|
-
|
|
504
|
-
cli
|
|
505
|
-
.command('drag <from> <to>', 'Drag from one coordinate to another')
|
|
506
|
-
.option('--duration [duration]', z.number().describe('Duration in milliseconds'))
|
|
507
|
-
.option('--button [button]', z.enum(['left', 'right', 'middle']).default('left').describe('Mouse button'))
|
|
508
|
-
.option('--coord-map [coordMap]', z.string().describe('Map input coordinates from screenshot space'))
|
|
509
|
-
.action(async (from, to, options) => {
|
|
510
|
-
const coordMap = parseCoordMapOrThrow(options.coordMap)
|
|
511
|
-
await bridge.drag({
|
|
512
|
-
from: mapPointFromCoordMap({ point: parsePointOrThrow(from), coordMap }),
|
|
513
|
-
to: mapPointFromCoordMap({ point: parsePointOrThrow(to), coordMap }),
|
|
514
|
-
durationMs: options.duration,
|
|
515
|
-
button: options.button,
|
|
516
|
-
})
|
|
517
|
-
})
|
|
518
|
-
|
|
519
|
-
cli
|
|
520
|
-
.command('hover [target]', 'Move mouse cursor to coordinates without clicking')
|
|
521
|
-
.option('-x [x]', z.number().describe('X coordinate'))
|
|
522
|
-
.option('-y [y]', z.number().describe('Y coordinate'))
|
|
523
|
-
.option('--coord-map [coordMap]', z.string().describe('Map input coordinates from screenshot space'))
|
|
524
|
-
.action(async (target, options) => {
|
|
525
|
-
const point = resolvePointInput({
|
|
526
|
-
x: options.x,
|
|
527
|
-
y: options.y,
|
|
528
|
-
target,
|
|
529
|
-
command: 'hover',
|
|
530
|
-
})
|
|
531
|
-
const coordMap = parseCoordMapOrThrow(options.coordMap)
|
|
532
|
-
await bridge.hover(mapPointFromCoordMap({ point, coordMap }))
|
|
533
|
-
})
|
|
534
|
-
|
|
535
|
-
cli
|
|
536
|
-
.command('mouse move [x] [y]', 'Move mouse cursor to absolute coordinates (optional before click; click can target coordinates directly)')
|
|
537
|
-
.option('-x [x]', z.number().describe('X coordinate'))
|
|
538
|
-
.option('-y [y]', z.number().describe('Y coordinate'))
|
|
539
|
-
.option('--coord-map [coordMap]', z.string().describe('Map input coordinates from screenshot space'))
|
|
540
|
-
.action(async (x, y, options) => {
|
|
541
|
-
const point = resolvePointInput({
|
|
542
|
-
x: options.x,
|
|
543
|
-
y: options.y,
|
|
544
|
-
target: x && y ? `${x},${y}` : undefined,
|
|
545
|
-
command: 'mouse move',
|
|
546
|
-
})
|
|
547
|
-
const coordMap = parseCoordMapOrThrow(options.coordMap)
|
|
548
|
-
await bridge.mouseMove(mapPointFromCoordMap({ point, coordMap }))
|
|
549
|
-
})
|
|
550
|
-
|
|
551
|
-
cli
|
|
552
|
-
.command('mouse down', 'Press and hold mouse button')
|
|
553
|
-
.option('--button [button]', z.enum(['left', 'right', 'middle']).default('left').describe('Mouse button'))
|
|
554
|
-
.action(async (options) => {
|
|
555
|
-
await bridge.mouseDown({ button: parseButton(options.button) })
|
|
556
|
-
})
|
|
557
|
-
|
|
558
|
-
cli
|
|
559
|
-
.command('mouse up', 'Release mouse button')
|
|
560
|
-
.option('--button [button]', z.enum(['left', 'right', 'middle']).default('left').describe('Mouse button'))
|
|
561
|
-
.action(async (options) => {
|
|
562
|
-
await bridge.mouseUp({ button: parseButton(options.button) })
|
|
563
|
-
})
|
|
564
|
-
|
|
565
|
-
cli
|
|
566
|
-
.command('mouse position', 'Print current mouse position as x,y')
|
|
567
|
-
.option('--json', 'Output as JSON')
|
|
568
|
-
.action(async (options) => {
|
|
569
|
-
const position = await bridge.mousePosition()
|
|
570
|
-
if (options.json) {
|
|
571
|
-
printJson(position)
|
|
572
|
-
return
|
|
573
|
-
}
|
|
574
|
-
printLine(`${position.x},${position.y}`)
|
|
575
|
-
})
|
|
576
|
-
|
|
577
|
-
cli
|
|
578
|
-
.command('display list', 'List connected displays')
|
|
579
|
-
.option('--json', 'Output as JSON')
|
|
580
|
-
.action(async (options) => {
|
|
581
|
-
const displays = await bridge.displayList()
|
|
582
|
-
if (options.json) {
|
|
583
|
-
printJson(displays)
|
|
584
|
-
return
|
|
585
|
-
}
|
|
586
|
-
printDesktopList({ displays })
|
|
587
|
-
})
|
|
588
|
-
|
|
589
|
-
cli
|
|
590
|
-
.command('desktop list', 'List desktops as display indexes and sizes (#0 is the primary display)')
|
|
591
|
-
.option('--windows', 'Include available windows grouped by desktop index')
|
|
592
|
-
.option('--json', 'Output as JSON')
|
|
593
|
-
.action(async (options) => {
|
|
594
|
-
const displays = await bridge.displayList()
|
|
595
|
-
const windows = options.windows ? await bridge.windowList() : []
|
|
596
|
-
if (options.json) {
|
|
597
|
-
if (options.windows) {
|
|
598
|
-
printJson({ displays, windows })
|
|
599
|
-
return
|
|
600
|
-
}
|
|
601
|
-
printJson(displays)
|
|
602
|
-
return
|
|
603
|
-
}
|
|
604
|
-
if (options.windows) {
|
|
605
|
-
printDesktopListWithWindows({ displays, windows })
|
|
606
|
-
return
|
|
607
|
-
}
|
|
608
|
-
printDesktopList({ displays })
|
|
609
|
-
})
|
|
610
|
-
|
|
611
|
-
cli
|
|
612
|
-
.command('clipboard get', 'Print clipboard text')
|
|
613
|
-
.action(async () => {
|
|
614
|
-
const text = await bridge.clipboardGet()
|
|
615
|
-
printLine(text)
|
|
616
|
-
})
|
|
617
|
-
|
|
618
|
-
cli
|
|
619
|
-
.command('clipboard set <text>', 'Set clipboard text')
|
|
620
|
-
.action(async (text) => {
|
|
621
|
-
await bridge.clipboardSet({ text })
|
|
622
|
-
})
|
|
623
|
-
|
|
624
|
-
cli.command('snapshot').action(() => {
|
|
625
|
-
notImplemented({ command: 'snapshot' })
|
|
626
|
-
})
|
|
627
|
-
cli.command('get text <target>').action(() => {
|
|
628
|
-
notImplemented({ command: 'get text' })
|
|
629
|
-
})
|
|
630
|
-
cli.command('get title <target>').action(() => {
|
|
631
|
-
notImplemented({ command: 'get title' })
|
|
632
|
-
})
|
|
633
|
-
cli.command('get value <target>').action(() => {
|
|
634
|
-
notImplemented({ command: 'get value' })
|
|
635
|
-
})
|
|
636
|
-
cli.command('get bounds <target>').action(() => {
|
|
637
|
-
notImplemented({ command: 'get bounds' })
|
|
638
|
-
})
|
|
639
|
-
cli.command('get focused').action(() => {
|
|
640
|
-
notImplemented({ command: 'get focused' })
|
|
641
|
-
})
|
|
642
|
-
cli.command('window list').option('--json', 'Output as JSON').action(async (options) => {
|
|
643
|
-
const windows = await bridge.windowList()
|
|
644
|
-
if (options.json) {
|
|
645
|
-
printJson(windows)
|
|
646
|
-
return
|
|
647
|
-
}
|
|
648
|
-
printWindowList({ windows })
|
|
649
|
-
})
|
|
650
|
-
cli.command('window focus <target>').action(() => {
|
|
651
|
-
notImplemented({ command: 'window focus' })
|
|
652
|
-
})
|
|
653
|
-
cli.command('window resize <target> <width> <height>').action(() => {
|
|
654
|
-
notImplemented({ command: 'window resize' })
|
|
655
|
-
})
|
|
656
|
-
cli.command('window move <target> <x> <y>').action(() => {
|
|
657
|
-
notImplemented({ command: 'window move' })
|
|
658
|
-
})
|
|
659
|
-
cli.command('window minimize <target>').action(() => {
|
|
660
|
-
notImplemented({ command: 'window minimize' })
|
|
661
|
-
})
|
|
662
|
-
cli.command('window maximize <target>').action(() => {
|
|
663
|
-
notImplemented({ command: 'window maximize' })
|
|
664
|
-
})
|
|
665
|
-
cli.command('window close <target>').action(() => {
|
|
666
|
-
notImplemented({ command: 'window close' })
|
|
667
|
-
})
|
|
668
|
-
cli.command('app list').action(() => {
|
|
669
|
-
notImplemented({ command: 'app list' })
|
|
670
|
-
})
|
|
671
|
-
cli.command('app launch <name>').action(() => {
|
|
672
|
-
notImplemented({ command: 'app launch' })
|
|
673
|
-
})
|
|
674
|
-
cli.command('app quit <name>').action(() => {
|
|
675
|
-
notImplemented({ command: 'app quit' })
|
|
676
|
-
})
|
|
677
|
-
cli.command('wait <target>').action(() => {
|
|
678
|
-
notImplemented({ command: 'wait' })
|
|
679
|
-
})
|
|
680
|
-
cli.command('find <query>').action(() => {
|
|
681
|
-
notImplemented({ command: 'find' })
|
|
682
|
-
})
|
|
683
|
-
cli.command('diff snapshot').action(() => {
|
|
684
|
-
notImplemented({ command: 'diff snapshot' })
|
|
685
|
-
})
|
|
686
|
-
cli.command('diff screenshot').action(() => {
|
|
687
|
-
notImplemented({ command: 'diff screenshot' })
|
|
688
|
-
})
|
|
689
|
-
|
|
690
|
-
cli.help()
|
|
691
|
-
cli.version(packageJson.version)
|
|
692
|
-
return cli
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
export function runCli(): void {
|
|
696
|
-
const cli = createCli()
|
|
697
|
-
cli.parse()
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
const isDirectEntrypoint = (() => {
|
|
701
|
-
const argvPath = process.argv[1]
|
|
702
|
-
if (!argvPath) {
|
|
703
|
-
return false
|
|
704
|
-
}
|
|
705
|
-
return import.meta.url === url.pathToFileURL(argvPath).href
|
|
706
|
-
})()
|
|
707
|
-
|
|
708
|
-
if (isDirectEntrypoint) {
|
|
709
|
-
runCli()
|
|
710
|
-
}
|