From 98ce15cbf11ee9689de6575d20a60d642869c8a6 Mon Sep 17 00:00:00 2001 From: fofolee Date: Thu, 14 Jan 2021 22:39:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20quickcommand.runIn?= =?UTF-8?q?Termial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assets/index.js | 2 +- src/preload.js | 94 ++++++++++++++++++++++++++++++++------------- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/assets/index.js b/src/assets/index.js index 99c239b..38c39a8 100644 --- a/src/assets/index.js +++ b/src/assets/index.js @@ -940,7 +940,7 @@ $('.customscript').hide(); $('.quickactions').hide(); $('#scptarg, #charset').show(); - $('#showInTerm').prop("disabled", false); + if (!utools.isLinux()) $('#showInTerm').prop("disabled", false) $('#charset').data(autoCharset(mode)); if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`); switch (mode) { diff --git a/src/preload.js b/src/preload.js index b9e4278..9a47b03 100644 --- a/src/preload.js +++ b/src/preload.js @@ -371,7 +371,6 @@ quickcommand = { } } - // 运行vbs脚本 if (process.platform == 'win32') quickcommand.runVbs = function (script) { return new Promise((reslove, reject) => { @@ -387,6 +386,48 @@ if (process.platform == 'win32') quickcommand.runVbs = function (script) { }) } + +// 在终端中执行 +if (process.platform !== 'linux') quickcommand.runInTerminal = function (cmdline, dir) { + let command = getCommandToLaunchTerminal(cmdline, dir) + child_process.exec(command) +} + +let getCommandToLaunchTerminal = (cmdline, dir) => { + let cd = '' + if (utools.isWindows()) { + let wtpath = path.join(utools.getPath('home'), '/AppData/Local/Microsoft/WindowsApps/wt.exe') + if (fs.existsSync(wtpath)) { + if (dir) cd = `-d "${dir.replace(/\\/g, '/')}"` + command = `wt ${cd} cmd /k "${cmdline}"` + } else { + if (dir) cd = `cd /d "${dir.replace(/\\/g, '/')}" &&` + command = `${cd} start "" cmd /k "${cmdline}"` + } + } else { + let iterm + if (fs.existsSync('/Applications/iTerm 2.app')) { + iterm = 'iTerm 2' + } else if (fs.existsSync('/Applications/iTerm.app')) { + iterm = 'iTerm' + } + if (dir) cd = `cd ${dir.replace(/ /g, `\\\\ `)} &&` + cmdline = cmdline.replace(/"/g, `\\"`) + if (iterm) { + command = `osascript -e 'tell application "${iterm}" + create window with default profile + tell current session of current window to write text "clear && ${cd} ${cmdline}" + end tell'` + } else { + command = `osascript -e 'tell application "Terminal" + do script "clear && ${cd} ${cmdline}" + activate + end tell'` + } + } + return command +} + swalOneByOne = options => { swal.getQueueStep() ? Swal.insertQueueStep(options) : Swal.queue([options]) } @@ -799,31 +840,32 @@ runCodeFile = (cmd, option, terminal, callback) => { cmdline = `${bin} ${argv} "${script}" ${scptarg}` } // 在终端中输出 - if (terminal) { - if (utools.isWindows()) { - if (bin.slice(-7) == 'csc.exe' || bin == 'gcc') { - cmdline = cmdline.split("&&") - cmdline = cmdline[0] + "&& start cmd /k " + cmdline[1] - } else { - cmdline = `start cmd /k ${cmdline}` - } - } else if(utools.isMacOs()){ - var appleScript = `if application "Terminal" is running then - tell application "Terminal" - do script "clear;${cmdline.replace(/"/g, `\\"`)}" - activate - end tell - else - tell application "Terminal" - do script "clear;${cmdline.replace(/"/g, `\\"`)}" in window 1 - activate - end tell - end if`; - cmdline = `osascript -e '${appleScript}'` - } else { - return message('Linux 不支持在终端输出') - } - } + if (terminal) cmdline = getCommandToLaunchTerminal(cmdline) + // if (terminal) { + // if (utools.isWindows()) { + // if (bin.slice(-7) == 'csc.exe' || bin == 'gcc') { + // cmdline = cmdline.split("&&") + // cmdline = cmdline[0] + "&& start cmd /k " + cmdline[1] + // } else { + // cmdline = `start cmd /k ${cmdline}` + // } + // } else if(utools.isMacOs()){ + // var appleScript = `if application "Terminal" is running then + // tell application "Terminal" + // do script "clear;${cmdline.replace(/"/g, `\\"`)}" + // activate + // end tell + // else + // tell application "Terminal" + // do script "clear;${cmdline.replace(/"/g, `\\"`)}" in window 1 + // activate + // end tell + // end if`; + // cmdline = `osascript -e '${appleScript}'` + // } else { + // return message('Linux 不支持在终端输出') + // } + // } child = child_process.spawn(cmdline, { encoding: 'buffer', shell: true }) // var chunks = [], // err_chunks = [];