解决批处理换行符BUG,输出方式增加在终端显示,改进获取选中文件的方法

This commit is contained in:
fofolee 2020-04-09 18:06:14 +08:00
parent 02f4f06088
commit 3764db3f50

View File

@ -16,10 +16,9 @@ logo = nativeImage.createFromPath(path.join(__dirname, 'logo.png'));
// fix PATH
process.env.PATH += ':/usr/local/bin:/usr/local/sbin'
messageBox = (options, callback) => {
messageBox = options => {
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), options, index => {
utools.showMainWindow()
callback(index);
})
}
@ -119,33 +118,55 @@ restoreClip = historyData => {
}
getSelectText = () => {
var historyData = storeClip()
var historyData = storeClip();
clipboard.writeText('');
copy();
var selectText = clipboard.readText()
restoreClip(historyData)
return selectText
}
getSelectFile = () => {
var historyData = storeClip()
copy();
var filePath;
if (isWin) {
filePath = clipboard.readBuffer('FileNameW').toString('ucs2');
filePath = filePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
getSelectFile = () =>
new Promise((reslove, reject) => {
if (isWin) {
GetForegroundWindow(hwnd => {
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 "`;
exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
if (err) reject(stderr)
reslove(iconv.decode(stdout, '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
set parentPath to do shell script "dirname " & quoted form of POSIX path of (item 1 of selectedItems)
set pathData to ""
repeat with theItem in selectedItems
set pathData to pathData & POSIX path of theItem & linefeed
end repeat
set stdout to pathData
'
`
exec(cmd, (err, stdout, stderr) => {
if (err) reject(stderr)
reslove(stdout.trim());
});
}
})
GetBinPath = () => {
if (isDev) {
return path.join(__dirname.replace(/(unsafe-\w+\.asar)/,'$1.unpacked'), 'bin', 'GetForegroundWindow.exe')
} else {
filePath = clipboard.read('public.file-url').replace('file://', '');
return path.join(__dirname, 'bin', 'GetForegroundWindow.exe')
}
restoreClip(historyData)
return filePath;
}
// 获取前台窗口句柄
GetForegroundWindow = callback =>
exec(`"${path.join(__dirname, 'bin', 'GetForegroundWindow.exe')}"`, (error, stdout, stderr) => {
exec(`"${GetBinPath()}"`, (error, stdout, stderr) => {
callback(stdout);
});
pwd = () =>
@ -155,7 +176,7 @@ pwd = () =>
var cmd = `powershell.exe -NoProfile "((New-Object -COM 'Shell.Application').Windows() | Where-Object { $_.HWND -eq (${hwnd}) } | Select-Object -Expand LocationURL).replace('file:///','')"`;
exec(cmd, { encoding: "buffer" }, (err, stdout, stderr) => {
if (err) {
console.log(stderr);
console.log(iconv.decode(stderr, 'GBK'));
reslove(`${os.homedir().replace(/\\/g, '/')}/Desktop`)
} else {
reslove(decodeURIComponent(iconv.decode(stdout, 'GBK').trim()));
@ -215,37 +236,67 @@ special = async cmd => {
}
// 获取选中的文件
if (cmd.includes('{{SelectFile}}')) {
let repl = getSelectFile();
let repl = await getSelectFile();
cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl)
}
return cmd;
}
run = async (cmd, option, codec, callback) => {
run = async (cmd, option, codec, terminal, callback) => {
var tmp = os.tmpdir(),
bin = option.bin,
argv = option.argv,
ext = option.ext;
cmd = await special(cmd);
let script = path.join(tmp, `QuickCommandTempScript.${ext}`)
// if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd, 'cp936');
// 批处理和 powershell 默认编码为 GBK, 解决批处理的换行问题
if (ext == 'bat' || ext == 'ps1') cmd = iconv.encode(cmd.replace(/\n/g, '\r\n'), 'GBK');
fs.writeFileSync(script, cmd);
var argvs = [script]
if (argv) {
argvs = argv.split(' ')
argvs.push(script);
}
var child;
if (bin) {
var child = spawn(bin, argvs, { encoding: 'buffer' })
// 在终端中输出
if (terminal) {
if (isWin) {
child = spawn('cmd', ['/c', 'start', 'cmd', '/k', bin].concat(argvs), { encoding: 'buffer' })
} else {
var appleScript = `if application "Terminal" is running then
tell application "Terminal"
# do script without "in window" will open a new window
do script "clear;${bin} ${argv} ${script}"
activate
end tell
else
tell application "Terminal"
# window 1 is guaranteed to be recently opened window
do script "clear;${bin} ${argv} ${script}" in window 1
activate
end tell
end if`;
child = spawn('osascript', ['-e', appleScript], { encoding: 'buffer' })
}
} else {
child = spawn(bin, argvs, { encoding: 'buffer' })
}
} else {
var child = spawn(script, { encoding: 'buffer' })
if (terminal) {
child = spawn('cmd', ['/c', 'start', 'cmd', '/k', script], { encoding: 'buffer' })
} else {
child = spawn(script, { encoding: 'buffer' })
}
}
var chunks = [],
err_chunks = [];
child.stdout.on('data', chunk => {
if (ext == 'bat' || ext == 'ps1') chunk = iconv.decode(chunk, 'GBK')
chunks.push(chunk)
})
child.stderr.on('data', err_chunk => {
if (ext == 'bat' || ext == 'ps1') err_chunk = iconv.decode(err_chunk, 'GBK')
err_chunks.push(err_chunk)
})
child.on('close', code => {