forked from github/copilot-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.js
More file actions
170 lines (143 loc) · 4.77 KB
/
window.js
File metadata and controls
170 lines (143 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* window command - Window management
* @module cli/commands/window
*/
const path = require('path');
const { success, error, info, table, dim, highlight } = require('../util/output');
const UI_MODULE = path.resolve(__dirname, '../../main/ui-automation');
let ui;
function loadUI() {
if (!ui) {
ui = require(UI_MODULE);
}
return ui;
}
/**
* Run the window command
*
* Usage:
* liku window # List all windows
* liku window "Visual Studio" # Focus window by title
* liku window --active # Show active window info
* liku window --front "Notepad" # Bring window to front
* liku window --back "Notepad" # Send window to back
* liku window --minimize "Notepad"
* liku window --restore "Notepad"
*/
async function run(args, options) {
loadUI();
const titleFromArgs = args.length > 0 ? args.join(' ') : null;
const getTarget = (preferredTitle = null) => {
const title = preferredTitle || titleFromArgs || options.title || null;
if (options.hwnd) {
return { hwnd: Number(options.hwnd) };
}
if (title) {
return { title };
}
return null;
};
// Show active window info
if (options.active) {
const win = await ui.getActiveWindow();
if (!win) {
error('Could not get active window');
return { success: false };
}
if (!options.quiet && !options.json) {
const bounds = win.bounds || { x: '?', y: '?', width: '?', height: '?' };
console.log(`
${highlight('Active Window:')}
Title: ${win.title || '(unknown)'}
Process: ${win.processName || '(unknown)'}
Class: ${win.className || '(unknown)'}
Handle: ${win.hwnd}
Position: ${bounds.x}, ${bounds.y}
Size: ${bounds.width} x ${bounds.height}
`);
}
return { success: true, window: win };
}
if (options.front || options.back || options.minimize || options.restore || options.maximize) {
const operation = options.front ? 'front'
: options.back ? 'back'
: options.minimize ? 'minimize'
: options.maximize ? 'maximize'
: 'restore';
const preferredTitle =
typeof options.front === 'string' ? options.front
: typeof options.back === 'string' ? options.back
: typeof options.minimize === 'string' ? options.minimize
: typeof options.maximize === 'string' ? options.maximize
: typeof options.restore === 'string' ? options.restore
: null;
const target = getTarget(preferredTitle);
if (!target) {
error('No target window specified. Pass title text or --hwnd <handle>.');
return { success: false, error: 'No target window specified' };
}
if (!options.quiet) {
info(`Window op: ${operation} (${target.hwnd ? `hwnd=${target.hwnd}` : `title="${target.title}"`})`);
}
let result;
if (operation === 'front') {
result = await ui.bringWindowToFront(target);
} else if (operation === 'back') {
result = await ui.sendWindowToBack(target);
} else if (operation === 'minimize') {
result = await ui.minimizeWindow(target);
} else if (operation === 'maximize') {
result = await ui.maximizeWindow(target);
} else {
result = await ui.restoreWindow(target);
}
if (!result?.success) {
error(`Window operation failed: ${operation}`);
return { success: false, error: `window ${operation} failed`, operation };
}
if (!options.quiet) {
success(`Window operation complete: ${operation}`);
}
return { success: true, operation, target, result };
}
// Focus window by title
if (args.length > 0) {
const title = args.join(' ');
if (!options.quiet) {
info(`Focusing window: "${title}"`);
}
const result = await ui.focusWindow({ title });
if (result.success) {
if (!options.quiet) {
success(`Focused: ${result.window?.title || title}`);
}
return { success: true, window: result.window };
} else {
error(`Window not found: "${title}"`);
return { success: false, error: 'Window not found' };
}
}
// List all windows
if (!options.quiet) {
info('Listing windows...');
}
const windows = await ui.findWindows({});
if (windows.length === 0) {
if (!options.quiet) {
info('No windows found');
}
return { success: true, windows: [], count: 0 };
}
if (!options.quiet && !options.json) {
console.log(`\n${highlight(`Found ${windows.length} windows:`)}\n`);
const rows = windows.map((w, i) => [
i + 1,
w.title?.substring(0, 50) || dim('(untitled)'),
w.processName || '-',
`${w.bounds.width}x${w.bounds.height}`,
]);
table(rows, ['#', 'Title', 'Process', 'Size']);
}
return { success: true, windows, count: windows.length };
}
module.exports = { run };