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.
@@ -0,0 +1,10 @@
1
+ export declare function find_sep(path: any): string;
2
+ /**
3
+ * windows 是 \ linux 是 /
4
+ * @param path
5
+ * @param filename
6
+ * @returns {*|string}
7
+ */
8
+ export declare function path_join(path: any, filename: any): any;
9
+ export declare function get_best_cmd(list: any): any;
10
+ export declare function isAbsolutePath(path: any): boolean;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.find_sep = find_sep;
4
+ exports.path_join = path_join;
5
+ exports.get_best_cmd = get_best_cmd;
6
+ exports.isAbsolutePath = isAbsolutePath;
7
+ function get_extname(filePath) {
8
+ const lastDotIndex = filePath.lastIndexOf('.');
9
+ // 如果没有找到点或文件以点开头,则没有扩展名
10
+ if (lastDotIndex === -1 || lastDotIndex === 0) {
11
+ return '';
12
+ }
13
+ // 返回从最后一个点开始到字符串结尾的部分
14
+ return filePath.slice(lastDotIndex);
15
+ }
16
+ function find_sep(path) {
17
+ let sep = "\\";
18
+ for (const it of path) {
19
+ if (it === "/" || it === "\\") {
20
+ sep = it;
21
+ break;
22
+ }
23
+ }
24
+ return sep;
25
+ }
26
+ /**
27
+ * windows 是 \ linux 是 /
28
+ * @param path
29
+ * @param filename
30
+ * @returns {*|string}
31
+ */
32
+ function path_join(path, filename) {
33
+ // 先找到系统上的分隔符是啥
34
+ let s_r = find_sep(path);
35
+ if (filename === '.') {
36
+ return path;
37
+ }
38
+ else if (filename === "..") {
39
+ if (path.length > 1) {
40
+ let ok = path[path.length - 1] === s_r ? 1 : 0;
41
+ for (let i = path.length - 1 - ok; i >= 0; i--) {
42
+ if (path[i] === s_r) {
43
+ return path.substring(0, i + 1);
44
+ }
45
+ }
46
+ }
47
+ return path;
48
+ }
49
+ if (path.endsWith(s_r) && filename.startsWith(s_r)) {
50
+ // 前后都有 /
51
+ return path.slice(0, -1) + filename;
52
+ }
53
+ else if (!path.endsWith(s_r) && !filename.startsWith(s_r)) {
54
+ // 前后都没有 /
55
+ return path + s_r + filename;
56
+ }
57
+ else {
58
+ return path + filename;
59
+ }
60
+ }
61
+ const exec_map = {
62
+ ".com": 4, // 越大优先
63
+ ".exe": 3,
64
+ ".bat": 2,
65
+ ".cmd": 1
66
+ };
67
+ // 获取最合适的命令 list 都是字符串内容
68
+ function get_best_cmd(list) {
69
+ if (!list)
70
+ return;
71
+ let ok;
72
+ let ok_p = 0;
73
+ for (const item of list) {
74
+ const v = exec_map[get_extname(item)];
75
+ if (v !== undefined && v > ok_p) {
76
+ ok = item;
77
+ ok_p = v;
78
+ }
79
+ }
80
+ if (ok === undefined) {
81
+ // 选出一个最短的
82
+ for (const item of list) {
83
+ if (item.length > ok_p) {
84
+ ok = item;
85
+ ok_p = item.length;
86
+ }
87
+ }
88
+ }
89
+ return ok;
90
+ }
91
+ function isAbsolutePath(path) {
92
+ if (typeof path !== 'string') {
93
+ return false;
94
+ }
95
+ // 检查是否为 Windows 绝对路径,以盘符(如 C:)开头
96
+ const windowsRegex = /^[a-zA-Z]:[\\\/]/;
97
+ // 检查是否为 Unix/Linux 绝对路径,以斜杠 / 开头
98
+ const unixRegex = /^\//;
99
+ return windowsRegex.test(path) || unixRegex.test(path);
100
+ }
101
+ // console.log(get_best_cmd(['ab','ab.bat','ab.exe']))
@@ -0,0 +1,21 @@
1
+ export declare class word_detection_js {
2
+ root: any;
3
+ size: any;
4
+ constructor();
5
+ add(str: any): void;
6
+ clear(): void;
7
+ /**
8
+ * 在完全匹配的基础之上(不是尽可能) 再额外添加一个字符 获取这个额外添加的字符的所有更多尽可能的可能性
9
+ * @param word
10
+ * @param extra_word_char 在匹配自己的前提下 获取以此 往前多一个分叉的所有子分叉 (比如 cmd 使用 . 获取 cmd.exe cmd.bat 等)
11
+ */
12
+ detection_next_list_word(word: any, extra_word_char: any): any[];
13
+ /**
14
+ * 会 ”尽可能“ 的往前匹配更多更匹配的 "唯一" 单词(有分叉就停止检索)
15
+ * 如果和原本的输入一样就不返回了
16
+ * @param word 单词 而不是被检测的文本
17
+ * @param prefer_char 如果有这种情况 遇上分叉了 但是提供一个可选项,使用这个如果可以继续往前获取唯一的单词就继续往前
18
+ * @return str or undefined
19
+ */
20
+ detection_next_one_word(word: any, prefer_char?: any): any;
21
+ }
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.word_detection_js = void 0;
4
+ class Node {
5
+ constructor() {
6
+ this.children = new Map();
7
+ this.char = "";
8
+ this.is_end = false;
9
+ }
10
+ }
11
+ class word_detection_js {
12
+ constructor() {
13
+ this.root = new Node(); // 根节点的字符是空的
14
+ this.size = 0;
15
+ }
16
+ add(str) {
17
+ if (!str) {
18
+ return;
19
+ }
20
+ str = str.trim();
21
+ let node = this.root;
22
+ for (const char of str) {
23
+ // 不断的给子节点添加子节点
24
+ let ret = node.children.get(char);
25
+ if (ret == null) {
26
+ ret = new Node();
27
+ ret.char = char;
28
+ node.children.set(char, ret);
29
+ }
30
+ node = ret;
31
+ }
32
+ node.is_end = true;
33
+ }
34
+ clear() {
35
+ this.root = undefined;
36
+ }
37
+ /**
38
+ * 在完全匹配的基础之上(不是尽可能) 再额外添加一个字符 获取这个额外添加的字符的所有更多尽可能的可能性
39
+ * @param word
40
+ * @param extra_word_char 在匹配自己的前提下 获取以此 往前多一个分叉的所有子分叉 (比如 cmd 使用 . 获取 cmd.exe cmd.bat 等)
41
+ */
42
+ detection_next_list_word(word, extra_word_char) {
43
+ const r_list = [];
44
+ if (!word || !this.root)
45
+ r_list;
46
+ let now_node = this.root;
47
+ for (let i = 0; i < word.length; i++) {
48
+ const v = now_node.children.get(word[i]);
49
+ if (v == null) {
50
+ return r_list; // 本身就无法匹配完整
51
+ }
52
+ now_node = v;
53
+ }
54
+ // 基本字符都匹配上了现在来看看有没有多余的
55
+ if (now_node.is_end) {
56
+ // 把本身先加入进去
57
+ r_list.push(word);
58
+ }
59
+ if (!now_node.children) {
60
+ return r_list;
61
+ }
62
+ const ok_children_list = []; // 前面的都匹配上了
63
+ const dif_char_node = now_node.children.get(extra_word_char);
64
+ if (dif_char_node !== undefined && dif_char_node.children !== undefined) {
65
+ for (const node of dif_char_node.children.values()) {
66
+ node.word = word + extra_word_char;
67
+ ok_children_list.push(node);
68
+ }
69
+ }
70
+ // 剩下的必须唯一 不然就太多了
71
+ for (let i = 0; i < ok_children_list.length; i++) {
72
+ let node = ok_children_list[i];
73
+ let str = node.word;
74
+ while (node != null) {
75
+ if (node.is_end) {
76
+ str += node.char;
77
+ r_list.push(str);
78
+ break;
79
+ }
80
+ if (node.children !== undefined) {
81
+ // 还有子节点
82
+ if (node.children.size === 1) {
83
+ str += node.char;
84
+ node = node.children.values().next().value; // 获取唯一的元素值
85
+ }
86
+ else {
87
+ break; // 不唯一返回吧
88
+ }
89
+ }
90
+ else {
91
+ if (node.is_end) {
92
+ // 没有子节点了 is_end 也是true 不做判断了 直接返回吧
93
+ str += node.char;
94
+ r_list.push(str);
95
+ break;
96
+ }
97
+ else {
98
+ // 既没有子节点 也不是最后一个字节 数据是有问题的只能忽略了
99
+ break;
100
+ }
101
+ }
102
+ }
103
+ }
104
+ return r_list;
105
+ }
106
+ /**
107
+ * 会 ”尽可能“ 的往前匹配更多更匹配的 "唯一" 单词(有分叉就停止检索)
108
+ * 如果和原本的输入一样就不返回了
109
+ * @param word 单词 而不是被检测的文本
110
+ * @param prefer_char 如果有这种情况 遇上分叉了 但是提供一个可选项,使用这个如果可以继续往前获取唯一的单词就继续往前
111
+ * @return str or undefined
112
+ */
113
+ detection_next_one_word(word, prefer_char = undefined) {
114
+ if (!word || !this.root)
115
+ return;
116
+ let now_node = this.root;
117
+ for (let i = 0; i < word.length; i++) {
118
+ const v = now_node.children.get(word[i]);
119
+ if (v == null) {
120
+ return; // 本身就无法匹配完整
121
+ }
122
+ now_node = v;
123
+ }
124
+ if (now_node.is_end) {
125
+ return; // 一样的话就不返回了
126
+ }
127
+ if (now_node.children !== undefined) {
128
+ if (now_node.children.size === 1) {
129
+ // 只有一个元素 跳过最后一个字符
130
+ now_node = now_node.children.values().next().value;
131
+ }
132
+ else if (prefer_char !== undefined) {
133
+ // 有优先字符查找是否有优先字符
134
+ now_node = now_node.children.get(prefer_char);
135
+ }
136
+ }
137
+ // 前面的都匹配的有了
138
+ while (now_node != null) {
139
+ if (now_node.is_end) {
140
+ word += now_node.char;
141
+ return word;
142
+ }
143
+ if (now_node.children !== undefined) {
144
+ // 还有子节点
145
+ if (now_node.children.size === 1) {
146
+ word += now_node.char;
147
+ now_node = now_node.children.values().next().value; // 获取唯一的元素值
148
+ }
149
+ else {
150
+ return word + now_node.char; // 接下来匹配会分叉就匹配到这返回吧
151
+ }
152
+ }
153
+ else {
154
+ if (now_node.is_end) {
155
+ // 没有子节点了 is_end 也是true 不做判断了 直接返回吧
156
+ return word;
157
+ }
158
+ else {
159
+ // 既没有子节点 也不是最后一个字节 数据是有问题的只能忽略了
160
+ return;
161
+ }
162
+ }
163
+ }
164
+ // 返回特殊的return
165
+ }
166
+ }
167
+ exports.word_detection_js = word_detection_js;
168
+ // const test = new word_detection_js();
169
+ // test.add("node.exe")
170
+ // test.add("node")
171
+ // test.add("cmd.exe")
172
+ // test.add("powershell.exe")
173
+ //
174
+ // console.log(test.detection_next_list_word("node",))
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "pty-shell",
3
+ "version": "1.0.5",
4
+ "description": "a virtual PTY shell for javaScript",
5
+ "author": "xiaobaidadada",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git@github.com:xiaobaidadada/pty-shell.git"
11
+ },
12
+ "homepage": "https://github.com/xiaobaidadada/pty-shell",
13
+ "bugs": {
14
+ "url": "https://github.com/xiaobaidadada/pty-shell/issues"
15
+ },
16
+ "scripts": {
17
+ "build": "tsc"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^22.13.10",
21
+ "typescript": "^5.5.3"
22
+ },
23
+ "files": [
24
+ "dist/**/*",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "package.json"
28
+ ],
29
+ "keywords":[
30
+ "pty",
31
+ "shell",
32
+ "tty",
33
+ "terminal",
34
+ "browser",
35
+ "web",
36
+ "virtual",
37
+ "command intercept"
38
+ ]
39
+ }