2021-07-07 17:00:11 +00:00
|
|
|
import type { MetadataCache, Vault } from "obsidian";
|
2021-07-07 12:34:39 +00:00
|
|
|
import { DataCollector } from "src/data/collector";
|
|
|
|
import type { BetterWordCountSettings } from "src/settings/settings";
|
|
|
|
import {
|
|
|
|
getWordCount,
|
|
|
|
getCharacterCount,
|
|
|
|
getSentenceCount,
|
|
|
|
} from "../data/stats";
|
2021-07-03 19:22:50 +00:00
|
|
|
import type { StatusBar } from "./bar";
|
2021-07-07 12:34:39 +00:00
|
|
|
import { Expression, parse } from "./parse";
|
2021-07-03 19:22:50 +00:00
|
|
|
|
|
|
|
export class BarManager {
|
|
|
|
private statusBar: StatusBar;
|
2021-07-07 12:34:39 +00:00
|
|
|
private settings: BetterWordCountSettings;
|
2021-07-07 17:00:11 +00:00
|
|
|
private vault: Vault;
|
2021-07-07 12:34:39 +00:00
|
|
|
private dataCollector: DataCollector;
|
2021-07-03 19:22:50 +00:00
|
|
|
|
2021-07-07 12:34:39 +00:00
|
|
|
constructor(
|
|
|
|
statusBar: StatusBar,
|
|
|
|
settings: BetterWordCountSettings,
|
2021-07-07 17:00:11 +00:00
|
|
|
vault: Vault,
|
|
|
|
metadataCache: MetadataCache
|
2021-07-07 12:34:39 +00:00
|
|
|
) {
|
|
|
|
this.statusBar = statusBar;
|
|
|
|
this.settings = settings;
|
2021-07-07 17:00:11 +00:00
|
|
|
this.vault = vault;
|
|
|
|
this.dataCollector = new DataCollector(vault, metadataCache);
|
2021-07-07 12:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateStatusBar(text: string): Promise<void> {
|
|
|
|
let newText = "";
|
|
|
|
const expression: Expression = parse(this.settings.statusBarQuery);
|
|
|
|
|
|
|
|
let varsIndex = 0;
|
|
|
|
expression.parsed.forEach((value: string) => {
|
|
|
|
newText = newText + value;
|
|
|
|
switch (expression.vars[varsIndex]) {
|
|
|
|
case 0:
|
|
|
|
newText = newText + getWordCount(text);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
newText = newText + getCharacterCount(text);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
newText = newText + getSentenceCount(text);
|
|
|
|
break;
|
|
|
|
case 3:
|
2021-07-07 17:00:11 +00:00
|
|
|
newText = newText + 0;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-07-07 17:00:11 +00:00
|
|
|
newText = newText + 0;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2021-07-07 17:00:11 +00:00
|
|
|
newText = newText + 0;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-07-07 17:00:11 +00:00
|
|
|
newText = newText + 0;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
varsIndex++;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.statusBar.displayText(newText);
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateAltStatusBar(): Promise<void> {
|
|
|
|
let newText = "";
|
|
|
|
const expression: Expression = parse(this.settings.statusBarAltQuery);
|
|
|
|
|
|
|
|
let varsIndex = 0;
|
2021-07-07 17:00:11 +00:00
|
|
|
for (const i in expression.parsed) {
|
|
|
|
const e = expression.parsed[i];
|
|
|
|
newText = newText + e;
|
2021-07-07 12:34:39 +00:00
|
|
|
switch (expression.vars[varsIndex]) {
|
|
|
|
case 0:
|
|
|
|
newText = newText + getWordCount("");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
newText = newText + getCharacterCount("");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
newText = newText + getSentenceCount("");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
newText = newText + (await this.dataCollector.getTotalWordCount());
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
newText =
|
|
|
|
newText + (await this.dataCollector.getTotalCharacterCount());
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
newText =
|
2021-07-07 17:00:11 +00:00
|
|
|
newText + (await this.dataCollector.getTotalCharacterCount());
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-07-07 17:00:11 +00:00
|
|
|
newText = newText + this.dataCollector.getTotalFileCount();
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
varsIndex++;
|
2021-07-07 17:00:11 +00:00
|
|
|
}
|
2021-07-07 12:34:39 +00:00
|
|
|
|
|
|
|
this.statusBar.displayText(newText);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursorActivity(cm: CodeMirror.Editor) {
|
|
|
|
if (cm.somethingSelected()) {
|
|
|
|
this.updateStatusBar(cm.getSelection());
|
|
|
|
} else {
|
|
|
|
this.updateStatusBar(cm.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// change(cm: CodeMirror.Editor, changeObj: CodeMirror.EditorChangeLinkedList) {
|
|
|
|
// this.updateStatusBar(cm.getValue());
|
|
|
|
// }
|
2021-07-03 19:22:50 +00:00
|
|
|
}
|