Skip to content

Commit

Permalink
Merge pull request #112 from NullCub3/stats-location-config
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeleppan authored Jan 23, 2024
2 parents 49ed5c1 + 7ff940f commit cfa194c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "better-word-count",
"name": "Better Word Count",
"version": "0.10.0",
"version": "0.10.1",
"description": "Counts the words of selected text in the editor.",
"author": "Luke Leppan",
"authorUrl": "https://lukeleppan.com",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "better-word-count",
"version": "0.10.0",
"version": "0.10.1",
"description": "Counts the words of selected text in the editor.",
"main": "main.js",
"types": "src/api.d.ts",
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const VIEW_TYPE_STATS = "vault-stats";
export const STATS_FILE = ".obsidian/vault-stats.json";
export const STATS_ICON = `<g transform="matrix(0.95,0,0,0.95,2.5,2.5)"><path fill="currentColor" stroke="currentColor" d="M3.77,100L22.421,100C24.503,100 26.19,98.013 26.19,95.561L26.19,34.813C26.19,32.361 24.503,30.374 22.421,30.374L3.77,30.374C1.688,30.374 -0,32.361 -0,34.813L-0,95.561C-0,98.013 1.688,100 3.77,100ZM40.675,100L59.325,100C61.408,100 63.095,98.013 63.095,95.561L63.095,4.439C63.095,1.987 61.408,-0 59.325,-0L40.675,-0C38.592,-0 36.905,1.987 36.905,4.439L36.905,95.561C36.905,98.013 38.592,100 40.675,100ZM77.579,100L96.23,100C98.312,100 100,98.013 100,95.561L100,46.495C100,44.043 98.312,42.056 96.23,42.056L77.579,42.056C75.497,42.056 73.81,44.043 73.81,46.495L73.81,95.561C73.81,98.013 75.497,100 77.579,100Z" style="fill:none;fill-rule:nonzero;stroke-width:8px;"/></g>`;
export const STATS_ICON_NAME = "stats-graph";
export const MATCH_HTML_COMMENT = new RegExp(
Expand Down
2 changes: 2 additions & 0 deletions src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface BetterWordCountSettings {
collectStats: boolean;
pageWords: number;
displaySectionCounts: boolean;
statsPath: string;
}

export const DEFAULT_SETTINGS: BetterWordCountSettings = {
Expand Down Expand Up @@ -78,4 +79,5 @@ export const DEFAULT_SETTINGS: BetterWordCountSettings = {
collectStats: false,
displaySectionCounts: false,
pageWords: 300,
statsPath: ".obsidian/vault-stats.json",
};
17 changes: 16 additions & 1 deletion src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class BetterWordCountSettingsTab extends PluginSettingTab {
new Setting(containerEl)
.setName("Collect Statistics")
.setDesc(
"Reload Required for change to take effect. Turn on to start collecting daily statistics of your writing. Stored in the vault-stats.json file in the .obsidian of your vault. This is required for counts of the day as well as total counts."
"Reload required for change to take effect. Turn on to start collecting daily statistics of your writing. Stored in the path specified below. This is required for counts of the day as well as total counts."
)
.addToggle((cb: ToggleComponent) => {
cb.setValue(this.plugin.settings.collectStats);
Expand Down Expand Up @@ -61,6 +61,21 @@ export default class BetterWordCountSettingsTab extends PluginSettingTab {
});
});

// Advanced Settings
containerEl.createEl("h4", { text: "Advanced Settings" });
new Setting(containerEl)
.setName("Vault Stats File Path")
.setDesc("Reload required for change to take effect. The location of the vault statistics file, relative to the vault root.")
.addText((text: TextComponent) => {
text.setPlaceholder(".obsidian/vault-stats.json");
text.setValue(this.plugin.settings.statsPath.toString());
text.onChange(async (value: string) => {
this.plugin.settings.statsPath = value;
await this.plugin.saveSettings();
});
});


// Status Bar Settings
addStatusBarSettings(this.plugin, containerEl);
}
Expand Down
15 changes: 7 additions & 8 deletions src/stats/StatsManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
import type BetterWordCount from "../main";
import { STATS_FILE } from "../constants";
import type { Day, VaultStatistics } from "./Stats";
import moment from "moment";
import {
Expand Down Expand Up @@ -45,32 +44,32 @@ export default class StatsManager {
}
});

this.vault.adapter.exists(STATS_FILE).then(async (exists) => {
this.vault.adapter.exists(this.plugin.settings.statsPath).then(async (exists) => {
if (!exists) {
const vaultSt: VaultStatistics = {
history: {},
modifiedFiles: {},
};
await this.vault.adapter.write(STATS_FILE, JSON.stringify(vaultSt));
this.vaultStats = JSON.parse(await this.vault.adapter.read(STATS_FILE));
await this.vault.adapter.write(this.plugin.settings.statsPath, JSON.stringify(vaultSt));
this.vaultStats = JSON.parse(await this.vault.adapter.read(this.plugin.settings.statsPath));
} else {
this.vaultStats = JSON.parse(await this.vault.adapter.read(STATS_FILE));
this.vaultStats = JSON.parse(await this.vault.adapter.read(this.plugin.settings.statsPath));
if (!this.vaultStats.hasOwnProperty("history")) {
const vaultSt: VaultStatistics = {
history: {},
modifiedFiles: {},
};
await this.vault.adapter.write(STATS_FILE, JSON.stringify(vaultSt));
await this.vault.adapter.write(this.plugin.settings.statsPath, JSON.stringify(vaultSt));
}
this.vaultStats = JSON.parse(await this.vault.adapter.read(STATS_FILE));
this.vaultStats = JSON.parse(await this.vault.adapter.read(this.plugin.settings.statsPath));
}

await this.updateToday();
});
}

async update(): Promise<void> {
this.vault.adapter.write(STATS_FILE, JSON.stringify(this.vaultStats));
this.vault.adapter.write(this.plugin.settings.statsPath, JSON.stringify(this.vaultStats));
}

async updateToday(): Promise<void> {
Expand Down

0 comments on commit cfa194c

Please sign in to comment.