tauri-pty 0.0.3 → 0.0.5

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/dist/index.es.js CHANGED
@@ -73,6 +73,7 @@ class TauriPty {
73
73
  constructor(file, args, opt) {
74
74
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
75
75
  this._onData = new EventEmitter2();
76
+ this._onExit = new EventEmitter2();
76
77
  args = typeof args === 'string' ? [args] : args !== null && args !== void 0 ? args : [];
77
78
  const invokeArgs = {
78
79
  file, args,
@@ -87,6 +88,7 @@ class TauriPty {
87
88
  flowControlResume: (_j = opt === null || opt === void 0 ? void 0 : opt.flowControlResume) !== null && _j !== void 0 ? _j : null,
88
89
  };
89
90
  invoke('plugin:pty|spawn', invokeArgs).then(pid => {
91
+ this._exitted = false;
90
92
  this.pid = pid;
91
93
  this.readData();
92
94
  });
@@ -95,14 +97,23 @@ class TauriPty {
95
97
  throw new Error("Method not implemented.");
96
98
  }
97
99
  get onData() { return this._onData.event; }
100
+ get onExit() { return this._onExit.event; }
98
101
  resize(columns, rows) {
99
- invoke('plugin:pty|resize', { pid: this.pid, cols: columns, rows });
102
+ this.cols = columns;
103
+ this.rows = rows;
104
+ invoke('plugin:pty|resize', { pid: this.pid, cols: columns, rows }).catch(e => {
105
+ console.error('Resize error: ', e);
106
+ this.errorCheck();
107
+ });
100
108
  }
101
109
  clear() {
102
110
  throw new Error("Method not implemented.");
103
111
  }
104
112
  write(data) {
105
- invoke('plugin:pty|write', { pid: this.pid, data });
113
+ invoke('plugin:pty|write', { pid: this.pid, data }).catch(e => {
114
+ console.error('Writing error: ', e);
115
+ this.errorCheck();
116
+ });
106
117
  }
107
118
  kill(signal) {
108
119
  throw new Error("Method not implemented.");
@@ -115,9 +126,35 @@ class TauriPty {
115
126
  }
116
127
  readData() {
117
128
  return __awaiter(this, void 0, void 0, function* () {
118
- for (;;) {
119
- const data = yield invoke('plugin:pty|read', { pid: this.pid });
120
- this._onData.fire(data);
129
+ try {
130
+ for (;;) {
131
+ const data = yield invoke('plugin:pty|read', { pid: this.pid });
132
+ this._onData.fire(data);
133
+ }
134
+ }
135
+ catch (e) {
136
+ this.errorCheck();
137
+ if (typeof e === 'string' && e.includes('EOF')) {
138
+ return;
139
+ }
140
+ console.error('Reading error: ', e);
141
+ }
142
+ });
143
+ }
144
+ errorCheck() {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ if (this._exitted) {
147
+ return;
148
+ }
149
+ try {
150
+ const exitCode = yield invoke('plugin:pty|exitstatus', { pid: this.pid });
151
+ if (exitCode != null) {
152
+ this._exitted = true;
153
+ this._onExit.fire({ exitCode });
154
+ }
155
+ }
156
+ catch (e) {
157
+ console.error(e);
121
158
  }
122
159
  });
123
160
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.es.js","sources":["../api/eventEmitter2.ts","../api/index.ts"],"sourcesContent":["/**\r\n * Copyright (c) 2019, Microsoft Corporation (MIT License).\r\n */\r\n\r\nimport { IDisposable } from \".\";\r\n\r\ninterface IListener<T> {\r\n (e: T): void;\r\n}\r\n\r\nexport interface IEvent<T> {\r\n (listener: (e: T) => any): IDisposable;\r\n}\r\n\r\nexport class EventEmitter2<T> {\r\n private _listeners: IListener<T>[] = [];\r\n private _event?: IEvent<T>;\r\n\r\n public get event(): IEvent<T> {\r\n if (!this._event) {\r\n this._event = (listener: (e: T) => any) => {\r\n this._listeners.push(listener);\r\n const disposable = {\r\n dispose: () => {\r\n for (let i = 0; i < this._listeners.length; i++) {\r\n if (this._listeners[i] === listener) {\r\n this._listeners.splice(i, 1);\r\n return;\r\n }\r\n }\r\n }\r\n };\r\n return disposable;\r\n };\r\n }\r\n return this._event;\r\n }\r\n\r\n public fire(data: T): void {\r\n const queue: IListener<T>[] = [];\r\n for (let i = 0; i < this._listeners.length; i++) {\r\n queue.push(this._listeners[i]);\r\n }\r\n for (let i = 0; i < queue.length; i++) {\r\n queue[i].call(undefined, data);\r\n }\r\n }\r\n}","/**\r\n * Copyright (c) 2017, Daniel Imms (MIT License).\r\n * Copyright (c) 2018, Microsoft Corporation (MIT License).\r\n * Copyright (c) 2023, Tnze (MIT License).\r\n */\r\nimport { invoke } from \"@tauri-apps/api\"\r\nimport { EventEmitter2 } from \"./eventEmitter2\";\r\n\r\n/**\r\n * Forks a process as a pseudoterminal.\r\n * @param file The file to launch.\r\n * @param args The file's arguments as argv (string[]) or in a pre-escaped CommandLine format\r\n * (string). Note that the CommandLine option is only available on Windows and is expected to be\r\n * escaped properly.\r\n * @param options The options of the terminal.\r\n * @see CommandLineToArgvW https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx\r\n * @see Parsing C++ Comamnd-Line Arguments https://msdn.microsoft.com/en-us/library/17w5ykft.aspx\r\n * @see GetCommandLine https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156.aspx\r\n */\r\nexport function spawn(file: string, args: string[] | string, options: IPtyForkOptions | IWindowsPtyForkOptions): IPty {\r\n return new TauriPty(file, args, options)\r\n}\r\n\r\nexport interface IBasePtyForkOptions {\r\n\r\n /**\r\n * Name of the terminal to be set in environment ($TERM variable).\r\n */\r\n name?: string;\r\n\r\n /**\r\n * Number of intial cols of the pty.\r\n */\r\n cols?: number;\r\n\r\n /**\r\n * Number of initial rows of the pty.\r\n */\r\n rows?: number;\r\n\r\n /**\r\n * Working directory to be set for the child program.\r\n */\r\n cwd?: string;\r\n\r\n /**\r\n * Environment to be set for the child program.\r\n */\r\n env?: { [key: string]: string | undefined };\r\n\r\n /**\r\n * String encoding of the underlying pty.\r\n * If set, incoming data will be decoded to strings and outgoing strings to bytes applying this encoding.\r\n * If unset, incoming data will be delivered as raw bytes (Buffer type).\r\n * By default 'utf8' is assumed, to unset it explicitly set it to `null`.\r\n */\r\n encoding?: string | null;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * Whether to enable flow control handling (false by default). If enabled a message of `flowControlPause`\r\n * will pause the socket and thus blocking the child program execution due to buffer back pressure.\r\n * A message of `flowControlResume` will resume the socket into flow mode.\r\n * For performance reasons only a single message as a whole will match (no message part matching).\r\n * If flow control is enabled the `flowControlPause` and `flowControlResume` messages are not forwarded to\r\n * the underlying pseudoterminal.\r\n */\r\n handleFlowControl?: boolean;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * The string that should pause the pty when `handleFlowControl` is true. Default is XOFF ('\\x13').\r\n */\r\n flowControlPause?: string;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * The string that should resume the pty when `handleFlowControl` is true. Default is XON ('\\x11').\r\n */\r\n flowControlResume?: string;\r\n}\r\n\r\nexport interface IPtyForkOptions extends IBasePtyForkOptions {\r\n /**\r\n * Security warning: use this option with great caution,\r\n * as opened file descriptors with higher privileges might leak to the child program.\r\n */\r\n uid?: number;\r\n gid?: number;\r\n}\r\n\r\nexport interface IWindowsPtyForkOptions extends IBasePtyForkOptions {\r\n /**\r\n * Whether to use the ConPTY system on Windows. When this is not set, ConPTY will be used when\r\n * the Windows build number is >= 18309 (instead of winpty). Note that ConPTY is available from\r\n * build 17134 but is too unstable to enable by default.\r\n *\r\n * This setting does nothing on non-Windows.\r\n */\r\n useConpty?: boolean;\r\n\r\n /**\r\n * Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty.\r\n * @see https://docs.microsoft.com/en-us/windows/console/createpseudoconsole\r\n */\r\n conptyInheritCursor?: boolean;\r\n}\r\n\r\n/**\r\n * An interface representing a pseudoterminal, on Windows this is emulated via the winpty library.\r\n */\r\nexport interface IPty {\r\n /**\r\n * The process ID of the outer process.\r\n */\r\n readonly pid: number;\r\n\r\n /**\r\n * The column size in characters.\r\n */\r\n readonly cols: number;\r\n\r\n /**\r\n * The row size in characters.\r\n */\r\n readonly rows: number;\r\n\r\n /**\r\n * The title of the active process.\r\n */\r\n readonly process: string;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * Whether to handle flow control. Useful to disable/re-enable flow control during runtime.\r\n * Use this for binary data that is likely to contain the `flowControlPause` string by accident.\r\n */\r\n handleFlowControl: boolean;\r\n\r\n /**\r\n * Adds an event listener for when a data event fires. This happens when data is returned from\r\n * the pty.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\n readonly onData: IEvent<string>;\r\n\r\n /**\r\n * Adds an event listener for when an exit event fires. This happens when the pty exits.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\n readonly onExit: IEvent<{ exitCode: number, signal?: number }>;\r\n\r\n /**\r\n * Resizes the dimensions of the pty.\r\n * @param columns The number of columns to use.\r\n * @param rows The number of rows to use.\r\n */\r\n resize(columns: number, rows: number): void;\r\n\r\n /**\r\n * Clears the pty's internal representation of its buffer. This is a no-op\r\n * unless on Windows/ConPTY. This is useful if the buffer is cleared on the\r\n * frontend in order to synchronize state with the backend to avoid ConPTY\r\n * possibly reprinting the screen.\r\n */\r\n clear(): void;\r\n\r\n /**\r\n * Writes data to the pty.\r\n * @param data The data to write.\r\n */\r\n write(data: string): void;\r\n\r\n /**\r\n * Kills the pty.\r\n * @param signal The signal to use, defaults to SIGHUP. This parameter is not supported on\r\n * Windows.\r\n * @throws Will throw when signal is used on Windows.\r\n */\r\n kill(signal?: string): void;\r\n\r\n /**\r\n * Pauses the pty for customizable flow control.\r\n */\r\n pause(): void;\r\n\r\n /**\r\n * Resumes the pty for customizable flow control.\r\n */\r\n resume(): void;\r\n}\r\n\r\n/**\r\n * An object that can be disposed via a dispose function.\r\n */\r\nexport interface IDisposable {\r\n dispose(): void;\r\n}\r\n\r\n/**\r\n * An event that can be listened to.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\nexport interface IEvent<T> {\r\n (listener: (e: T) => any): IDisposable;\r\n}\r\n\r\nexport type ArgvOrCommandLine = string[] | string;\r\n\r\nclass TauriPty implements IPty, IDisposable {\r\n pid: number;\r\n cols: number;\r\n rows: number;\r\n process: string;\r\n handleFlowControl: boolean;\r\n\r\n private _onData = new EventEmitter2<string>();\r\n\r\n constructor(file: string, args?: ArgvOrCommandLine, opt?: IWindowsPtyForkOptions) {\r\n args = typeof args === 'string' ? [args] : args ?? []; // Convert args to string[] anyways.\r\n const invokeArgs = {\r\n file, args,\r\n termName: opt?.name ?? 'Terminal',\r\n cols: opt?.cols ?? null,\r\n rows: opt?.rows ?? null,\r\n cwd: opt?.cwd ?? null,\r\n env: opt?.env ?? {},\r\n encoding: opt?.encoding ?? null,\r\n handleFlowControl: opt?.handleFlowControl ?? null,\r\n flowControlPause: opt?.flowControlPause ?? null,\r\n flowControlResume: opt?.flowControlResume ?? null,\r\n };\r\n invoke<number>('plugin:pty|spawn', invokeArgs).then(pid => {\r\n this.pid = pid;\r\n this.readData()\r\n });\r\n }\r\n dispose(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n public get onData(): IEvent<string> { return this._onData.event; }\r\n onExit: IEvent<{ exitCode: number; signal?: number | undefined; }>;\r\n resize(columns: number, rows: number): void {\r\n invoke('plugin:pty|resize', { pid: this.pid, cols: columns, rows });\r\n }\r\n clear(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n write(data: string): void {\r\n invoke('plugin:pty|write', { pid: this.pid, data });\r\n }\r\n kill(signal?: string | undefined): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n pause(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n resume(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n private async readData() {\r\n for (; ;) {\r\n const data = await invoke<string>('plugin:pty|read', { pid: this.pid });\r\n this._onData.fire(data);\r\n }\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAca,aAAa,CAAA;AAA1B,IAAA,WAAA,GAAA;QACU,IAAU,CAAA,UAAA,GAAmB,EAAE,CAAC;KAgCzC;AA7BC,IAAA,IAAW,KAAK,GAAA;AACd,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,IAAI,CAAC,MAAM,GAAG,CAAC,QAAuB,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;KACvD;AACD,IAAA,IAAI,CAAC,MAA2B,EAAA;AAC5B,QAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;KAC9C;IACD,KAAK,GAAA;AACD,QAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;KAC9C;IACD,MAAM,GAAA;AACF,QAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;KAC9C;IAEa,QAAQ,GAAA;;AAClB,YAAA,SAAU;AACN,gBAAA,MAAM,IAAI,GAAG,MAAM,MAAM,CAAS,iBAAiB,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;AACxE,gBAAA,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aAC3B;SACJ,CAAA,CAAA;AAAA,KAAA;AACJ;;;;"}
1
+ {"version":3,"file":"index.es.js","sources":["../api/eventEmitter2.ts","../api/index.ts"],"sourcesContent":["/**\r\n * Copyright (c) 2019, Microsoft Corporation (MIT License).\r\n */\r\n\r\nimport { IDisposable } from \".\";\r\n\r\ninterface IListener<T> {\r\n (e: T): void;\r\n}\r\n\r\nexport interface IEvent<T> {\r\n (listener: (e: T) => any): IDisposable;\r\n}\r\n\r\nexport class EventEmitter2<T> {\r\n private _listeners: IListener<T>[] = [];\r\n private _event?: IEvent<T>;\r\n\r\n public get event(): IEvent<T> {\r\n if (!this._event) {\r\n this._event = (listener: (e: T) => any) => {\r\n this._listeners.push(listener);\r\n const disposable = {\r\n dispose: () => {\r\n for (let i = 0; i < this._listeners.length; i++) {\r\n if (this._listeners[i] === listener) {\r\n this._listeners.splice(i, 1);\r\n return;\r\n }\r\n }\r\n }\r\n };\r\n return disposable;\r\n };\r\n }\r\n return this._event;\r\n }\r\n\r\n public fire(data: T): void {\r\n const queue: IListener<T>[] = [];\r\n for (let i = 0; i < this._listeners.length; i++) {\r\n queue.push(this._listeners[i]);\r\n }\r\n for (let i = 0; i < queue.length; i++) {\r\n queue[i].call(undefined, data);\r\n }\r\n }\r\n}","/**\r\n * Copyright (c) 2017, Daniel Imms (MIT License).\r\n * Copyright (c) 2018, Microsoft Corporation (MIT License).\r\n * Copyright (c) 2023, Tnze (MIT License).\r\n */\r\nimport { invoke } from \"@tauri-apps/api\"\r\nimport { EventEmitter2 } from \"./eventEmitter2\";\r\n\r\n/**\r\n * Forks a process as a pseudoterminal.\r\n * @param file The file to launch.\r\n * @param args The file's arguments as argv (string[]) or in a pre-escaped CommandLine format\r\n * (string). Note that the CommandLine option is only available on Windows and is expected to be\r\n * escaped properly.\r\n * @param options The options of the terminal.\r\n * @see CommandLineToArgvW https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx\r\n * @see Parsing C++ Comamnd-Line Arguments https://msdn.microsoft.com/en-us/library/17w5ykft.aspx\r\n * @see GetCommandLine https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156.aspx\r\n */\r\nexport function spawn(file: string, args: string[] | string, options: IPtyForkOptions | IWindowsPtyForkOptions): IPty {\r\n return new TauriPty(file, args, options)\r\n}\r\n\r\nexport interface IBasePtyForkOptions {\r\n\r\n /**\r\n * Name of the terminal to be set in environment ($TERM variable).\r\n */\r\n name?: string;\r\n\r\n /**\r\n * Number of intial cols of the pty.\r\n */\r\n cols?: number;\r\n\r\n /**\r\n * Number of initial rows of the pty.\r\n */\r\n rows?: number;\r\n\r\n /**\r\n * Working directory to be set for the child program.\r\n */\r\n cwd?: string;\r\n\r\n /**\r\n * Environment to be set for the child program.\r\n */\r\n env?: { [key: string]: string | undefined };\r\n\r\n /**\r\n * String encoding of the underlying pty.\r\n * If set, incoming data will be decoded to strings and outgoing strings to bytes applying this encoding.\r\n * If unset, incoming data will be delivered as raw bytes (Buffer type).\r\n * By default 'utf8' is assumed, to unset it explicitly set it to `null`.\r\n */\r\n encoding?: string | null;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * Whether to enable flow control handling (false by default). If enabled a message of `flowControlPause`\r\n * will pause the socket and thus blocking the child program execution due to buffer back pressure.\r\n * A message of `flowControlResume` will resume the socket into flow mode.\r\n * For performance reasons only a single message as a whole will match (no message part matching).\r\n * If flow control is enabled the `flowControlPause` and `flowControlResume` messages are not forwarded to\r\n * the underlying pseudoterminal.\r\n */\r\n handleFlowControl?: boolean;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * The string that should pause the pty when `handleFlowControl` is true. Default is XOFF ('\\x13').\r\n */\r\n flowControlPause?: string;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * The string that should resume the pty when `handleFlowControl` is true. Default is XON ('\\x11').\r\n */\r\n flowControlResume?: string;\r\n}\r\n\r\nexport interface IPtyForkOptions extends IBasePtyForkOptions {\r\n /**\r\n * Security warning: use this option with great caution,\r\n * as opened file descriptors with higher privileges might leak to the child program.\r\n */\r\n uid?: number;\r\n gid?: number;\r\n}\r\n\r\nexport interface IWindowsPtyForkOptions extends IBasePtyForkOptions {\r\n /**\r\n * Whether to use the ConPTY system on Windows. When this is not set, ConPTY will be used when\r\n * the Windows build number is >= 18309 (instead of winpty). Note that ConPTY is available from\r\n * build 17134 but is too unstable to enable by default.\r\n *\r\n * This setting does nothing on non-Windows.\r\n */\r\n useConpty?: boolean;\r\n\r\n /**\r\n * Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty.\r\n * @see https://docs.microsoft.com/en-us/windows/console/createpseudoconsole\r\n */\r\n conptyInheritCursor?: boolean;\r\n}\r\n\r\n/**\r\n * An interface representing a pseudoterminal, on Windows this is emulated via the winpty library.\r\n */\r\nexport interface IPty {\r\n /**\r\n * The process ID of the outer process.\r\n */\r\n readonly pid: number;\r\n\r\n /**\r\n * The column size in characters.\r\n */\r\n readonly cols: number;\r\n\r\n /**\r\n * The row size in characters.\r\n */\r\n readonly rows: number;\r\n\r\n /**\r\n * The title of the active process.\r\n */\r\n readonly process: string;\r\n\r\n /**\r\n * (EXPERIMENTAL)\r\n * Whether to handle flow control. Useful to disable/re-enable flow control during runtime.\r\n * Use this for binary data that is likely to contain the `flowControlPause` string by accident.\r\n */\r\n handleFlowControl: boolean;\r\n\r\n /**\r\n * Adds an event listener for when a data event fires. This happens when data is returned from\r\n * the pty.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\n readonly onData: IEvent<string>;\r\n\r\n /**\r\n * Adds an event listener for when an exit event fires. This happens when the pty exits.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\n readonly onExit: IEvent<{ exitCode: number, signal?: number }>;\r\n\r\n /**\r\n * Resizes the dimensions of the pty.\r\n * @param columns The number of columns to use.\r\n * @param rows The number of rows to use.\r\n */\r\n resize(columns: number, rows: number): void;\r\n\r\n /**\r\n * Clears the pty's internal representation of its buffer. This is a no-op\r\n * unless on Windows/ConPTY. This is useful if the buffer is cleared on the\r\n * frontend in order to synchronize state with the backend to avoid ConPTY\r\n * possibly reprinting the screen.\r\n */\r\n clear(): void;\r\n\r\n /**\r\n * Writes data to the pty.\r\n * @param data The data to write.\r\n */\r\n write(data: string): void;\r\n\r\n /**\r\n * Kills the pty.\r\n * @param signal The signal to use, defaults to SIGHUP. This parameter is not supported on\r\n * Windows.\r\n * @throws Will throw when signal is used on Windows.\r\n */\r\n kill(signal?: string): void;\r\n\r\n /**\r\n * Pauses the pty for customizable flow control.\r\n */\r\n pause(): void;\r\n\r\n /**\r\n * Resumes the pty for customizable flow control.\r\n */\r\n resume(): void;\r\n}\r\n\r\n/**\r\n * An object that can be disposed via a dispose function.\r\n */\r\nexport interface IDisposable {\r\n dispose(): void;\r\n}\r\n\r\n/**\r\n * An event that can be listened to.\r\n * @returns an `IDisposable` to stop listening.\r\n */\r\nexport interface IEvent<T> {\r\n (listener: (e: T) => any): IDisposable;\r\n}\r\n\r\nexport type ArgvOrCommandLine = string[] | string;\r\n\r\nclass TauriPty implements IPty, IDisposable {\r\n pid: number;\r\n cols: number;\r\n rows: number;\r\n process: string;\r\n handleFlowControl: boolean;\r\n\r\n _exitted: boolean;\r\n\r\n private _onData = new EventEmitter2<string>();\r\n private _onExit = new EventEmitter2<{ exitCode: number; signal?: number | undefined; }>();\r\n\r\n constructor(file: string, args?: ArgvOrCommandLine, opt?: IWindowsPtyForkOptions) {\r\n args = typeof args === 'string' ? [args] : args ?? []; // Convert args to string[] anyways.\r\n const invokeArgs = {\r\n file, args,\r\n termName: opt?.name ?? 'Terminal',\r\n cols: opt?.cols ?? null,\r\n rows: opt?.rows ?? null,\r\n cwd: opt?.cwd ?? null,\r\n env: opt?.env ?? {},\r\n encoding: opt?.encoding ?? null,\r\n handleFlowControl: opt?.handleFlowControl ?? null,\r\n flowControlPause: opt?.flowControlPause ?? null,\r\n flowControlResume: opt?.flowControlResume ?? null,\r\n };\r\n invoke<number>('plugin:pty|spawn', invokeArgs).then(pid => {\r\n this._exitted = false;\r\n this.pid = pid;\r\n this.readData()\r\n });\r\n }\r\n dispose(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n public get onData(): IEvent<string> { return this._onData.event; }\r\n public get onExit(): IEvent<{ exitCode: number; signal?: number | undefined; }> { return this._onExit.event; }\r\n\r\n resize(columns: number, rows: number): void {\r\n this.cols = columns;\r\n this.rows = rows;\r\n invoke('plugin:pty|resize', { pid: this.pid, cols: columns, rows }).catch(e => {\r\n console.error('Resize error: ', e);\r\n this.errorCheck();\r\n });\r\n }\r\n clear(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n write(data: string): void {\r\n invoke('plugin:pty|write', { pid: this.pid, data }).catch(e => {\r\n console.error('Writing error: ', e);\r\n this.errorCheck();\r\n });\r\n }\r\n kill(signal?: string | undefined): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n pause(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n resume(): void {\r\n throw new Error(\"Method not implemented.\");\r\n }\r\n\r\n private async readData() {\r\n try {\r\n for (; ;) {\r\n const data = await invoke<string>('plugin:pty|read', { pid: this.pid });\r\n this._onData.fire(data);\r\n }\r\n } catch (e: any) {\r\n this.errorCheck();\r\n if (typeof e === 'string' && e.includes('EOF')) {\r\n return;\r\n }\r\n console.error('Reading error: ', e);\r\n }\r\n }\r\n\r\n private async errorCheck() {\r\n if (this._exitted) {\r\n return;\r\n }\r\n try {\r\n const exitCode = await invoke<number | null>('plugin:pty|exitstatus', { pid: this.pid })\r\n if (exitCode != null) {\r\n this._exitted = true;\r\n this._onExit.fire({ exitCode });\r\n }\r\n } catch (e: any) {\r\n console.error(e)\r\n }\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAca,aAAa,CAAA;AAA1B,IAAA,WAAA,GAAA;QACU,IAAU,CAAA,UAAA,GAAmB,EAAE,CAAC;KAgCzC;AA7BC,IAAA,IAAW,KAAK,GAAA;AACd,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,IAAI,CAAC,MAAM,GAAG,CAAC,QAAuB,KAAI;AACxC,gBAAA,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC/B,gBAAA,MAAM,UAAU,GAAG;oBACjB,OAAO,EAAE,MAAK;AACZ,wBAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;4BAC/C,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE;gCACnC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;gCAC7B,OAAO;6BACR;yBACF;qBACF;iBACF,CAAC;AACF,gBAAA,OAAO,UAAU,CAAC;AACpB,aAAC,CAAC;SACH;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;KACpB;AAEM,IAAA,IAAI,CAAC,IAAO,EAAA;QACjB,MAAM,KAAK,GAAmB,EAAE,CAAC;AACjC,QAAA,KAAK,IAAI,CAAC,GAAG,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;SACJ,CAAA,CAAA;AAAA,KAAA;AACJ;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tauri-pty",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "API package for tauri-plugin-pty",
5
5
  "repository": {
6
6
  "type": "git",