From 4b60c3ee86f7ca1bf6b3f0b0b1ce39d9686d0af7 Mon Sep 17 00:00:00 2001 From: Leo Yao <45076643+leoccyao@users.noreply.github.com> Date: Fri, 2 Apr 2021 17:24:17 -0400 Subject: [PATCH] Add total word counts into graph view count This patch adds total word/character/sentence counts into the count for non-markdown files. Visibility of each count is based on already existing settings. Note that the first call of updateAltCount() has been moved after the settings are loaded. --- src/main.ts | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 78dc0c3..e5974af 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,13 +25,13 @@ export default class BetterWordCount extends Plugin { let statusBarEl = this.addStatusBarItem(); this.statusBar = new StatusBar(statusBarEl); - this.updateAltCount(); - this.recentlyTyped = false; this.settings = (await this.loadData()) || new BetterWordCountSettings(); this.addSettingTab(new BetterWordCountSettingsTab(this.app, this)); + this.updateAltCount(); + this.registerEvent( this.app.workspace.on("file-open", this.onFileOpen, this) ); @@ -100,7 +100,41 @@ export default class BetterWordCount extends Plugin { async updateAltCount() { const files = getFilesCount(this.app.vault.getFiles()); - this.statusBar.displayText(`${files} files`); + let displayText: string = `${files} files `; + let allWords = 0; + let allCharacters = 0; + let allSentences = 0; + + for(const f of this.app.vault.getMarkdownFiles()){ + let fileContents = await this.app.vault.cachedRead(f); + allWords += getWordCount(fileContents); + allCharacters += getCharacterCount(fileContents); + allSentences += getSentenceCount(fileContents); + } + + if (this.settings.showWords) { + displayText = + displayText + + this.settings.wordsPrefix + + allWords + + this.settings.wordsSuffix; + } + if (this.settings.showCharacters) { + displayText = + displayText + + this.settings.charactersPrefix + + allCharacters + + this.settings.charactersSuffix; + } + if (this.settings.showSentences) { + displayText = + displayText + + this.settings.sentencesPrefix + + allSentences + + this.settings.sentencesSuffix; + } + + this.statusBar.displayText(displayText); } updateWordCount(text: string) {