完善执行命令功能 90%

This commit is contained in:
fofolee 2022-04-10 00:38:56 +08:00
parent e86fb1b02a
commit 73c5dbacee
9 changed files with 200 additions and 140 deletions

View File

@ -255,10 +255,6 @@ let getSleepCodeByShell = ms => {
return cmd
}
let modWindowHeight = height => {
$('#options').is(':hidden') && utools.setExpendHeight(height > 600 ? 600 : height);
}
// 屏蔽危险函数
getuToolsLite = () => {
var utoolsLite = Object.assign({}, utools)
@ -564,6 +560,16 @@ getCurrentFolderPathFix = () => {
return pwdFix.replace(/\\/g, '\\\\')
}
getMatchedFilesFix = payload => {
let MatchedFiles = payload
let Matched = cmd.match(/\{\{MatchedFiles(\[\d+\]){0,1}(\.\w{1,11}){0,1}\}\}/g)
Matched && Matched.forEach(m => {
repl = eval(m.slice(2, -2))
typeof repl == 'object' ? (repl = JSON.stringify(repl)) : (repl = repl.replace('\\', '\\\\'))
cmd = cmd.replace(m, repl.replace('$', '$$$'))
})
}
saveFile = (content, file) => {
if (file instanceof Object) {
file = utools.showSaveDialog(file)
@ -580,16 +586,14 @@ yuQueClient = axios.create({
}
});
getSelectFile = hwnd =>
new Promise((reslove, reject) => {
getSelectFile = hwnd => {
if (utools.isWindows()) {
var cmd = `powershell.exe -NoProfile "(New-Object -COM 'Shell.Application').Windows() | Where-Object { $_.HWND -eq ${hwnd} } | Select-Object -Expand Document | select @{ n='SelectItems'; e={$_.SelectedItems()} } | select -Expand SelectItems | select -Expand Path "`;
child_process.exec(cmd, {
encoding: "buffer"
}, (err, stdout, stderr) => {
if (err) reject(stderr)
else reslove(iconv.decode(stdout, 'GBK').trim().replace(/\\/g, '/'));
let result = child_process.execSync(cmd, {
encoding: "buffer",
windowsHide: true
})
return iconv.decode(result, 'GBK').trim().replace(/\\/g, '/');
} else {
var cmd = `osascript -e 'tell application "Finder" to set selectedItems to selection as alias list
if selectedItems is {} then return
@ -600,45 +604,16 @@ getSelectFile = hwnd =>
end repeat
'
`
child_process.exec(cmd, (err, stdout, stderr) => {
if (err) reject(stderr)
else reslove(stdout.trim());
});
}
let result = child_process.execSync(cmd, {
encoding: "utf8",
windowsHide: true
})
console.log(result);
return result ? result.trim() : ""
}
}
clipboardReadText = () => {
return electron.clipboard.readText()
},
special = cmd => {
// 判断是否 windows 系统
if (cmd.includes('{{isWin}}')) {
let repl = utools.isWindows() ? 1 : 0;
cmd = cmd.replace(/\{\{isWin\}\}/mg, repl)
}
// 获取本机唯一ID
if (cmd.includes('{{LocalId}}')) {
let repl = utools.getNativeId();
cmd = cmd.replace(/\{\{LocalId\}\}/mg, repl)
}
// 获取浏览器当前链接
if (cmd.includes('{{BrowserUrl}}')) {
let repl = utools.getCurrentBrowserUrl();
cmd = cmd.replace(/\{\{BrowserUrl\}\}/mg, repl)
}
// 获取剪切板的文本
if (cmd.includes('{{ClipText}}')) {
let repl = clipboardReadText();
cmd = cmd.replace(/\{\{ClipText\}\}/mg, repl)
}
// 获取选中的文本
// if (cmd.includes('{{SelectText}}')) {
// let repl = getSelectText();
// cmd = cmd.replace(/\{\{SelectText\}\}/mg, repl)
// }
return cmd;
}
clipboardReadText = () => electron.clipboard.readText()
runCodeFile = (cmd, option, terminal, callback) => {
var bin = option.bin,

View File

@ -108,10 +108,9 @@
<span v-for="cmd in commandInfo.features.cmds" :key="cmd">
<span v-if="typeof cmd === 'string'">
<q-badge rounded :color="cmdBadgeColor()"
><q-icon
class="q-mr-xs"
:name="commandTypes.key.icon"
/>{{ getShortStrByByte(cmd) }}</q-badge
><q-icon class="q-mr-xs" :name="commandTypes.key.icon" />{{
getShortStrByByte(cmd)
}}</q-badge
>
<q-tooltip>
<div class="text-subtitle2">
@ -302,9 +301,11 @@ export default {
},
//
runCommand() {
utools.redirect(
this.commandInfo.features.cmds.filter((x) => x.length)[0]
);
let event = {
type: "run",
data: this.commandInfo.features.code,
};
this.$emit("commandChanged", event);
},
// /
toggleCommandActivated() {

View File

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

View File

@ -165,13 +165,8 @@
<q-item v-bind="scope.itemProps">
<q-item-section>
<q-item-label v-html="scope.opt.label" />
<q-tooltip v-if="!scope.opt.type">
注意需要自行在变量两边加上引号"{{ scope.opt.label }}"
</q-tooltip>
<q-tooltip v-else>
需要自行对json进行处理如json.loads(r"""{{
scope.opt.label
}}""")
<q-tooltip v-if="scope.opt.tooltip">
{{ scope.opt.tooltip }}
</q-tooltip>
<q-item-label caption>{{ scope.opt.desc }}</q-item-label>
</q-item-section>
@ -337,7 +332,7 @@ export default {
this.cmdMatch = `/${this.cmdMatch}/`;
},
insertSpecialVar(text) {
this.$parent.$refs.editor.repacleEditorSelection(text);
this.$parent.$refs.editor.repacleEditorSelection(`'${text}'`);
},
//
SaveMenuData() {

View File

@ -44,6 +44,7 @@
<script>
import outputTypes from "../js/options/outputTypes.js";
import specialVars from "../js/options/specialVars.js";
export default {
data() {
@ -67,8 +68,8 @@ export default {
methods: {
//
async runCurrentCommand(currentCommand) {
currentCommand.cmd = window.special(currentCommand.cmd);
currentCommand.cmd = await this.replaceTempInputVals(currentCommand.cmd);
currentCommand.cmd = this.assignSpecialVars(currentCommand.cmd);
// currentCommand.cmd = await this.replaceTempInputVars(currentCommand.cmd);
let { hideWindow, outPlugin, action } =
outputTypes[currentCommand.output];
//
@ -102,14 +103,24 @@ export default {
);
}
},
//
assignSpecialVars(cmd) {
let spVars = _.filter(specialVars, (sp) => sp.repl);
_.forIn(spVars, (val, key) => {
if (cmd.includes(val.label.slice(0, 12))) {
cmd = cmd.replace(val.match, (x) => val.repl(x));
}
});
return cmd;
},
//
async replaceTempInputVals(cmd) {
async replaceTempInputVars(cmd) {
let tempInputVals = [];
let needInputVal = [
"input",
"subinput",
"pwd",
"SelectFile",
// "SelectFile",
"WindowInfo",
"MatchedFiles",
];

View File

@ -2,115 +2,7 @@
* 所有的匹配类型
*/
const commandTypes = {
key: {
name: "key",
label: "关键词",
icon: "font_download",
matchLabel: "关键词",
desc: "在主输入框输入对应关键字进入插件,最通用的一种模式,关键字可以设置多个",
valueType: "array",
disabledSpecialVars: /{{input}}|{{SelectFile}}|{{pwd}}|{{WindowInfo}}|{{MatchedFiles}}/g,
matchToCmds: (rules, desc) => rules,
verify: (rules) => rules.length > 0 || "关键词不能为空",
},
regex: {
name: "regex",
label: "正则/划词",
matchLabel: "正则",
icon: "rule",
desc: "匹配主输入框或超级面板选中的文本,可以获取输入框文本或选中文本作为变量",
valueType: "regex",
disabledSpecialVars: /{{SelectFile}}|{{WindowInfo}}|{{pwd}}|{{MatchedFiles}}/g,
matchToCmds: (rules, desc) => [{
label: desc,
type: "regex",
match: rules,
minNum: 1,
}, ],
verify: rules => !!rules > 0 || "正则不能为空"
},
over: {
name: "over",
label: "所有文本",
matchLabel: "无需设置",
icon: "emergency",
desc: "匹配主输入框的所有文本,但只有在该文本未设置对应的插件或功能时才生效",
valueType: null,
disabledSpecialVars: /{{SelectFile}}|{{WindowInfo}}|{{pwd}}|{{MatchedFiles}}/g,
matchToCmds: (rules, desc) => [{
label: desc,
type: "over",
minNum: 1,
}],
verify: rules => true
},
window: {
name: "window",
label: "窗口/进程",
matchLabel: "进程名",
icon: "widgets",
desc: "匹配呼出uTools前或唤出超级面板时的活动窗口可以获取窗口的信息或文件夹路径作为变量",
valueType: "array",
disabledSpecialVars: /{{input}}|{{MatchedFiles}}/g,
matchToCmds: (rules, desc) => [{
type: "window",
label: desc,
match: {
"app": rules
}
}],
verify: rules => rules.length > 0 || "进程名不能为空"
},
img: {
name: "img",
label: "图片",
matchLabel: "无需配置",
icon: "panorama",
desc: "匹配主输入框或超级面板选中的图片,并返回图片的 base64",
valueType: null,
disabledSpecialVars: /{{input}}|{{SelectFile}}|{{pwd}}|{{WindowInfo}}|{{MatchedFiles}}/g,
matchToCmds: (rules, desc) => [{
label: desc,
type: "img",
}],
verify: rules => true
},
files: {
name: "files",
label: "复制/选中文件",
matchLabel: "正则",
icon: "description",
desc: "匹配主输入框或超级面板选中的文件,可以获取复制及选中的文件信息作为变量",
valueType: "regex",
disabledSpecialVars: /{{input}}|{{SelectFile}}|{{pwd}}|{{WindowInfo}}/g,
matchToCmds: (rules, desc) => [{
type: "files",
label: desc,
match: rules,
"minLength": 1,
}, ],
verify: rules => !!rules > 0 || "正则不能为空"
},
professional: {
name: "professional",
label: "专业模式",
matchLabel: "json配置",
icon: "construction",
desc: "通过json格式的配置实现同时匹配关键字、窗口、文件甚至图片或者指定文件数量、窗口类等",
valueType: "json",
disabledSpecialVars: null,
matchToCmds: (rules, desc) => JSON.parse(rules),
verify: rules => {
try {
JSON.parse(rules);
return true
} catch (error) {
return "专业模式json配置错误"
}
},
jsonSample: [
const jsonSample = [
"关键词",
{
"type": "img",
@ -152,7 +44,117 @@ const commandTypes = {
]
}
}
]
]
const commandTypes = {
key: {
name: "key",
label: "关键词",
icon: "font_download",
matchLabel: "关键词",
desc: "在主输入框输入对应关键字进入插件,最通用的一种模式,关键字可以设置多个",
valueType: "array",
disabledSpecialVars: /{{input}}|{{SelectFile}}|{{pwd}}|{{WindowInfo.*?}}|{{MatchedFiles.*?}}/g,
matchToCmds: (rules, desc) => rules,
verify: (rules) => !_.isEmpty(rules) || "关键词不能为空",
},
regex: {
name: "regex",
label: "正则/划词",
matchLabel: "正则",
icon: "rule",
desc: "匹配主输入框或超级面板选中的文本,可以获取输入框文本或选中文本作为变量",
valueType: "regex",
disabledSpecialVars: /{{SelectFile}}|{{WindowInfo.*?}}|{{pwd}}|{{MatchedFiles.*?}}/g,
matchToCmds: (rules, desc) => [{
label: desc,
type: "regex",
match: rules,
minNum: 1,
}, ],
verify: rules => !!rules > 0 || "正则不能为空"
},
over: {
name: "over",
label: "所有文本",
matchLabel: "无需设置",
icon: "emergency",
desc: "匹配主输入框的所有文本,但只有在该文本未设置对应的插件或功能时才生效",
valueType: null,
disabledSpecialVars: /{{SelectFile}}|{{WindowInfo.*?}}|{{pwd}}|{{MatchedFiles.*?}}/g,
matchToCmds: (rules, desc) => [{
label: desc,
type: "over",
minNum: 1,
}],
verify: rules => true
},
window: {
name: "window",
label: "窗口/进程",
matchLabel: "进程名",
icon: "widgets",
desc: "匹配呼出uTools前或唤出超级面板时的活动窗口可以获取窗口的信息或文件夹路径作为变量",
valueType: "array",
disabledSpecialVars: /{{input}}|{{MatchedFiles.*?}}/g,
matchToCmds: (rules, desc) => [{
type: "window",
label: desc,
match: {
"app": rules
}
}],
verify: rules => !_.isEmpty(rules) || "进程名不能为空"
},
img: {
name: "img",
label: "图片",
matchLabel: "无需配置",
icon: "panorama",
desc: "匹配主输入框或超级面板选中的图片,并返回图片的 base64",
valueType: null,
disabledSpecialVars: /{{input}}|{{SelectFile}}|{{pwd}}|{{WindowInfo.*?}}|{{MatchedFiles.*?}}/g,
matchToCmds: (rules, desc) => [{
label: desc,
type: "img",
}],
verify: rules => true
},
files: {
name: "files",
label: "复制/选中文件",
matchLabel: "正则",
icon: "description",
desc: "匹配主输入框或超级面板选中的文件,可以获取复制及选中的文件信息作为变量",
valueType: "regex",
disabledSpecialVars: /{{input}}|{{SelectFile}}|{{pwd}}|{{WindowInfo.*?}}/g,
matchToCmds: (rules, desc) => [{
type: "files",
label: desc,
match: rules,
"minLength": 1,
}, ],
verify: rules => !!rules > 0 || "正则不能为空"
},
professional: {
name: "professional",
label: "专业模式",
matchLabel: "json配置",
icon: "construction",
desc: "通过json格式的配置实现同时匹配关键字、窗口、文件甚至图片或者指定文件数量、窗口类等",
valueType: "json",
disabledSpecialVars: null,
matchToCmds: (rules, desc) => JSON.parse(rules),
verify: rules => {
try {
JSON.parse(rules);
return true
} catch (error) {
return "专业模式json配置错误"
}
},
jsonSample: jsonSample
}
}

View File

@ -2,64 +2,125 @@
* 所有的特殊变量
*/
let escapeItem = item => {
if (typeof item === 'number') return item
item = typeof item === 'object' ? JSON.stringify(item) : item.replace('\\', '\\\\')
return item.replace('$', '$$$')
}
let parseTheFirstLayerOfObjects = obj => {
let matched = /{{(\w+)(\[(\d+)\]){0,1}\.(\w+)}}/.exec(obj);
return matched ? {
obj: matched[1],
index: matched[3],
prop: matched[4],
} : {};
}
let handlingJsonVar = (jsonVar, srcObj) => {
try {
let parsed = parseTheFirstLayerOfObjects(jsonVar);
if (!parsed.obj) return escapeItem(srcObj)
else if (!parsed.index) return escapeItem(srcObj[parsed.prop])
else return escapeItem(srcObj[parsed.index][parsed.prop])
} catch {
return ""
}
}
const specialVars = {
isWin: {
name: "isWin",
label: "{{isWin}}",
desc: "是否为 windows 系统,返回 0 或 1",
disabledType: [],
match: /{{isWin}}/mg,
repl: () => utools.isWindows() ? 1 : 0
},
LocalId: {
name: "LocalId",
label: "{{LocalId}}",
desc: "本机唯一ID"
desc: "本机唯一ID",
disabledType: [],
match: /{{LocalId}}/mg,
repl: () => utools.getNativeId()
},
BrowserUrl: {
name: "BrowserUrl",
label: "{{BrowserUrl}}",
desc: "浏览器当前链接"
disabledType: [],
desc: "浏览器当前链接",
match: /{{BrowserUrl}}/mg,
repl: () => utools.getCurrentBrowserUrl()
},
ClipText: {
name: "ClipText",
label: "{{ClipText}}",
desc: "剪贴板内容"
disabledType: [],
desc: "剪贴板内容",
match: /{{ClipText}}/mg,
repl: () => window.clipboardReadText()
},
subinput: {
name: "subinput",
label: "{{subinput}}",
desc: "子输入框的文本"
disabledType: [],
tooltip: `可以自定义占位符,如{{subinput:请输入}}`,
desc: "子输入框的文本",
match: /{{subinput(:.+?){0,1}}}/mg,
},
input: {
name: "input",
label: "{{input}}",
desc: "主输入框的文本"
desc: "主输入框的文本",
match: /{{input}}/mg,
repl: () => quickcommand.enterData.payload
},
pwd: {
name: "pwd",
label: "{{pwd}}",
desc: "文件管理器当前目录"
desc: "文件管理器当前目录",
match: /{{pwd}}/mg,
repl: () => window.getCurrentFolderPathFix()
},
WindowInfo: {
name: "WindowInfo",
label: "{{WindowInfo}}",
desc: "当前窗口信息JSON格式字符串",
type: "json"
tooltip: `可以选择性读取其中的某一个属性,如{{WindowInfo.id}}`,
type: "json",
match: /{{WindowInfo(\.\w{1,7}){0,1}}}/mg,
repl: jsonVar => handlingJsonVar(jsonVar, quickcommand.enterData.payload)
},
SelectFile: {
name: "SelectFile",
label: "{{SelectFile}}",
desc: "文件管理器选中的文件不支持Linux"
desc: "文件管理器选中的文件不支持Linux",
match: /{{SelectFile}}/mg,
repl: () => window.getSelectFile(quickcommand.enterData.payload.id)
},
MatchedFiles: {
name: "MatchedFiles",
label: "{{MatchedFiles}}",
tooltip: `可以选择性读取其中的某一个属性,如{{MatchedFiles[0].path}}`,
desc: "匹配的文件JSON格式字符串",
type: "json"
type: "json",
match: /{{MatchedFiles(\[\d+\]){0,1}(\.\w{1,11}){0,1}}}/mg,
repl: jsonVar => handlingJsonVar(jsonVar, quickcommand.enterData.payload)
},
type: {
name: "type",
label: "{{type}}",
desc: "专业模式的type"
desc: "专业模式的type",
match: /{{type}}/mg,
repl: () => quickcommand.enterData.type
},
payload: {
name: "payload",
label: "{{payload}}",
desc: "专业模式的payload,JSON格式字符串",
type: "json"
desc: "专业模式的payload",
match: /{{payload}}/mg,
repl: () => escapeItem(quickcommand.enterData.payload)
}
}

View File

@ -29,7 +29,7 @@ export default {
},
methods: {
runCurrentCommand() {
this.$refs.result.runCurrentCommand(this.currentCommand);
this.$refs.result.runCurrentCommand(_.cloneDeep(this.currentCommand));
},
},
};

View File

@ -187,6 +187,10 @@
></CommandEditor>
</q-card>
</q-dialog>
<CommandRunResult
:action="{ type: 'inPlugin' }"
ref="result"
></CommandRunResult>
</div>
</template>
@ -195,6 +199,7 @@ import { defineAsyncComponent } from "vue";
import quickcommandParser from "../js/common/quickcommandParser.js";
import CommandCard from "components/CommandCard";
import ConfigurationMenu from "components/ConfigurationMenu.vue";
import CommandRunResult from "components/CommandRunResult.vue";
import importAll from "../js/common/importAll.js";
const CommandEditor = defineAsyncComponent(() =>
@ -209,6 +214,7 @@ export default {
CommandCard,
ConfigurationMenu,
CommandEditor,
CommandRunResult,
},
data() {
return {
@ -352,10 +358,19 @@ export default {
return;
case "edit":
this.editCommand(event.data);
return;
case "run":
this.runCommand(event.data);
return;
default:
return;
}
},
runCommand(code) {
this.$refs.result.runCurrentCommand(
_.cloneDeep(this.allQuickCommands[code])
);
},
//
enableCommand(code) {
this.$utools.whole.setFeature(