mac-helper 0.0.4 → 0.1.0

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/lib/iterm.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare function runInIterm(command: string): Promise<void>;
1
+ export declare function runInIterm(command: string, reuse?: boolean): Promise<void>;
2
2
  export declare const iTerm: {
3
3
  run: typeof runInIterm;
4
4
  runInIterm: typeof runInIterm;
package/lib/iterm.js CHANGED
@@ -2,15 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.iTerm = exports.runInIterm = void 0;
4
4
  const run_1 = require("@jxa/run");
5
- async function runInIterm(command) {
6
- await (0, run_1.run)((command) => {
5
+ async function runInIterm(command, reuse = true) {
6
+ await (0, run_1.run)((command, reuse) => {
7
7
  const app = Application('iTerm');
8
8
  app.includeStandardAdditions = true;
9
9
  app.activate();
10
- const tab = app.createTabWithDefaultProfile(app.currentWindow);
11
- const sess = tab.currentSession;
12
- app.write(sess, { text: command });
13
- }, command);
10
+ let usingSession;
11
+ // reuse when possible
12
+ if (reuse) {
13
+ const session = app.currentWindow().currentSession();
14
+ const isIdle = session.isAtShellPrompt();
15
+ if (isIdle) {
16
+ usingSession = session;
17
+ }
18
+ }
19
+ // create new tab when necessary
20
+ if (!usingSession) {
21
+ const tab = app.createTabWithDefaultProfile(app.currentWindow);
22
+ usingSession = tab.currentSession();
23
+ }
24
+ if (!usingSession) {
25
+ return;
26
+ }
27
+ app.write(usingSession, { text: command });
28
+ }, command, reuse);
14
29
  }
15
30
  exports.runInIterm = runInIterm;
16
31
  exports.iTerm = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mac-helper",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "useful helper for macOS",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/src/iterm.ts CHANGED
@@ -1,16 +1,39 @@
1
1
  import { run } from '@jxa/run'
2
2
  import { iTerm2 } from 'jxa-common-used'
3
3
 
4
- export async function runInIterm(command: string) {
5
- await run((command) => {
6
- const app = Application<iTerm2>('iTerm')
7
- app.includeStandardAdditions = true
8
- app.activate()
4
+ export async function runInIterm(command: string, reuse = true) {
5
+ await run(
6
+ (command, reuse) => {
7
+ const app = Application<iTerm2>('iTerm')
8
+ app.includeStandardAdditions = true
9
+ app.activate()
9
10
 
10
- const tab = app.createTabWithDefaultProfile(app.currentWindow)
11
- const sess = tab.currentSession
12
- app.write(sess, { text: command })
13
- }, command)
11
+ let usingSession: iTerm2.Session | undefined
12
+
13
+ // reuse when possible
14
+ if (reuse) {
15
+ const session: iTerm2.Session = app.currentWindow().currentSession()
16
+ const isIdle = session.isAtShellPrompt()
17
+ if (isIdle) {
18
+ usingSession = session
19
+ }
20
+ }
21
+
22
+ // create new tab when necessary
23
+ if (!usingSession) {
24
+ const tab = app.createTabWithDefaultProfile(app.currentWindow)
25
+ usingSession = tab.currentSession()
26
+ }
27
+
28
+ if (!usingSession) {
29
+ return
30
+ }
31
+
32
+ app.write(usingSession, { text: command })
33
+ },
34
+ command,
35
+ reuse
36
+ )
14
37
  }
15
38
 
16
39
  export const iTerm = {