支持匹配脚本文件,直接运行

This commit is contained in:
fofolee 2020-06-24 17:54:04 +08:00
parent 08ac0b501b
commit a5610f367b
4 changed files with 38 additions and 13 deletions

View File

@ -14,8 +14,10 @@ utools.onPluginEnter(async ({ code, type, payload }) => {
// $("#options").show();
showOptions();
} else if (code == 'code') {
var file = ""
utools.setExpendHeight(600);
showCodeEditor()
if (type == 'files') file = payload[0].path
showCodeEditor(file)
} else {
// console.log(new Date().getTime() - window.startTime);
utools.setExpendHeight(0);

View File

@ -23,7 +23,7 @@ let importCommand = () => {
var options = {
filters: [{ name: 'json', extensions: ['json'] }, ]
}
var file = openFileInDialog(options, true)
var file = getFileInfo({ type: 'dialog', argvs: options, readfile: true })
if (file) {
try {
var pushData = JSON.parse(file.data);
@ -700,7 +700,7 @@ $("#options").on('click', '#icon, #iconame', function () {
extensions: ['png']
}, ]
}
var file = openFileInDialog(options, false)
var file = getFileInfo({ type: 'dialog', argvs: options, readfile: false })
if (file) {
$("#iconame").val(file.name);
$("#icon").attr('src', file.path);
@ -960,7 +960,7 @@ let highlightIfKnown = ext => {
if (lang.length) window.editor.setOption("mode", lang[0])
}
showCodeEditor = () => {
showCodeEditor = file => {
let options = `<option>${Object.keys(programs).join('</option><option>')}</option>`
var customWindow = `<div id="customize">
<select id="program">
@ -991,9 +991,15 @@ showCodeEditor = () => {
$("span.customscript > input").css({"height": "30px"})
var db = getDB('codeHistory')
window.editor.setOption("theme", "ambiance")
if (db.history) {
$('#program').val(db.history.program)
window.editor.setValue(db.history.cmd)
if (file) {
var fileinfo = getFileInfo({ type: 'file', argvs: file, readfile: true })
console.log(fileinfo);
window.editor.setValue(fileinfo.data)
var program = Object.keys(programs).filter(x => `.${programs[x].ext}` == fileinfo.ext)[0]
$('#program').val(program)
runCurrentCommand()
} else {
db.history && $('#program').val(db.history.program) && window.editor.setValue(db.history.cmd)
}
programCheck()
$('#program').select2({

View File

@ -17,7 +17,13 @@
{
"code": "code",
"explain": "无需打开编辑器,快速运行临时代码",
"cmds": [ "运行代码", "RunCode"]
"cmds": [ "运行代码", "RunCode", {
"type": "files",
"label": "运行代码",
"fileType": "file",
"match": "/\\.(sh|scpt|bat|ps1|py|js|rb|php|c|cs|lua|pl)$/i",
"maxNum": 1
}]
}
]
}

View File

@ -356,15 +356,26 @@ getBase64Ico = path => {
return fs.readFileSync(path, 'base64');
}
openFileInDialog = (options, readfile) => {
var dialog = utools.showOpenDialog(options);
if (!dialog) return false
var file = dialog[0]
getFileInfo = options => {
var file
if (options.type == 'file') {
file = options.argvs
} else if (options.type == 'dialog') {
var dialog = utools.showOpenDialog(options.argvs);
if (!dialog) return false
file = dialog[0]
} else {
return false
}
var information = {
name: path.basename(file),
ext: path.extname(file),
path: file
}
if(readfile) information.data = fs.readFileSync(file)
if (options.readfile) {
var codec = (information.ext == '.bat' || information == '.ps1') ? 'gbk' : 'utf8'
information.data = iconv.decode(fs.readFileSync(file), codec)
}
return information
}