Skip to content

Commit 437547f

Browse files
committed
Merge pull request lukeleppan#76 from minermaniac447/master
2 parents d3b6938 + 0712d9f commit 437547f

File tree

8 files changed

+120
-3
lines changed

8 files changed

+120
-3
lines changed

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class BetterWordCount extends Plugin {
3333

3434
// Handle Statistics
3535
if (this.settings.collectStats) {
36-
this.statsManager = new StatsManager(this.app.vault, this.app.workspace);
36+
this.statsManager = new StatsManager(this.app.vault, this.app.workspace, this);
3737
}
3838

3939
// Handle Status Bar

src/settings/Settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export enum MetricCounter {
22
words,
33
characters,
44
sentences,
5+
pages,
56
files,
67
}
78

@@ -38,6 +39,7 @@ export interface BetterWordCountSettings {
3839
altBar: StatusBarItem[];
3940
countComments: boolean;
4041
collectStats: boolean;
42+
pageWords: number;
4143
}
4244

4345
export const DEFAULT_SETTINGS: BetterWordCountSettings = {
@@ -71,4 +73,5 @@ export const DEFAULT_SETTINGS: BetterWordCountSettings = {
7173
],
7274
countComments: false,
7375
collectStats: false,
76+
pageWords: 300,
7477
};

src/settings/SettingsTab.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, PluginSettingTab, Setting, ToggleComponent } from "obsidian";
1+
import { App, PluginSettingTab, Setting, ToggleComponent, TextComponent } from "obsidian";
22
import type BetterWordCount from "src/main";
33
import { addStatusBarSettings } from "./StatusBarSettings";
44

@@ -37,6 +37,18 @@ export default class BetterWordCountSettingsTab extends PluginSettingTab {
3737
await this.plugin.saveSettings();
3838
});
3939
});
40+
new Setting(containerEl)
41+
.setName("Page Word Count")
42+
.setDesc("Set how many words count as one \"page\"")
43+
.addText((text: TextComponent) => {
44+
text.inputEl.type = "number";
45+
text.setPlaceholder("300");
46+
text.setValue(this.plugin.settings.pageWords.toString());
47+
text.onChange(async (value: string) => {
48+
this.plugin.settings.pageWords = parseInt(value);
49+
await this.plugin.saveSettings();
50+
});
51+
});
4052

4153
// Status Bar Settings
4254
addStatusBarSettings(this.plugin, containerEl);

src/settings/StatusBarSettings.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
return "Chars in Note"
1919
case MetricCounter.sentences:
2020
return "Sentences in Note"
21+
case MetricCounter.pages:
22+
return "Pages in Note"
2123
case MetricCounter.files:
2224
return "Total Notes"
2325
}
@@ -29,6 +31,8 @@
2931
return "Daily Chars"
3032
case MetricCounter.sentences:
3133
return "Daily Sentences"
34+
case MetricCounter.pages:
35+
return "Daily Pages"
3236
case MetricCounter.files:
3337
return "Total Notes"
3438
}
@@ -40,6 +44,8 @@
4044
return "Total Chars"
4145
case MetricCounter.sentences:
4246
return "Total Sentences"
47+
case MetricCounter.pages:
48+
return "Total Pages"
4349
case MetricCounter.files:
4450
return "Total Notes"
4551
}
@@ -181,6 +187,7 @@
181187
<option value={MetricCounter.words}>Words</option>
182188
<option value={MetricCounter.characters}>Characters</option>
183189
<option value={MetricCounter.sentences}>Sentences</option>
190+
<option value={MetricCounter.pages}>Pages</option>
184191
<option value={MetricCounter.files}>Files</option>
185192
</select>
186193
</div>
@@ -348,6 +355,7 @@
348355
<option value={MetricCounter.words}>Words</option>
349356
<option value={MetricCounter.characters}>Characters</option>
350357
<option value={MetricCounter.sentences}>Sentences</option>
358+
<option value={MetricCounter.pages}>Pages</option>
351359
<option value={MetricCounter.files}>Files</option>
352360
</select>
353361
</div>

src/stats/Stats.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export interface Day {
99
words: number;
1010
characters: number;
1111
sentences: number;
12+
pages: number;
1213
files: number;
1314
totalWords: number;
1415
totalCharacters: number;
1516
totalSentences: number;
17+
totalPages: number;
1618
}
1719

1820
export type ModifiedFiles = Record<string, FileStat>;
@@ -21,6 +23,7 @@ export interface FileStat {
2123
words: CountDiff;
2224
characters: CountDiff;
2325
sentences: CountDiff;
26+
pages: CountDiff;
2427
}
2528

2629
export interface CountDiff {

src/stats/StatsManager.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
2+
import type BetterWordCount from "../main";
23
import { STATS_FILE } from "../constants";
34
import type { Day, VaultStatistics } from "./Stats";
45
import moment from "moment";
56
import {
67
getCharacterCount,
78
getSentenceCount,
9+
getPageCount,
810
getWordCount,
911
} from "../utils/StatUtils";
1012

1113
export default class StatsManager {
1214
private vault: Vault;
1315
private workspace: Workspace;
16+
private plugin: BetterWordCount;
1417
private vaultStats: VaultStatistics;
1518
private today: string;
1619
public debounceChange;
1720

18-
constructor(vault: Vault, workspace: Workspace) {
21+
constructor(vault: Vault, workspace: Workspace, plugin: BetterWordCount) {
1922
this.vault = vault;
2023
this.workspace = workspace;
24+
this.plugin = plugin;
2125
this.debounceChange = debounce(
2226
(text: string) => this.change(text),
2327
50,
@@ -76,15 +80,18 @@ export default class StatsManager {
7680
const totalWords = await this.calcTotalWords();
7781
const totalCharacters = await this.calcTotalCharacters();
7882
const totalSentences = await this.calcTotalSentences();
83+
const totalPages = await this.calcTotalPages();
7984

8085
const newDay: Day = {
8186
words: 0,
8287
characters: 0,
8388
sentences: 0,
89+
pages: 0,
8490
files: 0,
8591
totalWords: totalWords,
8692
totalCharacters: totalCharacters,
8793
totalSentences: totalSentences,
94+
totalPages: totalPages,
8895
};
8996

9097
this.vaultStats.modifiedFiles = {};
@@ -97,6 +104,8 @@ export default class StatsManager {
97104
const currentWords = getWordCount(text);
98105
const currentCharacters = getCharacterCount(text);
99106
const currentSentences = getSentenceCount(text);
107+
const currentPages = getPageCount(text, this.plugin.settings.pageWords);
108+
100109
if (
101110
this.vaultStats.history.hasOwnProperty(this.today) &&
102111
this.today === moment().format("YYYY-MM-DD")
@@ -110,9 +119,12 @@ export default class StatsManager {
110119
currentCharacters - modFiles[fileName].characters.current;
111120
this.vaultStats.history[this.today].totalSentences +=
112121
currentSentences - modFiles[fileName].sentences.current;
122+
this.vaultStats.history[this.today].totalPages +=
123+
currentPages - modFiles[fileName].pages.current;
113124
modFiles[fileName].words.current = currentWords;
114125
modFiles[fileName].characters.current = currentCharacters;
115126
modFiles[fileName].sentences.current = currentSentences;
127+
modFiles[fileName].pages.current = currentPages;
116128
} else {
117129
modFiles[fileName] = {
118130
words: {
@@ -127,6 +139,10 @@ export default class StatsManager {
127139
initial: currentSentences,
128140
current: currentSentences,
129141
},
142+
pages: {
143+
initial: currentPages,
144+
current: currentPages,
145+
},
130146
};
131147
}
132148

@@ -145,10 +161,16 @@ export default class StatsManager {
145161
Math.max(0, counts.sentences.current - counts.sentences.initial)
146162
)
147163
.reduce((a, b) => a + b, 0);
164+
const pages = Object.values(modFiles)
165+
.map((counts) =>
166+
Math.max(0, counts.pages.current - counts.pages.initial)
167+
)
168+
.reduce((a, b) => a + b, 0);
148169

149170
this.vaultStats.history[this.today].words = words;
150171
this.vaultStats.history[this.today].characters = characters;
151172
this.vaultStats.history[this.today].sentences = sentences;
173+
this.vaultStats.history[this.today].pages = pages;
152174
this.vaultStats.history[this.today].files = this.getTotalFiles();
153175

154176
await this.update();
@@ -167,6 +189,7 @@ export default class StatsManager {
167189
todayHist.totalWords = await this.calcTotalWords();
168190
todayHist.totalCharacters = await this.calcTotalCharacters();
169191
todayHist.totalSentences = await this.calcTotalSentences();
192+
todayHist.totalPages = await this.calcTotalPages();
170193
this.update();
171194
} else {
172195
this.updateToday();
@@ -211,6 +234,20 @@ export default class StatsManager {
211234

212235
return sentence;
213236
}
237+
238+
private async calcTotalPages(): Promise<number> {
239+
let pages = 0;
240+
241+
const files = this.vault.getFiles();
242+
for (const i in files) {
243+
const file = files[i];
244+
if (file.extension === "md") {
245+
pages += getPageCount(await this.vault.cachedRead(file), this.plugin.settings.pageWords);
246+
}
247+
}
248+
249+
return pages;
250+
}
214251

215252
public getDailyWords(): number {
216253
return this.vaultStats.history[this.today].words;
@@ -224,6 +261,10 @@ export default class StatsManager {
224261
return this.vaultStats.history[this.today].sentences;
225262
}
226263

264+
public getDailyPages(): number {
265+
return this.vaultStats.history[this.today].pages;
266+
}
267+
227268
public getTotalFiles(): number {
228269
return this.vault.getMarkdownFiles().length;
229270
}
@@ -242,4 +283,9 @@ export default class StatsManager {
242283
if (!this.vaultStats) return await this.calcTotalSentences();
243284
return this.vaultStats.history[this.today].totalSentences;
244285
}
286+
287+
public async getTotalPages(): Promise<number> {
288+
if (!this.vaultStats) return await this.calcTotalPages();
289+
return this.vaultStats.history[this.today].totalPages;
290+
}
245291
}

src/status/StatusBar.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getWordCount,
55
getCharacterCount,
66
getSentenceCount,
7+
getPageCount,
78
} from "src/utils/StatUtils";
89
import { debounce } from "obsidian";
910

@@ -107,6 +108,26 @@ export default class StatusBar {
107108
: 0));
108109
break;
109110
}
111+
} else if (metric.counter === MetricCounter.pages) {
112+
switch (metric.type) {
113+
case MetricType.file:
114+
display = display + getPageCount(text, this.plugin.settings.pageWords);
115+
break;
116+
case MetricType.daily:
117+
display =
118+
display +
119+
(this.plugin.settings.collectStats
120+
? this.plugin.statsManager.getDailyPages()
121+
: 0);
122+
break;
123+
case MetricType.total:
124+
display =
125+
display +
126+
(await (this.plugin.settings.collectStats
127+
? this.plugin.statsManager.getTotalPages()
128+
: 0));
129+
break;
130+
}
110131
} else if (metric.counter === MetricCounter.files) {
111132
switch (metric.type) {
112133
case MetricType.file:
@@ -209,6 +230,26 @@ export default class StatusBar {
209230
: 0));
210231
break;
211232
}
233+
} else if (metric.counter === MetricCounter.pages) {
234+
switch (metric.type) {
235+
case MetricType.file:
236+
display = display + 0;
237+
break;
238+
case MetricType.daily:
239+
display =
240+
display +
241+
(this.plugin.settings.collectStats
242+
? this.plugin.statsManager.getDailyPages()
243+
: 0);
244+
break;
245+
case MetricType.total:
246+
display =
247+
display +
248+
(await (this.plugin.settings.collectStats
249+
? this.plugin.statsManager.getTotalPages()
250+
: 0));
251+
break;
252+
}
212253
} else if (metric.counter === MetricCounter.files) {
213254
switch (metric.type) {
214255
case MetricType.file:

src/utils/StatUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export function getSentenceCount(text: string): number {
3737
return sentences;
3838
}
3939

40+
export function getPageCount(text: string, pageWords: number): number {
41+
return parseFloat((getWordCount(text) / pageWords).toFixed(1));
42+
}
43+
4044
export function getTotalFileCount(vault: Vault): number {
4145
return vault.getMarkdownFiles().length;
4246
}

0 commit comments

Comments
 (0)