Skip to content

Commit

Permalink
Tunes the format of user phrases.
Browse files Browse the repository at this point in the history
  • Loading branch information
zonble committed Feb 29, 2024
1 parent 764408e commit 52df689
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/pime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ enum PimeMcBopomofoCommand {
userPhrase = 5,
chineseConvert = 6,
halfWidthPunctuation = 7,
reloadUserPhrase = 8,
}

/** Wraps InputController and required states. */
Expand Down Expand Up @@ -390,21 +391,34 @@ class PimeMcBopomofo {

loadUserPhrases() {
fs.readFile(this.userPhrasesPath, (err, data) => {
let map = new Map<string, string[]>();
if (err) {
console.error(
"Unable to read user phrases from " + this.userPhrasesPath
);
this.writeUserPhrases(map);
return;
}
try {
map = new Map(JSON.parse(data.toString()));
let map = new Map<string, string[]>();

let string = data.toString();
let lines = string.split("\n");
for (let line in lines) {
let components = line.split(" ");
if (components.length >= 2) {
let key = components[0];
let phrase = components[1];
let phrases = map.get(key);
if (phrases === undefined) {
phrases = [];
}
phrases.push(phrase);
map.set(key, phrases);
}
}
this.mcInputController.setUserPhrases(map);
} catch {
this.writeUserPhrases(map);
console.error("Failed to parse user phrases");
}
this.mcInputController.setUserPhrases(map);
});
}

Expand All @@ -415,7 +429,17 @@ class PimeMcBopomofo {
fs.mkdirSync(this.userDataPath);
}

let string = JSON.stringify(Array.from(map.entries()));
let string = "";
for (let key of map.keys()) {
let phrases = map.get(key);
if (phrases === undefined) {
continue;
}
for (let phrase of phrases) {
string += key + " " + phrase + "\n";
}
}

console.log("Writing user phrases to " + this.userPhrasesPath);
fs.writeFile(this.userPhrasesPath, string, (err) => {
if (err) {
Expand Down Expand Up @@ -629,7 +653,7 @@ class PimeMcBopomofo {
break;
case PimeMcBopomofoCommand.homepage:
{
let url = "https://github.com/openvanilla/McBopomofoWeb";
let url = "https://mcbopomofo.openvanilla.org/";
let command = `start ${url}`;
console.log("Run " + command);
child_process.exec(command);
Expand Down Expand Up @@ -678,6 +702,8 @@ class PimeMcBopomofo {
pimeMcBopomofo.applySettings();
pimeMcBopomofo.writeSettings();
break;
case PimeMcBopomofoCommand.reloadUserPhrase:
pimeMcBopomofo.loadUserPhrases();
default:
break;
}
Expand Down Expand Up @@ -813,7 +839,7 @@ module.exports = {
let { opened } = request;
pimeMcBopomofo.isOpened = opened;
pimeMcBopomofo.loadSettings();
pimeMcBopomofo.loadUserPhrases();
// pimeMcBopomofo.loadUserPhrases();
let customUi = pimeMcBopomofo.customUiResponse();
let response = Object.assign({}, responseTemplate, customUi);
return response;
Expand Down Expand Up @@ -871,6 +897,10 @@ module.exports = {
text: "編輯使用者詞庫 (&U)",
id: PimeMcBopomofoCommand.userPhrase,
},
{
text: "重新載入使用者詞庫",
id: PimeMcBopomofoCommand.reloadUserPhrase,
},
];
let response = Object.assign({}, responseTemplate, { return: menu });
return response;
Expand Down

0 comments on commit 52df689

Please sign in to comment.