2021-07-11 21:36:48 +00:00
|
|
|
import { MetadataCache, Vault, Debouncer, debounce } from "obsidian";
|
2021-07-07 12:34:39 +00:00
|
|
|
import { DataCollector } from "src/data/collector";
|
2021-07-08 20:20:38 +00:00
|
|
|
import { DataManager } from "src/data/manager";
|
|
|
|
import type { TodayCounts } from "src/data/manager";
|
2021-07-07 12:34:39 +00:00
|
|
|
import type { BetterWordCountSettings } from "src/settings/settings";
|
|
|
|
import {
|
|
|
|
getWordCount,
|
|
|
|
getCharacterCount,
|
|
|
|
getSentenceCount,
|
2021-07-07 17:29:16 +00:00
|
|
|
cleanComments,
|
2021-07-07 12:34:39 +00:00
|
|
|
} 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-08 20:20:38 +00:00
|
|
|
private dataManager: DataManager;
|
2021-07-11 21:36:48 +00:00
|
|
|
private deboucer: Debouncer<[text: string]>;
|
|
|
|
private expression: Expression;
|
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-08 20:20:38 +00:00
|
|
|
this.dataManager = new DataManager(vault, metadataCache);
|
2021-07-11 21:36:48 +00:00
|
|
|
this.deboucer = debounce(
|
|
|
|
(text: string) => this.updateStatusBar(text),
|
|
|
|
20,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
this.expression = parse(this.settings.statusBarQuery);
|
2021-07-07 12:34:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 21:36:48 +00:00
|
|
|
updateStatusBar(text: string): void {
|
2021-07-07 12:34:39 +00:00
|
|
|
let newText = "";
|
2021-07-11 21:36:48 +00:00
|
|
|
|
2021-07-11 11:18:37 +00:00
|
|
|
if (this.settings.collectStats) {
|
|
|
|
this.dataManager.updateTodayCounts();
|
|
|
|
}
|
2021-07-08 20:20:38 +00:00
|
|
|
const todayCounts: TodayCounts = this.settings.collectStats
|
|
|
|
? this.dataManager.getTodayCounts()
|
|
|
|
: { words: 0, characters: 0, sentences: 0 };
|
2021-07-07 12:34:39 +00:00
|
|
|
|
|
|
|
let varsIndex = 0;
|
2021-07-11 21:36:48 +00:00
|
|
|
for (const i in this.expression.parsed) {
|
|
|
|
const e = this.expression.parsed[i];
|
2021-07-08 20:20:38 +00:00
|
|
|
newText = newText + e;
|
2021-07-11 21:36:48 +00:00
|
|
|
switch (this.expression.vars[varsIndex]) {
|
2021-07-07 12:34:39 +00:00
|
|
|
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-12 12:47:37 +00:00
|
|
|
newText = newText + this.dataManager.getTotalCounts().words;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-07-12 12:47:37 +00:00
|
|
|
newText = newText + this.dataManager.getTotalCounts().characters;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2021-07-12 12:47:37 +00:00
|
|
|
newText = newText + this.dataManager.getTotalCounts().sentences;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-07-08 20:20:38 +00:00
|
|
|
newText = newText + this.dataCollector.getTotalFileCount();
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
newText = newText + todayCounts.words;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
newText = newText + todayCounts.characters;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
newText = newText + todayCounts.sentences;
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
varsIndex++;
|
2021-07-08 20:20:38 +00:00
|
|
|
}
|
2021-07-07 12:34:39 +00:00
|
|
|
|
|
|
|
this.statusBar.displayText(newText);
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:47:37 +00:00
|
|
|
async updateAltStatusBar(): Promise<void> {
|
2021-07-07 12:34:39 +00:00
|
|
|
let newText = "";
|
|
|
|
const expression: Expression = parse(this.settings.statusBarAltQuery);
|
2021-07-11 21:36:48 +00:00
|
|
|
if (this.settings.collectStats) {
|
|
|
|
this.dataManager.updateTodayCounts();
|
|
|
|
}
|
|
|
|
|
2021-07-08 20:20:38 +00:00
|
|
|
const todayCounts: TodayCounts = this.settings.collectStats
|
|
|
|
? this.dataManager.getTodayCounts()
|
|
|
|
: { words: 0, characters: 0, sentences: 0 };
|
2021-07-07 12:34:39 +00:00
|
|
|
|
|
|
|
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:
|
2021-07-12 12:47:37 +00:00
|
|
|
newText = newText + (await this.dataCollector.getTotalWordCount());
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-07-12 12:47:37 +00:00
|
|
|
newText =
|
|
|
|
newText + (await this.dataCollector.getTotalCharacterCount());
|
2021-07-07 12:34:39 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2021-07-12 12:47:37 +00:00
|
|
|
newText =
|
|
|
|
newText + (await this.dataCollector.getTotalSentenceCount());
|
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;
|
2021-07-08 20:20:38 +00:00
|
|
|
case 7:
|
|
|
|
newText = newText + todayCounts.words;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
newText = newText + todayCounts.characters;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
newText = newText + todayCounts.sentences;
|
|
|
|
break;
|
2021-07-07 12:34:39 +00:00
|
|
|
}
|
|
|
|
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()) {
|
2021-07-07 17:29:16 +00:00
|
|
|
if (this.settings.countComments) {
|
2021-07-11 21:36:48 +00:00
|
|
|
this.deboucer(cleanComments(cm.getSelection()));
|
2021-07-07 17:29:16 +00:00
|
|
|
} else {
|
2021-07-11 21:36:48 +00:00
|
|
|
this.deboucer(cm.getSelection());
|
2021-07-07 17:29:16 +00:00
|
|
|
}
|
2021-07-07 12:34:39 +00:00
|
|
|
} else {
|
2021-07-08 20:20:38 +00:00
|
|
|
if (this.settings.collectStats) {
|
|
|
|
this.dataManager.updateFromFile();
|
|
|
|
}
|
2021-07-07 17:29:16 +00:00
|
|
|
if (this.settings.countComments) {
|
2021-07-11 21:36:48 +00:00
|
|
|
this.deboucer(cleanComments(cm.getValue()));
|
2021-07-07 17:29:16 +00:00
|
|
|
} else {
|
2021-07-11 21:36:48 +00:00
|
|
|
this.deboucer(cm.getValue());
|
2021-07-07 17:29:16 +00:00
|
|
|
}
|
2021-07-07 12:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-03 19:22:50 +00:00
|
|
|
}
|