pty-shell 1.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/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/index.d.ts +134 -0
- package/dist/index.js +1044 -0
- package/dist/path_util.d.ts +10 -0
- package/dist/path_util.js +101 -0
- package/dist/word_detection_js.d.ts +21 -0
- package/dist/word_detection_js.js +174 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 xiaobaidadada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# pty-shell
|
|
2
|
+
This is a virtual PTY shell that can be used to filter or log executable commands. It can also be used in a browser environment. Currently, it does not support features like piping. It supports the use of `node-pty`. For more usage examples, you can refer to the code in the [filecat](https://github.com/xiaobaidadada/filecat) project.
|
|
3
|
+
It needs to be used in combination with projects similar to xterm.js.
|
|
4
|
+
这是一个虚拟的pty shell ,可以用来过滤或者记录可执行命令, 并可以用于浏览器环境, 目前并不支持管道等功能,支持使用 node-pty ,更多使用例子目前可以参考 filecat(https://github.com/xiaobaidadada/filecat)项目中的代码 。
|
|
5
|
+
需要配合 xterm.js 类似的项目组合使用
|
|
6
|
+
# Sample Example
|
|
7
|
+
```js
|
|
8
|
+
import {PtyShell} from "pty-shell";
|
|
9
|
+
|
|
10
|
+
const ptyShell = new PtyShell({
|
|
11
|
+
cols: 200,
|
|
12
|
+
rows: 300,
|
|
13
|
+
cwd: __dirname,
|
|
14
|
+
node_pty:undefined,
|
|
15
|
+
node_pty_shell_list: [],
|
|
16
|
+
env:{},
|
|
17
|
+
on_call:(data)=>{
|
|
18
|
+
console.log(data)
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
ptyShell.write("cd .. \r");
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
# supports
|
|
26
|
+
1. commands auto-completion
|
|
27
|
+
2. command and parameter interception and filtering
|
|
28
|
+
3. browser environment
|
|
29
|
+
4. use node-pty
|
|
30
|
+
5. operations such as command, select all, move, select, etc., similar to those in PowerShell.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 有一些子进程 必须要 pty 环境 这里是没有办法的
|
|
3
|
+
* 一个标准的 pty 需要具备以下功能:
|
|
4
|
+
* 输入输出流:处理标准输入、标准输出和标准错误。
|
|
5
|
+
* 环境和工作目录管理:设置子进程的环境变量和工作目录。
|
|
6
|
+
* 终端特性:模拟终端的大小、信号和模式。
|
|
7
|
+
* 子进程管理:启动和管理子进程,处理进程的退出状态。
|
|
8
|
+
* 读取输出与写入输入:捕获输出并发送输入,模拟用户交互。
|
|
9
|
+
* 信号和控制字符支持:处理回车、换行等控制字符,并支持信号转发。
|
|
10
|
+
*/
|
|
11
|
+
export declare enum exec_type {
|
|
12
|
+
not = -1,// 不能执行
|
|
13
|
+
auto_child_process = 0,// 使用内置子线程执行(除了cd命令)
|
|
14
|
+
not_pty = 1,// 使用node_pty 执行(前提是传入了 node_pty)
|
|
15
|
+
continue = 2
|
|
16
|
+
}
|
|
17
|
+
export declare enum exec_cmd_type {
|
|
18
|
+
copy_text = 0
|
|
19
|
+
}
|
|
20
|
+
interface prompt_call_result {
|
|
21
|
+
char_num: number;
|
|
22
|
+
str: string;
|
|
23
|
+
}
|
|
24
|
+
interface PtyShellUserMethod {
|
|
25
|
+
on_call: (data: string) => void;
|
|
26
|
+
on_control_cmd: (type: exec_cmd_type, data?: string) => void;
|
|
27
|
+
on_prompt_call: (cwd: string) => prompt_call_result;
|
|
28
|
+
on_child_kill?: (code: number, pid: number) => void;
|
|
29
|
+
check_exe_cmd?: (cmd_exe: string, params: string[]) => Promise<exec_type>;
|
|
30
|
+
cmd_params_auto_completion: (param_word: string) => string;
|
|
31
|
+
cmd_exe_auto_completion?: (cmd_exe_word: string) => string;
|
|
32
|
+
}
|
|
33
|
+
interface Param extends Partial<PtyShellUserMethod> {
|
|
34
|
+
cwd: string;
|
|
35
|
+
not_use_node_pre_cmd_exec?: boolean;
|
|
36
|
+
node_pty?: any;
|
|
37
|
+
cols?: number;
|
|
38
|
+
rows?: number;
|
|
39
|
+
env?: any;
|
|
40
|
+
node_pty_shell_list?: string[];
|
|
41
|
+
}
|
|
42
|
+
type CmdHandler = (params: string[], send_prompt?: (data: string) => void) => void;
|
|
43
|
+
export declare class PtyShell implements PtyShellUserMethod {
|
|
44
|
+
rows: number;
|
|
45
|
+
cols: number;
|
|
46
|
+
cwd: string;
|
|
47
|
+
env: {};
|
|
48
|
+
constructor(param: Param);
|
|
49
|
+
cmd_params_auto_completion(param_str: any): string | undefined;
|
|
50
|
+
cmd_exe_auto_completion: any;
|
|
51
|
+
on_prompt_call: (cwd: any) => {
|
|
52
|
+
str: string;
|
|
53
|
+
char_num: number;
|
|
54
|
+
};
|
|
55
|
+
on_call: (data: string) => void;
|
|
56
|
+
on_control_cmd: (type: exec_cmd_type, data?: string) => void;
|
|
57
|
+
on_child_kill?: (code: number, pid?: number) => void;
|
|
58
|
+
check_exe_cmd: any;
|
|
59
|
+
private cmd_set;
|
|
60
|
+
private shell_set;
|
|
61
|
+
private node_require;
|
|
62
|
+
private not_use_node_pre_cmd_exec;
|
|
63
|
+
private cmd_exec_map;
|
|
64
|
+
private prompt_call_len;
|
|
65
|
+
private is_running;
|
|
66
|
+
private child_now_line;
|
|
67
|
+
private word_detection;
|
|
68
|
+
private node_pty;
|
|
69
|
+
private child;
|
|
70
|
+
private is_pty;
|
|
71
|
+
private line;
|
|
72
|
+
private line_index;
|
|
73
|
+
private select_line;
|
|
74
|
+
private select_start;
|
|
75
|
+
private select_end;
|
|
76
|
+
private history_line;
|
|
77
|
+
private history_line_index;
|
|
78
|
+
/**
|
|
79
|
+
* public method
|
|
80
|
+
*/
|
|
81
|
+
reset_option(param: Param): void;
|
|
82
|
+
add_cmd_handle(exe_cmd: string, handle: CmdHandler): void;
|
|
83
|
+
close(): void;
|
|
84
|
+
kill(): void;
|
|
85
|
+
/**
|
|
86
|
+
* 处理字符串内容工具函数 防止输出的时候 在尾部单词截断
|
|
87
|
+
* @param str
|
|
88
|
+
*/
|
|
89
|
+
cols_handle(str: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* 向pty写入数据
|
|
92
|
+
* @param data
|
|
93
|
+
*/
|
|
94
|
+
write(data: string): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* static method
|
|
97
|
+
*/
|
|
98
|
+
static isFullCharWidth(char: any): boolean;
|
|
99
|
+
static readFullCharIndex(str: string, start_index: number, len: number): number;
|
|
100
|
+
static get_full_char_num(str: string): number;
|
|
101
|
+
/**
|
|
102
|
+
* private method
|
|
103
|
+
*/
|
|
104
|
+
private get raw_prompt();
|
|
105
|
+
private get enter_prompt();
|
|
106
|
+
private clear_line;
|
|
107
|
+
private get is_line_end();
|
|
108
|
+
private insert_line;
|
|
109
|
+
private close_child;
|
|
110
|
+
private next_not_enter;
|
|
111
|
+
private send_and_enter;
|
|
112
|
+
private update_line;
|
|
113
|
+
private cancel_selected;
|
|
114
|
+
private get line_char_index();
|
|
115
|
+
private ctrl_exec;
|
|
116
|
+
private push_history_line;
|
|
117
|
+
private exec_end_call;
|
|
118
|
+
private parse_exec;
|
|
119
|
+
private multiple_line;
|
|
120
|
+
private spawn_write;
|
|
121
|
+
private spawn;
|
|
122
|
+
private exec_cmd;
|
|
123
|
+
private get_exec;
|
|
124
|
+
get_last_word_cmd_or_param(line: any, now_index: any): {
|
|
125
|
+
word: string;
|
|
126
|
+
is_exe: boolean;
|
|
127
|
+
};
|
|
128
|
+
private is_empty;
|
|
129
|
+
private get_enter_index;
|
|
130
|
+
private get_enter_line;
|
|
131
|
+
private removeCharacterAt;
|
|
132
|
+
private delete_all_enter;
|
|
133
|
+
}
|
|
134
|
+
export {};
|