From dcd392ba476668ecc54920087f398f11f6be7514 Mon Sep 17 00:00:00 2001 From: fofolee Date: Fri, 24 Jan 2025 23:53:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20runCode=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=9A=E5=B0=86=E5=8F=82=E6=95=B0=E7=94=B1=20progra?= =?UTF-8?q?m=20=E6=94=B9=E4=B8=BA=20options=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E9=85=8D=E7=BD=AE=E9=80=89=E9=A1=B9=EF=BC=8C?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=E7=BC=96=E7=A8=8B=E8=AF=AD=E8=A8=80=E3=80=81?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=8F=82=E6=95=B0=E5=92=8C=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=EF=BC=8C=E5=90=8C=E6=97=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=20API=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../quickcommandUI/QuickCommand.vue | 49 ++++++++++++- .../monaco/types/quickcommand.api.d.ts | 71 +++++++++++++------ 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/src/components/quickcommandUI/QuickCommand.vue b/src/components/quickcommandUI/QuickCommand.vue index 731c566..c5ec06d 100644 --- a/src/components/quickcommandUI/QuickCommand.vue +++ b/src/components/quickcommandUI/QuickCommand.vue @@ -266,12 +266,55 @@ export default { // 获取用户数据 window.quickcommand.userData = this.$root.utools.userData; - // 添加runCode方法,不在preload中加是因为programs写在了src中-_- - quickcommand.runCode = (code, program, runInTerminal = false) => { + /** + * 执行代码 + * 添加runCode方法,不在preload中加是因为programs写在了src中-_- + * @param code 代码文本 + * @param options 选项 + * @param options.language 编程语言 + * @param options.args 脚本参数 + * @param options.charset 编码 + * @param options.charset.scriptCode 脚本编码 + * @param options.charset.outputCode 输出编码 + * @param options.runInTerminal 终端运行参数,不传则不在终端运行 + * @param options.runInTerminal.dir 运行目录 + * @param options.runInTerminal.windows windows使用的终端,默认wt + * @param options.runInTerminal.macos macos使用的终端,默认warp + */ + quickcommand.runCode = (code, options) => { return new Promise((reslove, reject) => { + const isWin = window.utools.isWindows(); + const { + language = isWin ? "cmd" : "shell", + charset = {}, + args = [], + runInTerminal, + } = options; + + const unescapeAndQuote = (str) => `"${str.replace(/\\"/g, '"')}"`; + + if (!programs[language]) { + return reject(new Error(`不支持的语言: ${language}`)); + } + + if (!Array.isArray(args)) { + return reject(new TypeError(`args 应为 Array, 而非 ${typeof args}`)); + } + const argsStr = args.map(unescapeAndQuote).join(" "); + + const defaultCharset = + isWin && ["cmd", "powershell"].includes(language) ? "gbk" : "utf-8"; + + const { scriptCode = defaultCharset, outputCode = defaultCharset } = + charset; + window.runCodeFile( code, - { ...programs[program], charset: {}, scptarg: "" }, + { + ...programs[language], + charset: { scriptCode, outputCode }, + scptarg: argsStr, + }, runInTerminal, (result, err) => (err ? reject(err) : reslove(result)) ); diff --git a/src/plugins/monaco/types/quickcommand.api.d.ts b/src/plugins/monaco/types/quickcommand.api.d.ts index 2b57b67..44df73b 100644 --- a/src/plugins/monaco/types/quickcommand.api.d.ts +++ b/src/plugins/monaco/types/quickcommand.api.d.ts @@ -652,38 +652,63 @@ interface quickcommandApi { /** * 运行代码 * @param code 代码 - * @param program 编程语言 - * @param runInTerminal 终端运行参数,不传则不在终端运行 - * @param runInTerminal.dir 运行目录 - * @param runInTerminal.windows windows使用的终端,默认wt - * @param runInTerminal.macos macos使用的终端,默认warp + * @param options 选项 + * @param options.language 编程语言,不传时则根据操作系统选择cmd或是shell + * @param options.args 脚本参数 + * @param options.charset 编码,不传时则根据操作系统及语言选择utf-8或是gbk + * @param options.charset.scriptCode 脚本编码 + * @param options.charset.outputCode 输出编码 + * @param options.runInTerminal 终端运行参数,不传则不在终端运行 + * @param options.runInTerminal.dir 运行目录 + * @param options.runInTerminal.windows windows使用的终端,默认wt + * @param options.runInTerminal.macos macos使用的终端,默认warp * * 支持的编程语言: * shell, applescript, cmd, python, powershell, javascript, ruby, php, lua, perl, csharp, c * * ```js - * quickcommand.runCode("print('Hello, World!');", "python"); + * const script = ` + * import sys + * print(sys.argv[1]) + * ` + * const options = { + * language: "python", + * args: ["hello world"] + * } + + * quickcommand.runCode(script, options).then(result => { + * console.log(result) + * }).catch(e => { + * console.log(e) + * }) * ``` */ runCode( code: string, - program: - | "shell" - | "applescript" - | "cmd" - | "python" - | "powershell" - | "javascript" - | "ruby" - | "php" - | "lua" - | "perl" - | "csharp" - | "c", - runInTerminal?: { - dir?: string; - windows?: "wt" | "cmd"; - macos?: "warp" | "iterm" | "terminal"; + options: { + language: + | "shell" + | "applescript" + | "cmd" + | "python" + | "powershell" + | "javascript" + | "ruby" + | "php" + | "lua" + | "perl" + | "csharp" + | "c"; + args?: string[]; + charset?: { + scriptCode?: string; + outputCode?: string; + }; + runInTerminal?: { + dir?: string; + windows?: "wt" | "cmd"; + macos?: "warp" | "iterm" | "terminal"; + }; } ): Promise; }