From c095084c352324ae6e341a14e47dcfac0f6fe1cc Mon Sep 17 00:00:00 2001 From: fofolee Date: Tue, 29 Dec 2020 19:41:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E8=84=9A=E6=9C=AC=E7=9A=84=E7=BC=96=E7=A0=81=E5=92=8C?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E7=9A=84=E8=A7=A3=E7=A0=81=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assets/index.js | 69 ++++++++++++++++++++++++++++++++------------- src/preload.js | 7 +++-- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/assets/index.js b/src/assets/index.js index 5158ad8..565588e 100644 --- a/src/assets/index.js +++ b/src/assets/index.js @@ -57,8 +57,7 @@ if (program == 'custom') customoptions = { custombin: $('#custombin').val(), customarg: $('#customarg').val(), - customext: $('#customext').val(), - customcodec: $('#customcodec').val() + customext: $('#customext').val() } putDB({ cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, CFG_PREFIX + 'codeHistory') } @@ -102,6 +101,7 @@ option = programs[db.program]; } option.scptarg = db.scptarg + option.charset = db.charset ? db.charset : autoCharset(db.program) cmd = special(cmd); if (cmd.includes('{{type}}')) { cmd = cmd.replace(/\{\{type\}\}/mg, type) @@ -414,21 +414,18 @@ bin: '', argv: '', ext: 'bat', - codec: 'gbk', color: '#C1F12E' }, powershell: { bin: 'powershell', argv: '-NoProfile -File', ext: 'ps1', - codec: utools.isWindows() ? 'gbk' : '', color: '#012456' }, python: { bin: 'python', argv: '-u', ext: 'py', - codec: utools.isWindows() ? 'gbk' : '', color: '#3572A5' }, javascript: { @@ -453,14 +450,12 @@ bin: 'gcc', argv: '-o', ext: 'c', - codec: utools.isWindows() ? 'gbk' : '', color: '#555555' }, csharp: { bin: 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe', argv: '/Nologo', ext: 'cs', - codec: 'gbk', color: '#178600' }, lua: { @@ -479,11 +474,19 @@ bin: '', argv: '', ext: '', - codec: '', color: '#438eff' } } + // 自动设置编码 + let autoCharset = program => { + let charset = { scriptCode: '', outputCode: '' } + if (!utools.isWindows()) return charset + if (program == 'powershell' || program == 'cmd') charset.scriptCode = 'GBK' + if (['cmd', 'powershell', 'python', 'c', 'csharp'].includes(program)) charset.outputCode = 'GBK' + return charset + } + let getCmdsType = cmds => { try { JSON.stringify(cmds) @@ -689,8 +692,8 @@ - + 编码设置 ﹢动作 ﹢按键 @@ -698,7 +701,7 @@ 格式化

- +

@@ -912,8 +915,9 @@ let mode = $('#program').val(); $('.customscript').hide(); $('.quickactions').hide(); - $('#scptarg').show(); + $('#scptarg, #charset').show(); $('#showInTerm').prop("disabled", false); + $('#charset').data(autoCharset(mode)); if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`); switch (mode) { case 'custom': @@ -923,7 +927,7 @@ break; case 'quickcommand': $('.quickactions').show(); - $('#scptarg').hide(); + $('#scptarg, #charset').hide(); $('#showInTerm').prop("disabled", true); mode = 'javascript'; break; @@ -1065,10 +1069,10 @@ $('#custombin').show().val(data.customOptions.bin); $('#customarg').show().val(data.customOptions.argv); $('#customext').show().val(data.customOptions.ext); - $('#customcodec').show().val(data.customOptions.codec); } typeCheck(); programCheck(); + if (data.charset) $("#charset").data(data.charset); // 分段载入,保障动画流畅 if (animate) { window.editor.setValue(data.cmd.slice(0, 2000)); @@ -1088,6 +1092,31 @@ editCurrentCommand(data) }) + + // 编码设置 + $("#options").on('click', '#charset', function () { + var html = ` + 脚本编码: + 输出解码: + ` + Swal.fire({ + title: "编码设置", + onBeforeOpen: () => { + let charset = $('#charset').data() + document.getElementById('scriptCode').value = charset.scriptCode || '' + document.getElementById('outputCode').value = charset.outputCode || '' + }, + html: html, + showCancelButton: true, + footer: `基于 iconv-lite, 查看支持的编码`, + preConfirm: () => { + let scriptCode = document.getElementById('scriptCode').value + let outputCode = document.getElementById('outputCode').value + $('#charset').data({scriptCode: scriptCode, outputCode: outputCode}) + } + }) + }) + // 添加模拟按键 $("#options").on('click', '#addKey', function () { $("#addKey").text("▶ 录制中").addClass('record') @@ -1471,6 +1500,7 @@ code = $("#code").val(), tags = $('#tags').val(), rule = $('#rule').val(), + charset = $('#charset').data() cmd = window.editor.getValue(); if (tags && tags.includes("默认") && !isDev()) return if (type != "window" && !rule) return quickcommand.showMessageBox(`${$('#ruleWord').text().replace(" ", "")} 不能留空!`, 'error') @@ -1554,7 +1584,8 @@ cmd: cmd, output: output, hasSubInput: hasSubInput, - scptarg: scptarg + scptarg: scptarg, + charset: charset } if (extraInfo) { Object.assign(pushData, extraInfo) @@ -1568,8 +1599,7 @@ pushData.customOptions = { "bin": $('#custombin').val(), "argv": $('#customarg').val(), - "ext": $('#customext').val(), - 'codec': $('#customcodec').val() + "ext": $('#customext').val() } } putDB(pushData, QC_PREFIX + code); @@ -1690,10 +1720,10 @@ if (program == "custom") option = { "bin": $('#custombin').val(), "argv": $('#customarg').val(), - "ext": $('#customext').val(), - 'codec': $('#customcodec').val() + "ext": $('#customext').val() } option.scptarg = $('#scptarg').val() + option.charset = $('#charset').data() runCodeFile(cmd, option, terminal, (stdout, stderr) => { if (terminal) return if (stderr) return showRunResult(stderr, raw, false) @@ -1731,9 +1761,9 @@ - 运 行 + 编码设置 格式化 @@ -1766,7 +1796,6 @@ $('#custombin').val(custom.custombin) $('#customarg').val(custom.customarg) $('#customext').val(custom.customext) - $('#customcodec').val(custom.customcodec) } } programCheck() diff --git a/src/preload.js b/src/preload.js index 5cce4dc..4f0f01c 100644 --- a/src/preload.js +++ b/src/preload.js @@ -776,10 +776,11 @@ runCodeFile = (cmd, option, terminal, callback) => { var bin = option.bin, argv = option.argv, ext = option.ext, + charset = option.charset, scptarg = option.scptarg || ""; let script = getQuickCommandScriptFile(ext) // 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题 - if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), 'GBK'); + if (charset.scriptCode) cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), charset.scriptCode); fs.writeFileSync(script, cmd); // var argvs = [script] // if (argv) { @@ -828,12 +829,12 @@ runCodeFile = (cmd, option, terminal, callback) => { // err_chunks = []; console.log('running: ' + cmdline); child.stdout.on('data', chunk => { - if (option.codec) chunk = iconv.decode(chunk, option.codec) + if (charset.outputCode) chunk = iconv.decode(chunk, charset.outputCode) callback(chunk.toString(), null) // chunks.push(chunk) }) child.stderr.on('data', stderr => { - if (option.codec) stderr = iconv.decode(stderr, option.codec) + if (charset.outputCode) stderr = iconv.decode(stderr, charset.outputCode) callback(null, stderr.toString()) // err_chunks.push(err_chunk) })