mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-08 22:51:25 +08:00
完善执行命令功能 80%
This commit is contained in:
parent
b36248c9bc
commit
94b19dce5d
@ -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, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """) : value
|
||||
htmlEncode = (value) => {
|
||||
return String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """)
|
||||
}
|
||||
|
||||
hexEncode = text => Buffer.from(text, 'utf8').toString('hex')
|
||||
|
@ -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);
|
||||
},
|
||||
},
|
||||
|
@ -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",
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
@ -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)
|
||||
},
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user