运行命令功能

This commit is contained in:
fofolee 2022-03-30 16:45:11 +08:00
parent 8349b791fe
commit 4128e0562a
2 changed files with 89 additions and 1 deletions

View File

@ -19,7 +19,7 @@
</q-card-section> </q-card-section>
<q-card-actions align="right"> <q-card-actions align="right">
<q-btn color="primary" label="确定" @click="onOKClick" /> <q-btn color="primary" label="确定" @click="onOKClick" />
<q-btn color="primary" label="取消" @click="onCancelClick" /> <q-btn color="negative" label="取消" @click="onCancelClick" />
</q-card-actions> </q-card-actions>
</q-card> </q-card>
</q-dialog> </q-dialog>

View File

@ -98,12 +98,23 @@
" "
/> />
</div> </div>
<q-dialog v-model="isResultShow" @hide="runResult = ''" position="bottom">
<q-card style="width: 90vh">
<q-card-section class="row items-center">
<span v-html="runResult"></span>
</q-card-section>
<q-card-actions align="right">
<q-btn flat label="关闭" color="primary" v-close-popup />
</q-card-actions>
</q-card>
</q-dialog>
</div> </div>
</template> </template>
<script> <script>
import globalVars from "components/GlobalVars"; import globalVars from "components/GlobalVars";
import * as monaco from "monaco-editor"; import * as monaco from "monaco-editor";
import { toRaw } from "vue";
export default { export default {
data() { data() {
@ -115,6 +126,9 @@ export default {
scptarg: "", scptarg: "",
scriptCode: "", scriptCode: "",
outputCode: "", outputCode: "",
output: "",
isResultShow: false,
runResult: "",
}; };
}, },
mounted() { mounted() {
@ -144,6 +158,9 @@ export default {
} }
); );
}, },
getEditorValue() {
return toRaw(this.editor).getValue();
},
matchLanguage() { matchLanguage() {
let language = Object.values(globalVars.programs).filter( let language = Object.values(globalVars.programs).filter(
(program) => program.ext === this.customOptions.ext (program) => program.ext === this.customOptions.ext
@ -175,6 +192,77 @@ export default {
} }
); );
}, },
async runCurrentCommand() {
let cmd = this.getEditorValue();
cmd = window.special(cmd);
cmd = await this.replaceTempInputVals(cmd);
let terminal = false;
let raw = true;
switch (this.output) {
case "html":
raw = false;
break;
case "terminal":
terminal = true;
break;
case "ignore":
utools.hideMainWindow();
break;
}
if (this.program === "quickcommand") {
window.runCodeInVm(cmd, (stdout, stderr) => {
if (stderr) return this.showRunResult(stderr, raw, false);
this.showRunResult(stdout, raw, true);
});
} else {
let option = globalVars.programs[this.program];
if (program === "custom")
option = {
bin: this.customOptions.bin,
argv: this.customOptions.argv,
ext: this.customOptions.ext,
};
option.scptarg = this.scptarg;
option.charset = {
scriptCode: this.scriptCode,
outputCode: this.outputCode,
};
window.runCodeFile(cmd, option, terminal, (stdout, stderr) => {
if (terminal) return;
if (stderr) return this.showRunResult(stderr, raw, false);
this.showRunResult(stdout, raw, true);
});
}
},
async replaceTempInputVals(cmd) {
let tempInputVals = [];
let specilaVals = [
"input",
"subinput",
"pwd",
"SelectFile",
"WindowInfo",
"MatchedFiles",
];
specilaVals.forEach((x) => {
let m = cmd.match(new RegExp("{{" + x + ".*?}}", "g"));
m &&
m.forEach((y) => tempInputVals.includes(y) || tempInputVals.push(y));
});
if (!tempInputVals.length) return cmd;
let inputs = await quickcommand.showInputBox(
tempInputVals,
"需要临时为以下变量赋值"
);
tempInputVals.forEach((t, n) => {
cmd = cmd.replace(new RegExp(t, "g"), inputs[n]);
});
return cmd;
},
showRunResult(content, raw, success) {
this.isResultShow = true;
this.runResult += content;
},
}, },
}; };
</script> </script>