添加脚本参数

This commit is contained in:
fofolee 2020-06-25 20:14:06 +08:00
parent a5610f367b
commit f365f7986b
4 changed files with 75 additions and 28 deletions

View File

@ -1,12 +1,20 @@
utools.onPluginEnter(async ({ code, type, payload }) => { utools.onPluginEnter(async ({ code, type, payload }) => {
utools.onPluginOut(() => { utools.onPluginOut(() => {
var program = $('#program').val();
$("#options").empty()
$("#out").empty()
if (code == "code") { if (code == "code") {
var cmd = window.editor.getValue(); var cmd = window.editor.getValue();
putDB('history', { cmd: cmd, program: program }, 'codeHistory') var program = $('#program').val(),
scptarg = $('#scptarg').val(),
customoptions;
if (program == 'custom') customoptions = {
custombin: $('#custombin').val(),
customarg: $('#customarg').val(),
customext: $('#customext').val(),
customcodec: $('#customcodec').val()
}
putDB('history', { cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, 'codeHistory')
} }
$("#options").empty()
$("#out").empty()
}) })
// 配置页面 // 配置页面
if (code == 'options') { if (code == 'options') {
@ -31,6 +39,7 @@ utools.onPluginEnter(async ({ code, type, payload }) => {
}else{ }else{
option = programs[db.program]; option = programs[db.program];
} }
option.scptarg = db.scptarg
cmd = special(cmd); cmd = special(cmd);
// 正则 // 正则
if (type == 'regex') cmd = cmd.replace(/\{\{input\}\}/mg, payload); if (type == 'regex') cmd = cmd.replace(/\{\{input\}\}/mg, payload);

View File

@ -309,9 +309,10 @@ let showCustomize = () => {
</p> </p>
<p> <p>
<span class="word">&#12288;</span> <span class="word">&#12288;</span>
<span><input type="text" id="scptarg" placeholder="脚本参数"></span>
<span class="customscript"> <span class="customscript">
<input type="text" id="custombin" placeholder="解释器绝对路径"> <input type="text" id="custombin" placeholder="解释器路径">
<input type="text" id="customarg" placeholder="参数"> <input type="text" id="customarg" placeholder="解释器参数">
<input type="text" id="customext" placeholder="后缀,不含." onchange="highlightIfKnown(this.value)"> <input type="text" id="customext" placeholder="后缀,不含." onchange="highlightIfKnown(this.value)">
<input type="text" id="customcodec" placeholder="输出编码"> <input type="text" id="customcodec" placeholder="输出编码">
</span> </span>
@ -497,14 +498,18 @@ let programCheck = () => {
let mode = $('#program').val(); let mode = $('#program').val();
$('.customscript').hide(); $('.customscript').hide();
$('.simulation').hide(); $('.simulation').hide();
$('#scptarg').show();
$('#showInTerm').prop("disabled", false); $('#showInTerm').prop("disabled", false);
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`); if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
switch (mode) { switch (mode) {
case 'custom': case 'custom':
$('.customscript').show(); $('.customscript').show();
var customext = $('#customext').val()
customext && (mode = highlightIfKnown(customext))
break; break;
case 'simulation': case 'simulation':
$('.simulation').show(); $('.simulation').show();
$('#scptarg').hide();
$('#showInTerm').prop("disabled", true); $('#showInTerm').prop("disabled", true);
mode = 'javascript'; mode = 'javascript';
break; break;
@ -578,6 +583,7 @@ $("#options").on('click', '.editBtn', function () {
$('#program').val(data.program).trigger("change"); $('#program').val(data.program).trigger("change");
$('#output').val(data.output).trigger("change"); $('#output').val(data.output).trigger("change");
$('#desc').val(data.features.explain); $('#desc').val(data.features.explain);
$('#scptarg').val(data.scptarg);
$("#icon").attr('src', data.features.icon); $("#icon").attr('src', data.features.icon);
let mode = data.program; let mode = data.program;
if (mode == 'custom') { if (mode == 'custom') {
@ -718,6 +724,7 @@ let SaveCurrentCommand = async () => {
var code = `${type}_${uid}`; var code = `${type}_${uid}`;
} }
var output = $('#output').val(); var output = $('#output').val();
var scptarg = $('#scptarg').val();
var cmd = window.editor.getValue(); var cmd = window.editor.getValue();
// 合规性校验 // 合规性校验
if (type == 'key' if (type == 'key'
@ -812,7 +819,8 @@ let SaveCurrentCommand = async () => {
program: program, program: program,
cmd: cmd, cmd: cmd,
output: output, output: output,
hasSubInput: hasSubInput hasSubInput: hasSubInput,
scptarg: scptarg
} }
if (tags) pushData.tags = tags if (tags) pushData.tags = tags
if (program == 'custom') { if (program == 'custom') {
@ -933,6 +941,7 @@ let runCurrentCommand = async () => {
"ext": $('#customext').val(), "ext": $('#customext').val(),
'codec': $('#customcodec').val() 'codec': $('#customcodec').val()
} }
option.scptarg = $('#scptarg').val()
runCodeFile(cmd, option, terminal, (stdout, stderr) => { runCodeFile(cmd, option, terminal, (stdout, stderr) => {
if (terminal) return if (terminal) return
if (stderr) return showRunResult(stderr, raw, false) if (stderr) return showRunResult(stderr, raw, false)
@ -955,30 +964,35 @@ let quitCurrentCommand = () => {
} }
let highlightIfKnown = ext => { let highlightIfKnown = ext => {
// 未设置后缀时有自动补全bug
var lang = Object.keys(programs).filter(p => programs[p].ext == ext) var lang = Object.keys(programs).filter(p => programs[p].ext == ext)
if (lang.length) window.editor.setOption("mode", lang[0]) if (lang.length) {
if (lang[0] == 'python') getPythonMods()
window.editor.setOption("mode", lang[0])
return lang[0]
}
} }
showCodeEditor = file => { showCodeEditor = file => {
let options = `<option>${Object.keys(programs).join('</option><option>')}</option>` let options = `<option>${Object.keys(programs).join('</option><option>')}</option>`
var customWindow = `<div id="customize"> var customWindow = `
<select id="program"> <div id="customize">
<select id="program">
<option value="simulation">内置环境</option> <option value="simulation">内置环境</option>
${options} ${options}
</select> </select>
<span class="customscript"> <span class="customscript">
<input type="text" id="custombin" placeholder="解释器绝对路径"> <input type="text" id="custombin" placeholder="解释器路径">
<input type="text" id="customarg" placeholder="参数"> <input type="text" id="customarg" placeholder="解释器参数">
<input type="text" id="customext" placeholder="后缀,不含." onchange="highlightIfKnown(this.value)"> <input type="text" id="customext" placeholder="后缀,不含." onchange="highlightIfKnown(this.value)">
<input type="text" id="customcodec" placeholder="输出编码"> <input type="text" id="customcodec" placeholder="输出编码">
</span> </span>
<span id="runCode" class="footBtn robot"> </span> <span id="runCode" class="footBtn robot"> </span>
<input type="text" id="scptarg" placeholder="脚本参数">
<span class="simulation"> <span class="simulation">
<span id="beautifyCode" class="footBtn robot">格式化</span> <span id="beautifyCode" class="footBtn robot">格式化</span>
<span id="addAction" class="footBtn robot">动作</span> <span id="addAction" class="footBtn robot">动作</span>
<span id="addKey" class="footBtn robot">按键</span> <span id="addKey" class="footBtn robot">按键</span>
<span id="showHelp" class="footBtn robot">帮助</span> <span id="showHelp" class="footBtn robot">帮助</span>
</span> </span>
<textarea id="cmd" placeholder="可以直接拖放脚本文件至此处, 支持VSCode快捷键\nAlt+Enter 全屏\nCtrl+B 运行\nCtrl+F 搜索\nShift+Alt+F 格式化仅JS/PY"></textarea> <textarea id="cmd" placeholder="可以直接拖放脚本文件至此处, 支持VSCode快捷键\nAlt+Enter 全屏\nCtrl+B 运行\nCtrl+F 搜索\nShift+Alt+F 格式化仅JS/PY"></textarea>
</div> </div>
@ -995,11 +1009,20 @@ showCodeEditor = file => {
var fileinfo = getFileInfo({ type: 'file', argvs: file, readfile: true }) var fileinfo = getFileInfo({ type: 'file', argvs: file, readfile: true })
console.log(fileinfo); console.log(fileinfo);
window.editor.setValue(fileinfo.data) window.editor.setValue(fileinfo.data)
var program = Object.keys(programs).filter(x => `.${programs[x].ext}` == fileinfo.ext)[0] var program = Object.keys(programs).filter(x => `.${programs[x].ext}` == fileinfo.ext)
$('#program').val(program) if (program) $('#program').val(program[0])
runCurrentCommand() runCurrentCommand()
} else { } else if(db.history){
db.history && $('#program').val(db.history.program) && window.editor.setValue(db.history.cmd) window.editor.setValue(db.history.cmd)
$('#program').val(db.history.program)
$('#scptarg').val(db.history.scptarg)
var custom = db.history.customoptions
if (db.history.program = 'custom' && custom) {
$('#custombin').val(custom.custombin)
$('#customarg').val(custom.customarg)
$('#customext').val(custom.customext)
$('#customcodec').val(custom.customcodec)
}
} }
programCheck() programCheck()
$('#program').select2({ $('#program').select2({

View File

@ -335,7 +335,7 @@
#options #customize span.customscript > input { #options #customize span.customscript > input {
margin-left: 5px; margin-left: 5px;
width: 16%; width: 12%;
} }
#options #featureList { #options #featureList {
@ -391,6 +391,20 @@
width: 40%; width: 40%;
} }
#options #customize input#scptarg{
width: 170px;
float: right;
background: white;
outline: none;
border: 2px solid #3085d6;
height: 30px;
border-radius: 4px;
padding: 2px 5px;
margin: 0px 5px;
text-align: center;
display: none
}
#options #customize input.customize:hover{ #options #customize input.customize:hover{
border-bottom-color: #9e9e9ec7; border-bottom-color: #9e9e9ec7;
transition: 0.25s; transition: 0.25s;

View File

@ -497,7 +497,8 @@ special = cmd => {
runCodeFile = (cmd, option, terminal, callback) => { runCodeFile = (cmd, option, terminal, callback) => {
var bin = option.bin, var bin = option.bin,
argv = option.argv, argv = option.argv,
ext = option.ext; ext = option.ext,
scptarg = option.scptarg || "";
let script = getQuickCommandScriptFile(ext) let script = getQuickCommandScriptFile(ext)
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题 // 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), 'GBK'); if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), 'GBK');
@ -509,14 +510,14 @@ runCodeFile = (cmd, option, terminal, callback) => {
// } // }
var child, cmdline var child, cmdline
if (bin.slice(-7) == 'csc.exe') { if (bin.slice(-7) == 'csc.exe') {
cmdline = `${bin} ${argv} /out:"${script.slice(0, -2) + 'exe'}" "${script}" && "${script.slice(0, -2) + 'exe'}"` cmdline = `${bin} ${argv} /out:"${script.slice(0, -2) + 'exe'}" "${script}" && "${script.slice(0, -2) + 'exe'}" ${scptarg}`
} else if (bin == 'gcc') { } else if (bin == 'gcc') {
var suffix = utools.isWindows() ? '.exe' : '' var suffix = utools.isWindows() ? '.exe' : ''
cmdline = `${bin} ${argv} "${script.slice(0, -2)}" "${script}" && "${script.slice(0, -2) + suffix}"` cmdline = `${bin} ${argv} "${script.slice(0, -2)}" "${script}" && "${script.slice(0, -2) + suffix}" ${scptarg}`
} else if (utools.isWindows() && bin == 'bash') { } else if (utools.isWindows() && bin == 'bash') {
cmdline = `${bin} ${argv} "${script.replace(/\\/g, '/').replace(/C:/i, '/mnt/c')}"` cmdline = `${bin} ${argv} "${script.replace(/\\/g, '/').replace(/C:/i, '/mnt/c')}" ${scptarg}`
} else { } else {
cmdline = `${bin} ${argv} "${script}"` cmdline = `${bin} ${argv} "${script}" ${scptarg}`
} }
// 在终端中输出 // 在终端中输出
if (terminal) { if (terminal) {