pty-shell 1.3.0 → 1.4.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/dist/index.d.ts CHANGED
@@ -82,6 +82,9 @@ export declare class PtyShell implements PtyShellUserMethod {
82
82
  private history_line;
83
83
  private history_line_index;
84
84
  private history_line_max;
85
+ private _not_write;
86
+ get not_write(): boolean;
87
+ set not_write(value: boolean);
85
88
  /**
86
89
  * public method
87
90
  */
package/dist/index.js CHANGED
@@ -138,6 +138,7 @@ class PtyShell {
138
138
  this.history_line = [];
139
139
  this.history_line_index = -1;
140
140
  this.history_line_max = 20;
141
+ this._not_write = false;
141
142
  this.next_not_enter = false;
142
143
  this.reset_option(param);
143
144
  this.on_call(this.raw_prompt);
@@ -149,6 +150,7 @@ class PtyShell {
149
150
  const p = path.isAbsolute(params[0]) ? params[0] : path.join(this.cwd, params[0]);
150
151
  if (!fs.existsSync(p)) {
151
152
  this.send_and_enter(`not directory ${p}`);
153
+ return;
152
154
  }
153
155
  this.cwd = p;
154
156
  this.send_and_enter(``);
@@ -178,6 +180,12 @@ class PtyShell {
178
180
  }
179
181
  return v;
180
182
  }
183
+ get not_write() {
184
+ return this._not_write;
185
+ }
186
+ set not_write(value) {
187
+ this._not_write = value;
188
+ }
181
189
  /**
182
190
  * public method
183
191
  */
@@ -252,8 +260,10 @@ class PtyShell {
252
260
  */
253
261
  write(data) {
254
262
  return __awaiter(this, void 0, void 0, function* () {
255
- if (this.have_child()) {
256
- // 终端shell 完全 托管给 别的程序
263
+ if (this._not_write)
264
+ return;
265
+ if (this.node_pty_child != null) {
266
+ // 终端shell 完全 托管给 别的程序 只有 pty 的子程序可以 其他 需要pty-shell充当编辑器
257
267
  this.spawn_write(data);
258
268
  return;
259
269
  }
@@ -362,6 +372,7 @@ class PtyShell {
362
372
  }
363
373
  send_and_enter(str, send_prompt = false) {
364
374
  try {
375
+ const have_child = this.have_child();
365
376
  if (typeof str == "string") {
366
377
  const list = [];
367
378
  let i = 0;
@@ -381,7 +392,7 @@ class PtyShell {
381
392
  }
382
393
  if (i === 0 || i !== last_i)
383
394
  list.push(str.substring(i));
384
- if (this.have_child()) {
395
+ if (have_child) {
385
396
  // 添加子进程的提示换行
386
397
  this.child_now_line = list[list.length - 1];
387
398
  }
@@ -396,7 +407,7 @@ class PtyShell {
396
407
  }
397
408
  this.next_not_enter = str.endsWith('\n\r') || str.endsWith('\r\n'); // 下一次不用换行了
398
409
  }
399
- if (!this.have_child() || send_prompt) {
410
+ if (!have_child || send_prompt) {
400
411
  this.on_call(`${this.enter_prompt}`);
401
412
  }
402
413
  this.clear_line();
@@ -408,8 +419,9 @@ class PtyShell {
408
419
  // 重新更新显示本行 也许可以更节省的更新 文本 powershell 这样的每次都是全部更新 暂时和他一样
409
420
  update_line(param) {
410
421
  var _a, _b;
411
- const prompt = !this.child ? this.raw_prompt : this.child_now_line;
412
- let len = (!this.child ? this.prompt_call_len : char_util_1.CharUtil.get_full_char_num(prompt)) + this.line_char_index; // 字符串前面的字符数量
422
+ const have_child = this.have_child();
423
+ const prompt = !have_child ? this.raw_prompt : this.child_now_line;
424
+ let len = (!have_child ? this.prompt_call_len : char_util_1.CharUtil.get_full_char_num(prompt)) + this.line_char_index; // 字符串前面的字符数量
413
425
  if (param && param.line_add_num) {
414
426
  len += param.line_add_num;
415
427
  }
@@ -566,7 +578,7 @@ class PtyShell {
566
578
  this.cancel_selected();
567
579
  this.update_line({ line_add_num: 1 });
568
580
  }
569
- else if (this.child) {
581
+ else if (this.have_child()) {
570
582
  this.close_child(0);
571
583
  return;
572
584
  }
@@ -741,6 +753,7 @@ class PtyShell {
741
753
  // 解析和执行命令 执行完会自动换行的
742
754
  parse_exec() {
743
755
  return __awaiter(this, void 0, void 0, function* () {
756
+ var _a;
744
757
  // const line = this.delete_all_enter(this.line);
745
758
  if (!this.line && !this.have_child()) {
746
759
  this.send_and_enter("");
@@ -748,6 +761,12 @@ class PtyShell {
748
761
  return;
749
762
  }
750
763
  this.push_history_line(this.line);
764
+ if (this.have_child()) {
765
+ // 把数据给正在运行的别的程序 但是肯定不是 pty 的pty 不需要pty-shell 做命令编辑器
766
+ this.spawn_write(`${this.line}\n`);
767
+ this.clear_line();
768
+ return;
769
+ }
751
770
  let { exe, params } = this.get_exec(this.line);
752
771
  this.history_line_index = -1;
753
772
  try {
@@ -787,21 +806,24 @@ class PtyShell {
787
806
  }
788
807
  if (this.js_child_map.has(exe)) {
789
808
  const c_ = this.js_child_map.get(exe);
790
- this.js_func_child = new c_(() => {
791
- this.send_and_enter("");
809
+ this.js_func_child = new c_(this, () => {
810
+ this.send_and_enter("", true);
792
811
  this.next_not_enter = false; // 下一次的换行输出 上一次没有换行
793
812
  this.close_child();
794
813
  }, (str) => {
795
- this.on_call(str);
814
+ this.send_and_enter(str);
796
815
  }, params);
816
+ this.js_func_child.init();
797
817
  return;
798
818
  }
799
- this.spawn(exe, params, use_noe_pty);
819
+ else {
820
+ this.spawn(exe, params, use_noe_pty);
821
+ }
800
822
  this.clear_line();
801
823
  }
802
824
  catch (e) {
803
825
  // console.log("子线程执行异常", e);
804
- this.send_and_enter(e.message);
826
+ this.send_and_enter((_a = e.message) !== null && _a !== void 0 ? _a : e);
805
827
  this.exec_end_call(-1);
806
828
  }
807
829
  });
package/dist/type.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import { PtyShell } from "./index";
1
2
  export interface child_func_type {
2
3
  write(str: string): void;
3
4
  kill(): void;
5
+ init(): void;
4
6
  }
5
- export type child_func_constructor = new (exit_on: () => void, send: (str: string) => void, params: string[]) => child_func_type;
7
+ export type child_func_constructor = new (pty_shell: PtyShell, exit_on: () => void, send: (str: string) => void, params: string[]) => child_func_type;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pty-shell",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "a virtual PTY shell for javaScript",
5
5
  "author": "xiaobaidadada",
6
6
  "main": "dist/index.js",