完善执行命令功能 80%

This commit is contained in:
fofolee 2022-04-09 16:27:55 +08:00
parent b36248c9bc
commit 94b19dce5d
4 changed files with 102 additions and 56 deletions

View File

@ -195,22 +195,22 @@ if (process.platform !== 'linux') quickcommand.runInTerminal = function(cmdline,
}
let getCommandToLaunchTerminal = (cmdline, dir) => {
let cd = ''
if (utools.isWindows()) {
let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/')
// 直接 existsSync wt.exe 无效
if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) {
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"`
let cd = ''
if (utools.isWindows()) {
let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/')
// 直接 existsSync wt.exe 无效
if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) {
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"`
} else {
cmdline = cmdline.replace(/"/g, `^"`)
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&`
command = `${cd} start "" cmd /k "${cmdline}"`
}
} else {
cmdline = cmdline.replace(/"/g, `^"`)
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&`
command = `${cd} start "" cmd /k "${cmdline}"`
}
} else {
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
cmdline = cmdline.replace(/"/g, `\\"`)
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
if (fs.existsSync('/Applications/iTerm.app')) {
command = `osascript -e 'tell application "iTerm"
create window with default profile
@ -473,8 +473,8 @@ getNodeJsCommand = () => {
return obj
}
htmlEncode = (value, raw = true) => {
return raw ? String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;") : value
htmlEncode = (value) => {
return String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;")
}
hexEncode = text => Buffer.from(text, 'utf8').toString('hex')

View File

@ -352,7 +352,9 @@ export default {
},
//
runCurrentCommand() {
this.quickcommandInfo.output = this.$refs.menu?.currentCommand.output;
// action run text
this.quickcommandInfo.output =
this.$refs.menu?.currentCommand.output || "text";
this.$refs.result.runCurrentCommand(this.quickcommandInfo);
},
},

View File

@ -1,6 +1,6 @@
<template>
<div>
<div v-if="action.type === 'inPlugin'">
<div v-if="!fromUtools">
<q-dialog v-model="isResultShow" @hide="runResult = ''" position="bottom">
<q-card style="width: 90vh">
<q-toolbar>
@ -28,18 +28,23 @@
</q-dialog>
</div>
<div v-else>
<q-card>
<pre
:class="runResultStatus ? '' : 'text-red'"
v-html="runResult"
></pre>
</q-card>
<div
v-show="!!runResult"
:class="{
'text-red': !runResultStatus,
'q-pa-md': 1,
}"
style="white-space: pre"
v-html="runResult"
></div>
</div>
</div>
</template>
<script>
import outputTypes from "../js/options/outputTypes.js";
export default {
data() {
return {
@ -54,28 +59,31 @@ export default {
mounted() {
window.runResult = this;
},
computed: {
fromUtools() {
return this.action.type !== "inPlugin";
},
},
methods: {
//
async runCurrentCommand(currentCommand) {
currentCommand.cmd = window.special(currentCommand.cmd);
currentCommand.cmd = await this.replaceTempInputVals(currentCommand.cmd);
let terminal = false;
let raw = true;
switch (currentCommand.output) {
case "html":
raw = false;
break;
case "terminal":
terminal = true;
break;
case "ignore":
utools.hideMainWindow();
break;
}
let { hideWindow, outPlugin, action } =
outputTypes[currentCommand.output];
//
hideWindow && utools.hideMainWindow();
// 退
// 退
this.fromUtools &&
outPlugin &&
setTimeout(() => {
utools.outPlugin();
}, 500);
if (currentCommand.program === "quickcommand") {
window.runCodeInVm(currentCommand.cmd, (stdout, stderr) => {
if (stderr) return this.showRunResult(stderr, raw, false);
this.showRunResult(stdout, raw, true);
if (stderr) return this.showRunResult(stderr, false, action);
this.showRunResult(stdout, true, action);
});
} else {
let option = this.$programmings[currentCommand.program];
@ -86,11 +94,10 @@ export default {
window.runCodeFile(
currentCommand.cmd,
option,
terminal,
currentCommand.output === "terminal",
(stdout, stderr) => {
if (terminal) return;
if (stderr) return this.showRunResult(stderr, raw, false);
this.showRunResult(stdout, raw, true);
if (stderr) return this.showRunResult(stderr, false, action);
this.showRunResult(stdout, true, action);
}
);
}
@ -122,7 +129,7 @@ export default {
return cmd;
},
//
showRunResult(content, raw, isSuccess) {
showRunResult(content, isSuccess, action) {
this.isResultShow = true;
this.runResultStatus = isSuccess;
let contlength = content.length;
@ -133,7 +140,24 @@ export default {
contlength - this.resultMaxLength - 100
} 字省略\n...\n\n` +
content.slice(contlength - 100);
this.runResult += htmlEncode(content, raw);
let pretreatment = action(content);
pretreatment && (this.runResult += pretreatment);
this.fromUtools &&
this.$nextTick(() => {
this.outputAutoHeight();
});
},
// utools
outputAutoHeight(autoScroll = true, autoHeight = true) {
let clientHeight = document.body.clientHeight;
let pluginHeight = clientHeight < 600 ? clientHeight : 600;
autoHeight && utools.setExpendHeight(pluginHeight);
autoScroll &&
window.scroll({
top: clientHeight,
left: 0,
behavior: "smooth",
});
},
},
};

View File

@ -6,42 +6,62 @@ const outputTypes = {
ignore: {
name: "ignore",
label: "忽略输出并隐藏",
icon: "more_horiz"
icon: "more_horiz",
hideWindow: true,
outPlugin: true,
action: result => null
},
nothing: {
name: "nothing",
label: "忽略输出且不隐藏",
icon: "blur_linear"
icon: "blur_linear",
outPlugin: true,
action: result => null
},
text: {
name: "text",
label: "纯文本输出",
icon: "text_snippet"
icon: "text_snippet",
action: result => window.htmlEncode(result)
},
html: {
name: "html",
label: "html格式输出",
icon: "html"
icon: "html",
action: result => result
},
terminal: {
name: "terminal",
label: "在终端显示",
icon: "terminal"
icon: "terminal",
hideWindow: true,
outPlugin: true,
action: result => null
},
clip: {
name: "clip",
label: "复制到剪贴板",
icon: "content_paste"
icon: "content_paste",
hideWindow: true,
outPlugin: true,
action: result => window.copyTo(result)
},
send: {
name: "send",
label: "发送到活动窗口",
icon: "web_asset"
icon: "web_asset",
hideWindow: true,
outPlugin: true,
action: result => window.send(result)
},
notice: {
name: "notice",
label: "发送系统通知",
icon: "sms"
icon: "sms",
hideWindow: true,
outPlugin: true,
action: result => window.message(result)
},
};