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

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(); // $("#options").show();
showOptions(); showOptions();
} else if (code == 'code') { } else if (code == 'code') {
var file = ""
utools.setExpendHeight(600); utools.setExpendHeight(600);
showCodeEditor() if (type == 'files') file = payload[0].path
showCodeEditor(file)
} else { } else {
// console.log(new Date().getTime() - window.startTime); // console.log(new Date().getTime() - window.startTime);
utools.setExpendHeight(0); utools.setExpendHeight(0);

View File

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

View File

@ -17,7 +17,13 @@
{ {
"code": "code", "code": "code",
"explain": "无需打开编辑器,快速运行临时代码", "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'); return fs.readFileSync(path, 'base64');
} }
openFileInDialog = (options, readfile) => { getFileInfo = options => {
var dialog = utools.showOpenDialog(options); var file
if (!dialog) return false if (options.type == 'file') {
var file = dialog[0] 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 = { var information = {
name: path.basename(file), name: path.basename(file),
ext: path.extname(file),
path: 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 return information
} }