@@ -7,16 +7,7 @@ class CommandExecutor {
7
7
async isProcessing ( ) : Promise < boolean > {
8
8
const ascript = `
9
9
tell application "iTerm2"
10
- activate
11
- if windows is equal to {} then
12
- create window with default profile
13
- end if
14
-
15
10
tell front window
16
- if current session of current tab is missing value then
17
- create tab with default profile
18
- end if
19
-
20
11
tell current session of current tab
21
12
return is processing
22
13
end tell
@@ -28,53 +19,74 @@ class CommandExecutor {
28
19
const { stdout } = await execPromise ( `osascript -e '${ ascript } '` ) ;
29
20
return stdout . trim ( ) === 'true' ;
30
21
} catch ( error ) {
22
+ console . error ( 'Processing check error:' , error ) ;
31
23
throw new Error ( `Failed to check processing status: ${ error } ` ) ;
32
24
}
33
25
}
34
26
35
27
async executeCommand ( command : string ) : Promise < string > {
36
- const ascript = `
28
+ // First get the current contents
29
+ const getInitialContent = `
37
30
tell application "iTerm2"
38
- activate
39
- if windows is equal to {} then
40
- create window with default profile
41
- end if
42
-
43
31
tell front window
44
- if current session of current tab is missing value then
45
- create tab with default profile
46
- end if
47
-
48
32
tell current session of current tab
49
- write text "${ command . replace ( / " / g, '\\"' ) } "
33
+ set initialContent to contents
34
+ return initialContent
50
35
end tell
51
36
end tell
52
37
end tell
53
38
` ;
54
39
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 = `
40
+ const { stdout : initialContent } = await execPromise ( `osascript -e '${ getInitialContent } '` ) ;
41
+ const initialLength = initialContent . length ;
42
+
43
+ // Execute the command
44
+ const ascript = `
64
45
tell application "iTerm2"
65
46
tell front window
66
47
tell current session of current tab
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
48
+ write text "${ command . replace ( / " / g, '\\"' ) } "
71
49
end tell
72
50
end tell
73
51
end tell
74
52
` ;
75
-
76
- const { stdout } = await execPromise ( `osascript -e '${ getOutput } '` ) ;
77
- return stdout ;
53
+
54
+ try {
55
+ await execPromise ( `osascript -e '${ ascript } '` ) ;
56
+
57
+ // Wait until command completes
58
+ let retryCount = 0 ;
59
+ while ( await this . isProcessing ( ) ) {
60
+ if ( retryCount > 100 ) { // 10 second timeout
61
+ throw new Error ( 'Command execution timed out' ) ;
62
+ }
63
+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
64
+ retryCount ++ ;
65
+ }
66
+
67
+ // Give a small delay for output to settle
68
+ await new Promise ( resolve => setTimeout ( resolve , 200 ) ) ;
69
+
70
+ // Get final content
71
+ const getFinalContent = `
72
+ tell application "iTerm2"
73
+ tell front window
74
+ tell current session of current tab
75
+ return contents
76
+ end tell
77
+ end tell
78
+ end tell
79
+ ` ;
80
+
81
+ const { stdout : finalContent } = await execPromise ( `osascript -e '${ getFinalContent } '` ) ;
82
+
83
+ // Return only the new content
84
+ return finalContent . substring ( initialLength ) . trim ( ) ;
85
+
86
+ } catch ( error ) {
87
+ console . error ( 'Command execution error:' , error ) ;
88
+ throw new Error ( `Failed to execute command: ${ error } ` ) ;
89
+ }
78
90
}
79
91
}
80
92
0 commit comments