feat: 新增 quickcommand.runInTermial

This commit is contained in:
fofolee 2021-01-14 22:39:32 +08:00
parent 5d66f00449
commit 98ce15cbf1
2 changed files with 69 additions and 27 deletions

View File

@ -940,7 +940,7 @@
$('.customscript').hide(); $('.customscript').hide();
$('.quickactions').hide(); $('.quickactions').hide();
$('#scptarg, #charset').show(); $('#scptarg, #charset').show();
$('#showInTerm').prop("disabled", false); if (!utools.isLinux()) $('#showInTerm').prop("disabled", false)
$('#charset').data(autoCharset(mode)); $('#charset').data(autoCharset(mode));
if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`); if (!hasCustomIcon()) $("#icon").attr('src', `logo/${mode}.png`);
switch (mode) { switch (mode) {

View File

@ -371,7 +371,6 @@ quickcommand = {
} }
} }
// 运行vbs脚本 // 运行vbs脚本
if (process.platform == 'win32') quickcommand.runVbs = function (script) { if (process.platform == 'win32') quickcommand.runVbs = function (script) {
return new Promise((reslove, reject) => { 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 => { swalOneByOne = options => {
swal.getQueueStep() ? Swal.insertQueueStep(options) : Swal.queue([options]) swal.getQueueStep() ? Swal.insertQueueStep(options) : Swal.queue([options])
} }
@ -799,31 +840,32 @@ runCodeFile = (cmd, option, terminal, callback) => {
cmdline = `${bin} ${argv} "${script}" ${scptarg}` cmdline = `${bin} ${argv} "${script}" ${scptarg}`
} }
// 在终端中输出 // 在终端中输出
if (terminal) { if (terminal) cmdline = getCommandToLaunchTerminal(cmdline)
if (utools.isWindows()) { // if (terminal) {
if (bin.slice(-7) == 'csc.exe' || bin == 'gcc') { // if (utools.isWindows()) {
cmdline = cmdline.split("&&") // if (bin.slice(-7) == 'csc.exe' || bin == 'gcc') {
cmdline = cmdline[0] + "&& start cmd /k " + cmdline[1] // cmdline = cmdline.split("&&")
} else { // cmdline = cmdline[0] + "&& start cmd /k " + cmdline[1]
cmdline = `start cmd /k ${cmdline}` // } else {
} // cmdline = `start cmd /k ${cmdline}`
} else if(utools.isMacOs()){ // }
var appleScript = `if application "Terminal" is running then // } else if(utools.isMacOs()){
tell application "Terminal" // var appleScript = `if application "Terminal" is running then
do script "clear;${cmdline.replace(/"/g, `\\"`)}" // tell application "Terminal"
activate // do script "clear;${cmdline.replace(/"/g, `\\"`)}"
end tell // activate
else // end tell
tell application "Terminal" // else
do script "clear;${cmdline.replace(/"/g, `\\"`)}" in window 1 // tell application "Terminal"
activate // do script "clear;${cmdline.replace(/"/g, `\\"`)}" in window 1
end tell // activate
end if`; // end tell
cmdline = `osascript -e '${appleScript}'` // end if`;
} else { // cmdline = `osascript -e '${appleScript}'`
return message('Linux 不支持在终端输出') // } else {
} // return message('Linux 不支持在终端输出')
} // }
// }
child = child_process.spawn(cmdline, { encoding: 'buffer', shell: true }) child = child_process.spawn(cmdline, { encoding: 'buffer', shell: true })
// var chunks = [], // var chunks = [],
// err_chunks = []; // err_chunks = [];