diff --git a/plugin/plugin.json b/plugin/plugin.json index 99f19e4..66d50c8 100644 --- a/plugin/plugin.json +++ b/plugin/plugin.json @@ -14,7 +14,8 @@ "pluginSetting": { "single": false }, - "features": [{ + "features": [ + { "code": "configuration", "explain": "创建或编辑快捷命令", "cmds": [ diff --git a/plugin/preload.js b/plugin/preload.js index ff6539f..3e56f8a 100644 --- a/plugin/preload.js +++ b/plugin/preload.js @@ -569,7 +569,7 @@ window.runCodeInSandbox = (code, callback, addVars = {}) => { } } -window.runCodeFile = (cmd, option, terminal, callback) => { +window.runCodeFile = (cmd, option, terminal, callback, realTime=true) => { let { bin, argv, @@ -612,24 +612,28 @@ window.runCodeFile = (cmd, option, terminal, callback) => { shell: true, env: processEnv }); - // var chunks = [], - // err_chunks = []; + let chunks = [], err_chunks = []; console.log('Running: ' + cmdline); child.stdout.on('data', chunk => { if (charset.outputCode) chunk = iconv.decode(chunk, charset.outputCode); - callback(chunk.toString(), null); - // chunks.push(chunk) + realTime + ? callback(chunk.toString(), null) + : chunks.push(chunk); }); - child.stderr.on('data', stderr => { - if (charset.outputCode) stderr = iconv.decode(stderr, charset.outputCode); - callback(null, stderr.toString()); - // err_chunks.push(err_chunk) + child.stderr.on("data", (err_chunk) => { + if (charset.outputCode) + err_chunk = iconv.decode(err_chunk, charset.outputCode); + realTime + ? callback(null, err_chunk.toString()) + : err_chunks.push(err_chunk); }); - // child.on('close', code => { - // let stdout = chunks.join(""); - // let stderr = err_chunks.join(""); - // callback(stdout, stderr) - // }) + if (!realTime) { + child.on('close', code => { + let stdout = chunks.join(""); + let stderr = err_chunks.join(""); + callback(stdout, stderr) + }) + } return child } diff --git a/src/App.vue b/src/App.vue index b750a9f..e302cf8 100644 --- a/src/App.vue +++ b/src/App.vue @@ -62,6 +62,20 @@ export default defineComponent({ this.utools.whole.onPluginEnter((enter) => { this.enterPlugin(enter); }); + this.utools.whole.onMainPush( + async ({ code, type, payload }) => { + let result = await this.runCommand(code, payload, 5000); + return result.map((x) => { + return { + text: x, + }; + }); + }, + ({ code, type, payload, option }) => { + window.quickcommand.writeClipboard(option.text); + window.utools.showNotification("已复制"); + } + ); this.utools.whole.onPluginOut(() => { this.outPlugin(); }); @@ -115,19 +129,43 @@ export default defineComponent({ this.runCommandSilently(featureCode); }); }, + runCommand(featureCode, mainInput, timeout = false) { + return new Promise((reslove, reject) => { + timeout && + setTimeout(() => { + reslove([`超过${timeout}ms未响应`]); + }, timeout); + let command = this.utools.getDB("qc_" + featureCode); + let commandCode = command.cmd; + if (mainInput) + commandCode = commandCode.replace(/\{\{input\}\}/g, mainInput); + if (command.program === "quickcommand") { + window.runCodeInSandbox(commandCode, (stdout, stderr) => { + stderr && reslove([stderr.toString()]); + reslove(stdout); + }); + } else { + let option = + command.program === "custom" + ? command.customOptions + : this.programs[command.program]; + option.scptarg = command.scptarg; + option.charset = command.charset; + window.runCodeFile( + commandCode, + option, + false, + (stdout, stderr) => { + stderr && reslove([stderr.toString()]); + reslove([stdout]); + }, + false + ); + } + }); + }, runCommandSilently(featureCode) { - let command = this.utools.getDB("qc_" + featureCode); - if (command.program === "quickcommand") { - window.runCodeInSandbox(command.cmd, () => {}); - } else { - let option = - command.program === "custom" - ? command.customOptions - : this.programs[command.program]; - option.scptarg = command.scptarg; - option.charset = command.charset; - window.runCodeFile(command.cmd, option, false, () => {}); - } + this.runCommand(featureCode); }, usageStatistics(featureCode, runTime) { let statisticsData = this.utools.getDB("cfg_statisticsData"); diff --git a/src/components/CommandSideBar.vue b/src/components/CommandSideBar.vue index 7b0619d..e488bcb 100644 --- a/src/components/CommandSideBar.vue +++ b/src/components/CommandSideBar.vue @@ -23,6 +23,12 @@ />