Skip to content

Commit

Permalink
add config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Handgrip committed May 27, 2022
1 parent c647994 commit 2025517
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 41 deletions.
75 changes: 42 additions & 33 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
<div class="form-group">
<label for="directory-input">代码文件夹</label>
<div class="row">
<div class="col col-9">
<div class="col col-8">
<input
type="text"
id="directory-input"
class="form-control"
/>
</div>
<div class="col col-3">
<div class="col col-2">
<button
type="button"
onclick="chooseDirectory()"
Expand All @@ -34,6 +34,15 @@
选择文件夹
</button>
</div>
<div class="col col-2">
<button
type="button"
onclick="resetConfig()"
class="btn btn-primary"
>
重置配置
</button>
</div>
</div>
</div>
<div class="form-group">
Expand All @@ -43,9 +52,7 @@
cols="30"
rows="2"
class="form-control"
>
(filename) => true</textarea
>
></textarea>
</div>
<div class="form-group">
<button
Expand All @@ -70,7 +77,7 @@
<select id="language-select" class="form-control">
<option value="0">8086</option>
<option value="1">C</option>
<option value="2" selected>C++</option>
<option value="2">C++</option>
<option value="3">Java</option>
<option value="4">lisp</option>
<option value="5">m2</option>
Expand All @@ -85,53 +92,55 @@
id="threshold"
type="number"
step="1"
min="0"
min="1"
max="100"
class="form-control"
value="75"
/>
</div>
<div class="form-group">
<button
type="button"
class="btn btn-primary"
onclick="processSIM()"
>
执行
</button>
</div>
<label for="sim-stdout">sim-stdout</label>
<pre
id="sim-stdout"
class="bg-light text-info"
style="max-height: 300px"
>
Null</pre
>
<label for="sim-stderr">sim-stderr</label>
<pre
id="sim-stderr"
class="bg-light text-info"
style="max-height: 100px"
>
Null</pre
>
<div class="form-group">
<label for="result-fliter">结果过滤器</label>
<textarea
id="result-fliter"
cols="30"
rows="5"
class="form-control"
>
(fileAName, fileBName, percentage) => {
const A = fileAName.split("-");
const B = fileBName.split("-");
return A[0] != B[0] && A[1] == B[1];
};</textarea
>
></textarea>
</div>
<div class="form-group">
<button
type="button"
class="btn btn-primary"
onclick="processSIM()"
onclick="processResultFilter()"
>
执行
过滤
</button>
</div>
</form>
<label for="sim-stdout">sim-stdout</label>
<pre
id="sim-stdout"
class="bg-light text-info"
style="max-height: 300px"
>
Null</pre
>
<label for="sim-stderr">sim-stderr</label>
<pre
id="sim-stderr"
class="bg-light text-info"
style="max-height: 100px"
>
Null</pre
>
<!-- <div style="overflow-y: auto; max-height: 300px"> -->
<table
class="table table-striped table-hover"
Expand Down
56 changes: 51 additions & 5 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const { dialog } = require("electron").remote;
const fs = require("fs");
const path = require("path");
const child_process = require("child_process");
const stream = require("stream");
const os = require("os");
const process = require("process");
const configPath = path.join(process.cwd(), "config.json");

const valueToLanguage = [
"8086",
Expand All @@ -22,7 +23,7 @@ const valueToMonacoLanguage = [
"cpp",
"java",
"plaintext",
"plaintext",
"m3",
"plaintext",
"pascal",
"plaintext",
Expand Down Expand Up @@ -83,6 +84,10 @@ async function processSIM() {
const err = ret.stderr.toString("utf-8");
$("#sim-stdout").text(out);
$("#sim-stderr").text(err);
}

function processResultFilter() {
const out = $("#sim-stdout").text();
const resArr = out.split("\n");
let startIndex = resArr.indexOf("\r") + 1;
let tableData = [];
Expand All @@ -103,9 +108,7 @@ async function processSIM() {
tableData.forEach((v) => {
tableBody.append(
$(
`<tr onclick="diff()"><th>${v.fileAName}</th><th>${
v.fileBName
}</th><th>${v.similarPercentage.toString()}</th></tr>`
`<tr onclick="diff()"><th>${v.fileAName}</th><th>${v.fileBName}</th><th>${v.similarPercentage}</th></tr>`
)
);
});
Expand All @@ -132,6 +135,40 @@ function diff() {
$("#diffModal").modal();
}

function loadConfig() {
const config = JSON.parse(fs.readFileSync(configPath));
$("#directory-input").val(config.directory ?? "");
$("#input-file-fliter").val(config.fileFilter ?? "");
$("#language-select").val(config.language ?? "");
$("#threshold").val(config.threshold ?? "");
$("#result-fliter").val(config.resultFilter ?? "");
}

function saveConfig(config) {
fs.writeFileSync(
configPath,
JSON.stringify(
config ?? {
directory: $("#directory-input").val(),
fileFilter: $("#input-file-fliter").val(),
language: $("#language-select").val(),
threshold: $("#threshold").val(),
resultFilter: $("#result-fliter").val(),
}
)
);
}
function resetConfig() {
saveConfig({
directory: os.homedir(),
fileFilter: "(filename) => true",
language: "2",
threshold: 75,
resultFilter: `(fileAName, fileBName, percentage) => {\n const A = fileAName.split("-");\n const B = fileBName.split("-");\n return A[0] != B[0] && A[1] == B[1];\n};`,
});
loadConfig();
}

(function () {
const amdLoader = require("./node_modules/monaco-editor/min/vs/loader.js");
const amdRequire = amdLoader.require;
Expand Down Expand Up @@ -159,3 +196,12 @@ function diff() {
);
});
})();

(function () {
if (!fs.existsSync(configPath)) {
resetConfig();
} else {
loadConfig();
}
window.onbeforeunload = () => saveConfig();
})();
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simgui",
"version": "0.0.4",
"version": "0.0.5",
"description": "SimGUI",
"main": "dist/main.js",
"scripts": {
Expand Down

0 comments on commit 2025517

Please sign in to comment.