Skip to content

Commit a643697

Browse files
committed
Return output after the command has run
1 parent 5eb0d92 commit a643697

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

src/CommandExecutor.ts

+34-31
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,77 @@ import { promisify } from 'node:util';
44
const execPromise = promisify(exec);
55

66
class CommandExecutor {
7-
async executeCommand(command: string): Promise<string> {
7+
async isProcessing(): Promise<boolean> {
88
const ascript = `
99
tell application "iTerm2"
1010
activate
11-
delay 0.1
12-
1311
if windows is equal to {} then
1412
create window with default profile
15-
delay 0.1
1613
end if
1714
1815
tell front window
1916
if current session of current tab is missing value then
2017
create tab with default profile
21-
delay 0.1
2218
end if
2319
2420
tell current session of current tab
25-
write text "${command.replace(/"/g, '\\"')}"
21+
return is processing
2622
end tell
2723
end tell
2824
end tell
2925
`;
30-
31-
const { stdout } = await execPromise(`osascript -e '${ascript}'`);
32-
return stdout;
26+
27+
try {
28+
const { stdout } = await execPromise(`osascript -e '${ascript}'`);
29+
return stdout.trim() === 'true';
30+
} catch (error) {
31+
throw new Error(`Failed to check processing status: ${error}`);
32+
}
3333
}
3434

35-
async createNewTab(): Promise<void> {
35+
async executeCommand(command: string): Promise<string> {
3636
const ascript = `
3737
tell application "iTerm2"
3838
activate
39-
delay 0.1
40-
4139
if windows is equal to {} then
4240
create window with default profile
43-
else
44-
tell front window
41+
end if
42+
43+
tell front window
44+
if current session of current tab is missing value then
4545
create tab with default profile
46+
end if
47+
48+
tell current session of current tab
49+
write text "${command.replace(/"/g, '\\"')}"
4650
end tell
47-
end if
51+
end tell
4852
end tell
4953
`;
50-
51-
await execPromise(`osascript -e '${ascript}'`);
52-
}
5354

54-
async splitPane(vertical: boolean = true): Promise<void> {
55-
const direction = vertical ? "vertical" : "horizontal";
56-
const ascript = `
55+
await execPromise(`osascript -e '${ascript}'`);
56+
57+
// Wait until command completes
58+
while (await this.isProcessing()) {
59+
await new Promise(resolve => setTimeout(resolve, 100));
60+
}
61+
62+
// Get final output, but only the visible rows
63+
const getOutput = `
5764
tell application "iTerm2"
58-
activate
59-
delay 0.1
60-
61-
if windows is equal to {} then
62-
create window with default profile
63-
delay 0.1
64-
end if
65-
6665
tell front window
6766
tell current session of current tab
68-
split ${direction}ly with default profile
67+
set rowCount to number of rows
68+
set contentText to contents
69+
set visibleContent to text 1 thru (rowCount * 200) of contentText
70+
return visibleContent
6971
end tell
7072
end tell
7173
end tell
7274
`;
7375

74-
await execPromise(`osascript -e '${ascript}'`);
76+
const { stdout } = await execPromise(`osascript -e '${getOutput}'`);
77+
return stdout;
7578
}
7679
}
7780

0 commit comments

Comments
 (0)