pty-shell
Version:
a virtual PTY shell for javaScript
138 lines (137 loc) • 4.32 kB
TypeScript
/**
* 有一些子进程 必须要 pty 环境 这里是没有办法的
* 一个标准的 pty 需要具备以下功能:
* 输入输出流:处理标准输入、标准输出和标准错误。
* 环境和工作目录管理:设置子进程的环境变量和工作目录。
* 终端特性:模拟终端的大小、信号和模式。
* 子进程管理:启动和管理子进程,处理进程的退出状态。
* 读取输出与写入输入:捕获输出并发送输入,模拟用户交互。
* 信号和控制字符支持:处理回车、换行等控制字符,并支持信号转发。
*/
export declare enum exec_type {
not = -1,// 不能执行
auto_child_process = 0,// 使用内置子线程执行(除了cd命令)
not_pty = 1,// 使用node_pty 执行(前提是传入了 node_pty)
continue = 2
}
export declare enum exec_cmd_type {
copy_text = 0
}
interface prompt_call_result {
char_num: number;
str: string;
}
interface PtyShellUserMethod {
on_call: (data: string) => void;
on_control_cmd: (type: exec_cmd_type, data?: string) => void;
on_prompt_call: (cwd: string) => prompt_call_result;
on_child_kill?: (code: number, pid: number) => void;
check_exe_cmd?: (cmd_exe: string, params: string[]) => Promise<exec_type>;
cmd_params_auto_completion: (param_word: string) => string;
cmd_exe_auto_completion?: (cmd_exe_word: string) => string;
}
interface Param extends Partial<PtyShellUserMethod> {
cwd: string;
not_use_node_pre_cmd_exec?: boolean;
node_pty?: any;
cols?: number;
rows?: number;
env?: any;
node_pty_shell_list?: string[];
history_line?: string[];
history_line_max?: number;
}
type CmdHandler = (params: string[], send_prompt?: (data: string) => void) => void;
export declare class PtyShell implements PtyShellUserMethod {
rows: number;
cols: number;
cwd: string;
env: {};
constructor(param: Param);
cmd_params_auto_completion(param_str: any): string | undefined;
cmd_exe_auto_completion: any;
on_prompt_call: (cwd: any) => {
str: string;
char_num: number;
};
on_call: (data: string) => void;
on_control_cmd: (type: exec_cmd_type, data?: string) => void;
on_child_kill?: (code: number, pid?: number) => void;
check_exe_cmd: any;
private cmd_set;
private shell_set;
private node_require;
private not_use_node_pre_cmd_exec;
private cmd_exec_map;
private prompt_call_len;
private is_running;
private child_now_line;
private word_detection;
private node_pty;
private child;
private is_pty;
private line;
private line_index;
private select_line;
private select_start;
private select_end;
private history_line;
private history_line_index;
private history_line_max;
/**
* public method
*/
reset_option(param: Param): void;
add_cmd_handle(exe_cmd: string, handle: CmdHandler): void;
close(): void;
kill(): void;
/**
* 处理字符串内容工具函数 防止输出的时候 在尾部单词截断
* @param str
*/
cols_handle(str: string): string;
/**
* 向pty写入数据
* @param data
*/
write(data: string): Promise<void>;
/**
* static method
*/
static isFullCharWidth(char: any): boolean;
static readFullCharIndex(str: string, start_index: number, len: number): number;
static get_full_char_num(str: string): number;
/**
* private method
*/
private get raw_prompt();
private get enter_prompt();
private clear_line;
private get is_line_end();
private insert_line;
private close_child;
private next_not_enter;
private send_and_enter;
private update_line;
private cancel_selected;
private get line_char_index();
private ctrl_exec;
private push_history_line;
private exec_end_call;
private parse_exec;
private multiple_line;
private spawn_write;
private spawn;
private exec_cmd;
private get_exec;
get_last_word_cmd_or_param(line: any, now_index: any): {
word: string;
is_exe: boolean;
};
private is_empty;
private get_enter_index;
private get_enter_line;
private removeCharacterAt;
private delete_all_enter;
}
export {};