Skip to content

Commit

Permalink
Merge pull request #7 from jtprogru/feat-excluded-dirs
Browse files Browse the repository at this point in the history
feat: Add excluded directories
  • Loading branch information
jtprogru authored Jan 7, 2025
2 parents b5f769e + c8a1357 commit c9653d3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Status bar item with vault full statistics including:
- size of vault – total size of all files in the vault
- (new feature) vault quality – number of links divided by number of notes
- (new feature) number of tags – count of all tags in the vault
- (new feature) excluded dirs – comma-separated list of directories excluded from statistics

## Installation

Expand Down
50 changes: 29 additions & 21 deletions src/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class FullVaultMetricsCollector {
private metadataCache: MetadataCache;
private data: Map<string, FullVaultMetrics> = new Map();
private backlog: Array<string> = new Array();
private excludeDirectories: string;
private vaultMetrics: FullVaultMetrics = new FullVaultMetrics();

constructor(owner: Component) {
Expand All @@ -27,6 +28,11 @@ export class FullVaultMetricsCollector {
return this;
}

public setExcludeDirectories(excludeDirectories: string) {
this.excludeDirectories = excludeDirectories;
return this;
}

public setMetadataCache(metadataCache: MetadataCache) {
this.metadataCache = metadataCache;
return this;
Expand Down Expand Up @@ -55,6 +61,8 @@ export class FullVaultMetricsCollector {
});
this.owner.registerInterval(+setInterval(() => { this.processBacklog() }, 2000));

this.setExcludeDirectories(this.excludeDirectories);

return this;
}

Expand All @@ -72,7 +80,7 @@ export class FullVaultMetricsCollector {
private async processBacklog() {
while (this.backlog.length > 0) {
let path = this.backlog.shift();
if (path == null) {
if ((path == null) || (path.split("/").some((dir) => this.excludeDirectories.includes(dir)))) {
break;
}
// console.log(`processing ${path}`);
Expand Down Expand Up @@ -201,26 +209,26 @@ class NoteMetricsCollector {
metrics.size = file.stat?.size;
metrics.links = metadata?.links?.length || 0;
const words = await this.vault.cachedRead(file).then((content: string) => {
return metadata.sections?.map(section => {
const sectionType = section.type;
const startOffset = section.position?.start?.offset;
const endOffset = section.position?.end?.offset;
const tokenizer = NoteMetricsCollector.TOKENIZERS.get(sectionType);
if (!tokenizer) {
console.log(`${file.path}: no tokenizer, section.type=${section.type}`);
return 0;
} else {
const tokens = tokenizer.tokenize(content.substring(startOffset, endOffset));
return tokens.length;
}
}).reduce((a, b) => a + b, 0);
}).catch((e) => {
console.log(`${file.path} ${e}`);
return 0;
});
metrics.words = words ?? 0;
metrics.quality = metrics.links / metrics.notes ?? 0.0;
metrics.tags = metadata?.tags?.length ?? 0;
return metadata.sections?.map(section => {
const sectionType = section.type;
const startOffset = section.position?.start?.offset;
const endOffset = section.position?.end?.offset;
const tokenizer = NoteMetricsCollector.TOKENIZERS.get(sectionType);
if (!tokenizer) {
console.log(`${file.path}: no tokenizer, section.type=${section.type}`);
return 0;
} else {
const tokens = tokenizer.tokenize(content.substring(startOffset, endOffset));
return tokens.length;
}
}).reduce((a, b) => a + b, 0);
}).catch((e) => {
console.log(`${file.path} ${e}`);
return 0;
});
metrics.words = words ?? 0;
metrics.quality = metrics.notes !== 0 ? (metrics.links / metrics.notes) : 0.0;
metrics.tags = metadata?.tags?.length ?? 0;

return metrics;
}
Expand Down
19 changes: 10 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class FullStatisticsPlugin extends Plugin {
setVault(this.app.vault).
setMetadataCache(this.app.metadataCache).
setFullVaultMetrics(this.vaultMetrics).
setExcludeDirectories(this.settings.excludeDirectories).
start();

this.statusBarItem = new FullStatisticsStatusBarItem(this, this.addStatusBarItem()).
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class FullStatisticsPlugin extends Plugin {
class StatisticView {

/** Root node for the {@link StatisticView}. */
private containerEl: HTMLElement;
private containerElementsForVaultFullStatistics: HTMLElement;

/** Formatter that extracts and formats a value from a {@link Statistics} instance. */
private formatter: (s: FullVaultMetrics) => string;
Expand All @@ -74,15 +75,15 @@ class StatisticView {
* @param containerEl The parent element for the view.
*/
constructor(containerEl: HTMLElement) {
this.containerEl = containerEl.createSpan({ cls: ["obsidian-vault-full-statistics--item"] });
this.containerElementsForVaultFullStatistics = containerEl.createSpan({ cls: ["obsidian-vault-full-statistics--item"] });
this.setActive(false);
}

/**
* Sets the name of the statistic.
*/
setStatisticName(name: string): StatisticView {
this.containerEl.addClass(`obsidian-vault-full-statistics--item-${name}`);
this.containerElementsForVaultFullStatistics.addClass(`obsidian-vault-full-statistics--item-${name}`);
return this;
}

Expand All @@ -103,13 +104,13 @@ class StatisticView {
* mutually exclusive.
*/
setActive(isActive: boolean): StatisticView {
this.containerEl.removeClass("obsidian-vault-full-statistics--item--active");
this.containerEl.removeClass("obsidian-vault-full-statistics--item--inactive");
this.containerElementsForVaultFullStatistics.removeClass("obsidian-vault-full-statistics--item--active");
this.containerElementsForVaultFullStatistics.removeClass("obsidian-vault-full-statistics--item--inactive");

if (isActive) {
this.containerEl.addClass("obsidian-vault-full-statistics--item--active");
this.containerElementsForVaultFullStatistics.addClass("obsidian-vault-full-statistics--item--active");
} else {
this.containerEl.addClass("obsidian-vault-full-statistics--item--inactive");
this.containerElementsForVaultFullStatistics.addClass("obsidian-vault-full-statistics--item--inactive");
}

return this;
Expand All @@ -120,14 +121,14 @@ class StatisticView {
* Statistics}.
*/
refresh(s: FullVaultMetrics) {
this.containerEl.setText(this.formatter(s));
this.containerElementsForVaultFullStatistics.setText(this.formatter(s));
}

/**
* Returns the text content of the view.
*/
getText(): string {
return this.containerEl.getText();
return this.containerElementsForVaultFullStatistics.getText();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class FullVaultMetrics extends Events implements FullVaultMetrics {
words: number = 0;
quality: number = 0.00001;
tags: number = 0;

public reset() {
this.files = 0;
this.notes = 0;
Expand Down
15 changes: 14 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { App, PluginSettingTab, Setting } from "obsidian";
import StatisticsPlugin from "./main";

export interface FullStatisticsPluginSettings {
excludeDirectories: string,
displayIndividualItems: boolean,
showNotes: boolean,
showAttachments: boolean,
Expand All @@ -26,7 +27,19 @@ export class FullStatisticsPluginSettingTab extends PluginSettingTab {
let { containerEl } = this;

containerEl.empty();


new Setting(containerEl)
.setName("Exclude directories")
.setDesc("Exclude directories from statistics")
.addText((value) => {
value
.setValue(this.plugin.settings.excludeDirectories)
.onChange(async (value) => {
this.plugin.settings.excludeDirectories = value;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName("Show individual items")
.setDesc("Whether to show multiple items at once or cycle them with a click")
Expand Down

0 comments on commit c9653d3

Please sign in to comment.