mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-09 06:54:11 +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 getCommandToLaunchTerminal = (cmdline, dir) => {
|
||||||
let cd = ''
|
let cd = ''
|
||||||
if (utools.isWindows()) {
|
if (utools.isWindows()) {
|
||||||
let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/')
|
let appPath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/')
|
||||||
// 直接 existsSync wt.exe 无效
|
// 直接 existsSync wt.exe 无效
|
||||||
if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) {
|
if (fs.existsSync(appPath) && fs.readdirSync(appPath).includes('wt.exe')) {
|
||||||
cmdline = cmdline.replace(/"/g, `\\"`)
|
cmdline = cmdline.replace(/"/g, `\\"`)
|
||||||
if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
|
if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"`
|
||||||
command = `${appPath}wt.exe ${cd} cmd /k "${cmdline}"`
|
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 {
|
} else {
|
||||||
cmdline = cmdline.replace(/"/g, `^"`)
|
cmdline = cmdline.replace(/"/g, `\\"`)
|
||||||
if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&`
|
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
|
||||||
command = `${cd} start "" cmd /k "${cmdline}"`
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cmdline = cmdline.replace(/"/g, `\\"`)
|
|
||||||
if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&`
|
|
||||||
if (fs.existsSync('/Applications/iTerm.app')) {
|
if (fs.existsSync('/Applications/iTerm.app')) {
|
||||||
command = `osascript -e 'tell application "iTerm"
|
command = `osascript -e 'tell application "iTerm"
|
||||||
create window with default profile
|
create window with default profile
|
||||||
@ -473,8 +473,8 @@ getNodeJsCommand = () => {
|
|||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlEncode = (value, raw = true) => {
|
htmlEncode = (value) => {
|
||||||
return raw ? String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """) : value
|
return String(value).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """)
|
||||||
}
|
}
|
||||||
|
|
||||||
hexEncode = text => Buffer.from(text, 'utf8').toString('hex')
|
hexEncode = text => Buffer.from(text, 'utf8').toString('hex')
|
||||||
|
@ -352,7 +352,9 @@ export default {
|
|||||||
},
|
},
|
||||||
// 运行
|
// 运行
|
||||||
runCurrentCommand() {
|
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);
|
this.$refs.result.runCurrentCommand(this.quickcommandInfo);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<div v-if="action.type === 'inPlugin'">
|
<div v-if="!fromUtools">
|
||||||
<q-dialog v-model="isResultShow" @hide="runResult = ''" position="bottom">
|
<q-dialog v-model="isResultShow" @hide="runResult = ''" position="bottom">
|
||||||
<q-card style="width: 90vh">
|
<q-card style="width: 90vh">
|
||||||
<q-toolbar>
|
<q-toolbar>
|
||||||
@ -28,18 +28,23 @@
|
|||||||
</q-dialog>
|
</q-dialog>
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<q-card>
|
<div
|
||||||
<pre
|
v-show="!!runResult"
|
||||||
:class="runResultStatus ? '' : 'text-red'"
|
:class="{
|
||||||
v-html="runResult"
|
'text-red': !runResultStatus,
|
||||||
></pre>
|
'q-pa-md': 1,
|
||||||
</q-card>
|
}"
|
||||||
|
style="white-space: pre"
|
||||||
|
v-html="runResult"
|
||||||
|
></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import outputTypes from "../js/options/outputTypes.js";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -54,28 +59,31 @@ export default {
|
|||||||
mounted() {
|
mounted() {
|
||||||
window.runResult = this;
|
window.runResult = this;
|
||||||
},
|
},
|
||||||
|
computed: {
|
||||||
|
fromUtools() {
|
||||||
|
return this.action.type !== "inPlugin";
|
||||||
|
},
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
// 运行命令
|
// 运行命令
|
||||||
async runCurrentCommand(currentCommand) {
|
async runCurrentCommand(currentCommand) {
|
||||||
currentCommand.cmd = window.special(currentCommand.cmd);
|
currentCommand.cmd = window.special(currentCommand.cmd);
|
||||||
currentCommand.cmd = await this.replaceTempInputVals(currentCommand.cmd);
|
currentCommand.cmd = await this.replaceTempInputVals(currentCommand.cmd);
|
||||||
let terminal = false;
|
let { hideWindow, outPlugin, action } =
|
||||||
let raw = true;
|
outputTypes[currentCommand.output];
|
||||||
switch (currentCommand.output) {
|
// 需要隐藏的提前隐藏窗口
|
||||||
case "html":
|
hideWindow && utools.hideMainWindow();
|
||||||
raw = false;
|
// 对于本身就没有输出的命令,无法确认命令是否执行完成,所以干脆提前退出插件
|
||||||
break;
|
// 弊端就是如果勾选了隐藏后台就完全退出的话,会造成命令直接中断
|
||||||
case "terminal":
|
this.fromUtools &&
|
||||||
terminal = true;
|
outPlugin &&
|
||||||
break;
|
setTimeout(() => {
|
||||||
case "ignore":
|
utools.outPlugin();
|
||||||
utools.hideMainWindow();
|
}, 500);
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (currentCommand.program === "quickcommand") {
|
if (currentCommand.program === "quickcommand") {
|
||||||
window.runCodeInVm(currentCommand.cmd, (stdout, stderr) => {
|
window.runCodeInVm(currentCommand.cmd, (stdout, stderr) => {
|
||||||
if (stderr) return this.showRunResult(stderr, raw, false);
|
if (stderr) return this.showRunResult(stderr, false, action);
|
||||||
this.showRunResult(stdout, raw, true);
|
this.showRunResult(stdout, true, action);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let option = this.$programmings[currentCommand.program];
|
let option = this.$programmings[currentCommand.program];
|
||||||
@ -86,11 +94,10 @@ export default {
|
|||||||
window.runCodeFile(
|
window.runCodeFile(
|
||||||
currentCommand.cmd,
|
currentCommand.cmd,
|
||||||
option,
|
option,
|
||||||
terminal,
|
currentCommand.output === "terminal",
|
||||||
(stdout, stderr) => {
|
(stdout, stderr) => {
|
||||||
if (terminal) return;
|
if (stderr) return this.showRunResult(stderr, false, action);
|
||||||
if (stderr) return this.showRunResult(stderr, raw, false);
|
this.showRunResult(stdout, true, action);
|
||||||
this.showRunResult(stdout, raw, true);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -122,7 +129,7 @@ export default {
|
|||||||
return cmd;
|
return cmd;
|
||||||
},
|
},
|
||||||
// 显示运行结果
|
// 显示运行结果
|
||||||
showRunResult(content, raw, isSuccess) {
|
showRunResult(content, isSuccess, action) {
|
||||||
this.isResultShow = true;
|
this.isResultShow = true;
|
||||||
this.runResultStatus = isSuccess;
|
this.runResultStatus = isSuccess;
|
||||||
let contlength = content.length;
|
let contlength = content.length;
|
||||||
@ -133,7 +140,24 @@ export default {
|
|||||||
contlength - this.resultMaxLength - 100
|
contlength - this.resultMaxLength - 100
|
||||||
} 字省略\n...\n\n` +
|
} 字省略\n...\n\n` +
|
||||||
content.slice(contlength - 100);
|
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: {
|
ignore: {
|
||||||
name: "ignore",
|
name: "ignore",
|
||||||
label: "忽略输出并隐藏",
|
label: "忽略输出并隐藏",
|
||||||
icon: "more_horiz"
|
icon: "more_horiz",
|
||||||
|
hideWindow: true,
|
||||||
|
outPlugin: true,
|
||||||
|
action: result => null
|
||||||
},
|
},
|
||||||
nothing: {
|
nothing: {
|
||||||
name: "nothing",
|
name: "nothing",
|
||||||
label: "忽略输出且不隐藏",
|
label: "忽略输出且不隐藏",
|
||||||
icon: "blur_linear"
|
icon: "blur_linear",
|
||||||
|
outPlugin: true,
|
||||||
|
action: result => null
|
||||||
},
|
},
|
||||||
text: {
|
text: {
|
||||||
name: "text",
|
name: "text",
|
||||||
label: "纯文本输出",
|
label: "纯文本输出",
|
||||||
icon: "text_snippet"
|
icon: "text_snippet",
|
||||||
|
action: result => window.htmlEncode(result)
|
||||||
},
|
},
|
||||||
html: {
|
html: {
|
||||||
name: "html",
|
name: "html",
|
||||||
label: "html格式输出",
|
label: "html格式输出",
|
||||||
icon: "html"
|
icon: "html",
|
||||||
|
action: result => result
|
||||||
},
|
},
|
||||||
terminal: {
|
terminal: {
|
||||||
name: "terminal",
|
name: "terminal",
|
||||||
label: "在终端显示",
|
label: "在终端显示",
|
||||||
icon: "terminal"
|
icon: "terminal",
|
||||||
|
hideWindow: true,
|
||||||
|
outPlugin: true,
|
||||||
|
action: result => null
|
||||||
|
|
||||||
},
|
},
|
||||||
clip: {
|
clip: {
|
||||||
name: "clip",
|
name: "clip",
|
||||||
label: "复制到剪贴板",
|
label: "复制到剪贴板",
|
||||||
icon: "content_paste"
|
icon: "content_paste",
|
||||||
|
hideWindow: true,
|
||||||
|
outPlugin: true,
|
||||||
|
action: result => window.copyTo(result)
|
||||||
},
|
},
|
||||||
send: {
|
send: {
|
||||||
name: "send",
|
name: "send",
|
||||||
label: "发送到活动窗口",
|
label: "发送到活动窗口",
|
||||||
icon: "web_asset"
|
icon: "web_asset",
|
||||||
|
hideWindow: true,
|
||||||
|
outPlugin: true,
|
||||||
|
action: result => window.send(result)
|
||||||
},
|
},
|
||||||
notice: {
|
notice: {
|
||||||
name: "notice",
|
name: "notice",
|
||||||
label: "发送系统通知",
|
label: "发送系统通知",
|
||||||
icon: "sms"
|
icon: "sms",
|
||||||
|
hideWindow: true,
|
||||||
|
outPlugin: true,
|
||||||
|
action: result => window.message(result)
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user