Merge pull request #76 from minermaniac447/master

This commit is contained in:
Luke Leppan 2023-03-30 00:03:09 +02:00
commit 437547f02e
No known key found for this signature in database
GPG key ID: D35B272CE58AE00A
8 changed files with 120 additions and 3 deletions

View file

@ -33,7 +33,7 @@ export default class BetterWordCount extends Plugin {
// Handle Statistics // Handle Statistics
if (this.settings.collectStats) { if (this.settings.collectStats) {
this.statsManager = new StatsManager(this.app.vault, this.app.workspace); this.statsManager = new StatsManager(this.app.vault, this.app.workspace, this);
} }
// Handle Status Bar // Handle Status Bar

View file

@ -2,6 +2,7 @@ export enum MetricCounter {
words, words,
characters, characters,
sentences, sentences,
pages,
files, files,
} }
@ -38,6 +39,7 @@ export interface BetterWordCountSettings {
altBar: StatusBarItem[]; altBar: StatusBarItem[];
countComments: boolean; countComments: boolean;
collectStats: boolean; collectStats: boolean;
pageWords: number;
} }
export const DEFAULT_SETTINGS: BetterWordCountSettings = { export const DEFAULT_SETTINGS: BetterWordCountSettings = {
@ -71,4 +73,5 @@ export const DEFAULT_SETTINGS: BetterWordCountSettings = {
], ],
countComments: false, countComments: false,
collectStats: false, collectStats: false,
pageWords: 300,
}; };

View file

@ -1,4 +1,4 @@
import { App, PluginSettingTab, Setting, ToggleComponent } from "obsidian"; import { App, PluginSettingTab, Setting, ToggleComponent, TextComponent } from "obsidian";
import type BetterWordCount from "src/main"; import type BetterWordCount from "src/main";
import { addStatusBarSettings } from "./StatusBarSettings"; import { addStatusBarSettings } from "./StatusBarSettings";
@ -37,6 +37,18 @@ export default class BetterWordCountSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}); });
}); });
new Setting(containerEl)
.setName("Page Word Count")
.setDesc("Set how many words count as one \"page\"")
.addText((text: TextComponent) => {
text.inputEl.type = "number";
text.setPlaceholder("300");
text.setValue(this.plugin.settings.pageWords.toString());
text.onChange(async (value: string) => {
this.plugin.settings.pageWords = parseInt(value);
await this.plugin.saveSettings();
});
});
// Status Bar Settings // Status Bar Settings
addStatusBarSettings(this.plugin, containerEl); addStatusBarSettings(this.plugin, containerEl);

View file

@ -18,6 +18,8 @@
return "Chars in Note" return "Chars in Note"
case MetricCounter.sentences: case MetricCounter.sentences:
return "Sentences in Note" return "Sentences in Note"
case MetricCounter.pages:
return "Pages in Note"
case MetricCounter.files: case MetricCounter.files:
return "Total Notes" return "Total Notes"
} }
@ -29,6 +31,8 @@
return "Daily Chars" return "Daily Chars"
case MetricCounter.sentences: case MetricCounter.sentences:
return "Daily Sentences" return "Daily Sentences"
case MetricCounter.pages:
return "Daily Pages"
case MetricCounter.files: case MetricCounter.files:
return "Total Notes" return "Total Notes"
} }
@ -40,6 +44,8 @@
return "Total Chars" return "Total Chars"
case MetricCounter.sentences: case MetricCounter.sentences:
return "Total Sentences" return "Total Sentences"
case MetricCounter.pages:
return "Total Pages"
case MetricCounter.files: case MetricCounter.files:
return "Total Notes" return "Total Notes"
} }
@ -181,6 +187,7 @@
<option value={MetricCounter.words}>Words</option> <option value={MetricCounter.words}>Words</option>
<option value={MetricCounter.characters}>Characters</option> <option value={MetricCounter.characters}>Characters</option>
<option value={MetricCounter.sentences}>Sentences</option> <option value={MetricCounter.sentences}>Sentences</option>
<option value={MetricCounter.pages}>Pages</option>
<option value={MetricCounter.files}>Files</option> <option value={MetricCounter.files}>Files</option>
</select> </select>
</div> </div>
@ -348,6 +355,7 @@
<option value={MetricCounter.words}>Words</option> <option value={MetricCounter.words}>Words</option>
<option value={MetricCounter.characters}>Characters</option> <option value={MetricCounter.characters}>Characters</option>
<option value={MetricCounter.sentences}>Sentences</option> <option value={MetricCounter.sentences}>Sentences</option>
<option value={MetricCounter.pages}>Pages</option>
<option value={MetricCounter.files}>Files</option> <option value={MetricCounter.files}>Files</option>
</select> </select>
</div> </div>

View file

@ -9,10 +9,12 @@ export interface Day {
words: number; words: number;
characters: number; characters: number;
sentences: number; sentences: number;
pages: number;
files: number; files: number;
totalWords: number; totalWords: number;
totalCharacters: number; totalCharacters: number;
totalSentences: number; totalSentences: number;
totalPages: number;
} }
export type ModifiedFiles = Record<string, FileStat>; export type ModifiedFiles = Record<string, FileStat>;
@ -21,6 +23,7 @@ export interface FileStat {
words: CountDiff; words: CountDiff;
characters: CountDiff; characters: CountDiff;
sentences: CountDiff; sentences: CountDiff;
pages: CountDiff;
} }
export interface CountDiff { export interface CountDiff {

View file

@ -1,23 +1,27 @@
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian"; import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
import type BetterWordCount from "../main";
import { STATS_FILE } from "../constants"; import { STATS_FILE } from "../constants";
import type { Day, VaultStatistics } from "./Stats"; import type { Day, VaultStatistics } from "./Stats";
import moment from "moment"; import moment from "moment";
import { import {
getCharacterCount, getCharacterCount,
getSentenceCount, getSentenceCount,
getPageCount,
getWordCount, getWordCount,
} from "../utils/StatUtils"; } from "../utils/StatUtils";
export default class StatsManager { export default class StatsManager {
private vault: Vault; private vault: Vault;
private workspace: Workspace; private workspace: Workspace;
private plugin: BetterWordCount;
private vaultStats: VaultStatistics; private vaultStats: VaultStatistics;
private today: string; private today: string;
public debounceChange; public debounceChange;
constructor(vault: Vault, workspace: Workspace) { constructor(vault: Vault, workspace: Workspace, plugin: BetterWordCount) {
this.vault = vault; this.vault = vault;
this.workspace = workspace; this.workspace = workspace;
this.plugin = plugin;
this.debounceChange = debounce( this.debounceChange = debounce(
(text: string) => this.change(text), (text: string) => this.change(text),
50, 50,
@ -76,15 +80,18 @@ export default class StatsManager {
const totalWords = await this.calcTotalWords(); const totalWords = await this.calcTotalWords();
const totalCharacters = await this.calcTotalCharacters(); const totalCharacters = await this.calcTotalCharacters();
const totalSentences = await this.calcTotalSentences(); const totalSentences = await this.calcTotalSentences();
const totalPages = await this.calcTotalPages();
const newDay: Day = { const newDay: Day = {
words: 0, words: 0,
characters: 0, characters: 0,
sentences: 0, sentences: 0,
pages: 0,
files: 0, files: 0,
totalWords: totalWords, totalWords: totalWords,
totalCharacters: totalCharacters, totalCharacters: totalCharacters,
totalSentences: totalSentences, totalSentences: totalSentences,
totalPages: totalPages,
}; };
this.vaultStats.modifiedFiles = {}; this.vaultStats.modifiedFiles = {};
@ -97,6 +104,8 @@ export default class StatsManager {
const currentWords = getWordCount(text); const currentWords = getWordCount(text);
const currentCharacters = getCharacterCount(text); const currentCharacters = getCharacterCount(text);
const currentSentences = getSentenceCount(text); const currentSentences = getSentenceCount(text);
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
if ( if (
this.vaultStats.history.hasOwnProperty(this.today) && this.vaultStats.history.hasOwnProperty(this.today) &&
this.today === moment().format("YYYY-MM-DD") this.today === moment().format("YYYY-MM-DD")
@ -110,9 +119,12 @@ export default class StatsManager {
currentCharacters - modFiles[fileName].characters.current; currentCharacters - modFiles[fileName].characters.current;
this.vaultStats.history[this.today].totalSentences += this.vaultStats.history[this.today].totalSentences +=
currentSentences - modFiles[fileName].sentences.current; currentSentences - modFiles[fileName].sentences.current;
this.vaultStats.history[this.today].totalPages +=
currentPages - modFiles[fileName].pages.current;
modFiles[fileName].words.current = currentWords; modFiles[fileName].words.current = currentWords;
modFiles[fileName].characters.current = currentCharacters; modFiles[fileName].characters.current = currentCharacters;
modFiles[fileName].sentences.current = currentSentences; modFiles[fileName].sentences.current = currentSentences;
modFiles[fileName].pages.current = currentPages;
} else { } else {
modFiles[fileName] = { modFiles[fileName] = {
words: { words: {
@ -127,6 +139,10 @@ export default class StatsManager {
initial: currentSentences, initial: currentSentences,
current: currentSentences, current: currentSentences,
}, },
pages: {
initial: currentPages,
current: currentPages,
},
}; };
} }
@ -145,10 +161,16 @@ export default class StatsManager {
Math.max(0, counts.sentences.current - counts.sentences.initial) Math.max(0, counts.sentences.current - counts.sentences.initial)
) )
.reduce((a, b) => a + b, 0); .reduce((a, b) => a + b, 0);
const pages = Object.values(modFiles)
.map((counts) =>
Math.max(0, counts.pages.current - counts.pages.initial)
)
.reduce((a, b) => a + b, 0);
this.vaultStats.history[this.today].words = words; this.vaultStats.history[this.today].words = words;
this.vaultStats.history[this.today].characters = characters; this.vaultStats.history[this.today].characters = characters;
this.vaultStats.history[this.today].sentences = sentences; this.vaultStats.history[this.today].sentences = sentences;
this.vaultStats.history[this.today].pages = pages;
this.vaultStats.history[this.today].files = this.getTotalFiles(); this.vaultStats.history[this.today].files = this.getTotalFiles();
await this.update(); await this.update();
@ -167,6 +189,7 @@ export default class StatsManager {
todayHist.totalWords = await this.calcTotalWords(); todayHist.totalWords = await this.calcTotalWords();
todayHist.totalCharacters = await this.calcTotalCharacters(); todayHist.totalCharacters = await this.calcTotalCharacters();
todayHist.totalSentences = await this.calcTotalSentences(); todayHist.totalSentences = await this.calcTotalSentences();
todayHist.totalPages = await this.calcTotalPages();
this.update(); this.update();
} else { } else {
this.updateToday(); this.updateToday();
@ -211,6 +234,20 @@ export default class StatsManager {
return sentence; return sentence;
} }
private async calcTotalPages(): Promise<number> {
let pages = 0;
const files = this.vault.getFiles();
for (const i in files) {
const file = files[i];
if (file.extension === "md") {
pages += getPageCount(await this.vault.cachedRead(file), this.plugin.settings.pageWords);
}
}
return pages;
}
public getDailyWords(): number { public getDailyWords(): number {
return this.vaultStats.history[this.today].words; return this.vaultStats.history[this.today].words;
@ -224,6 +261,10 @@ export default class StatsManager {
return this.vaultStats.history[this.today].sentences; return this.vaultStats.history[this.today].sentences;
} }
public getDailyPages(): number {
return this.vaultStats.history[this.today].pages;
}
public getTotalFiles(): number { public getTotalFiles(): number {
return this.vault.getMarkdownFiles().length; return this.vault.getMarkdownFiles().length;
} }
@ -242,4 +283,9 @@ export default class StatsManager {
if (!this.vaultStats) return await this.calcTotalSentences(); if (!this.vaultStats) return await this.calcTotalSentences();
return this.vaultStats.history[this.today].totalSentences; return this.vaultStats.history[this.today].totalSentences;
} }
public async getTotalPages(): Promise<number> {
if (!this.vaultStats) return await this.calcTotalPages();
return this.vaultStats.history[this.today].totalPages;
}
} }

View file

@ -4,6 +4,7 @@ import {
getWordCount, getWordCount,
getCharacterCount, getCharacterCount,
getSentenceCount, getSentenceCount,
getPageCount,
} from "src/utils/StatUtils"; } from "src/utils/StatUtils";
import { debounce } from "obsidian"; import { debounce } from "obsidian";
@ -107,6 +108,26 @@ export default class StatusBar {
: 0)); : 0));
break; break;
} }
} else if (metric.counter === MetricCounter.pages) {
switch (metric.type) {
case MetricType.file:
display = display + getPageCount(text, this.plugin.settings.pageWords);
break;
case MetricType.daily:
display =
display +
(this.plugin.settings.collectStats
? this.plugin.statsManager.getDailyPages()
: 0);
break;
case MetricType.total:
display =
display +
(await (this.plugin.settings.collectStats
? this.plugin.statsManager.getTotalPages()
: 0));
break;
}
} else if (metric.counter === MetricCounter.files) { } else if (metric.counter === MetricCounter.files) {
switch (metric.type) { switch (metric.type) {
case MetricType.file: case MetricType.file:
@ -209,6 +230,26 @@ export default class StatusBar {
: 0)); : 0));
break; break;
} }
} else if (metric.counter === MetricCounter.pages) {
switch (metric.type) {
case MetricType.file:
display = display + 0;
break;
case MetricType.daily:
display =
display +
(this.plugin.settings.collectStats
? this.plugin.statsManager.getDailyPages()
: 0);
break;
case MetricType.total:
display =
display +
(await (this.plugin.settings.collectStats
? this.plugin.statsManager.getTotalPages()
: 0));
break;
}
} else if (metric.counter === MetricCounter.files) { } else if (metric.counter === MetricCounter.files) {
switch (metric.type) { switch (metric.type) {
case MetricType.file: case MetricType.file:

View file

@ -37,6 +37,10 @@ export function getSentenceCount(text: string): number {
return sentences; return sentences;
} }
export function getPageCount(text: string, pageWords: number): number {
return parseFloat((getWordCount(text) / pageWords).toFixed(1));
}
export function getTotalFileCount(vault: Vault): number { export function getTotalFileCount(vault: Vault): number {
return vault.getMarkdownFiles().length; return vault.getMarkdownFiles().length;
} }