xshell 1.1.10 → 1.1.11

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/stdin.d.ts +4 -0
  3. package/stdin.js +23 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xshell",
3
- "version": "1.1.10",
3
+ "version": "1.1.11",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "bin": {
package/stdin.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /** 监听终端按键 (输入),并调用 key_processor 处理
2
+ - on_key: 按键处理函数
3
+ - on_exit?: ctrl + c 会退出进程,可加入退出前自定义处理逻辑 */
4
+ export declare function process_stdin(on_key: (key: string) => void | Promise<void>, on_exit?: () => void | Promise<void>): void;
package/stdin.js ADDED
@@ -0,0 +1,23 @@
1
+ import process from 'process';
2
+ /** 监听终端按键 (输入),并调用 key_processor 处理
3
+ - on_key: 按键处理函数
4
+ - on_exit?: ctrl + c 会退出进程,可加入退出前自定义处理逻辑 */
5
+ export function process_stdin(on_key, on_exit) {
6
+ // https://stackoverflow.com/a/12506613/7609214
7
+ let { stdin } = process;
8
+ stdin.setRawMode(true);
9
+ stdin.resume();
10
+ stdin.setEncoding('utf-8');
11
+ // on any data into stdin
12
+ stdin.on('data', async (key) => {
13
+ // ctrl-c ( end of text )
14
+ if (key === '\u0003') {
15
+ await on_exit?.();
16
+ process.exit();
17
+ }
18
+ // write the key to stdout all normal like
19
+ console.log(key);
20
+ await on_key(key);
21
+ });
22
+ }
23
+ //# sourceMappingURL=stdin.js.map