Skip to content

Commit

Permalink
feat: adds checkbox chars settings for task stats
Browse files Browse the repository at this point in the history
  • Loading branch information
theotheo committed Mar 7, 2024
1 parent 07ffe1d commit 6e38a41
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
25 changes: 23 additions & 2 deletions src/BelyalovCommanderPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ import { WorkspaceLeaf, TFile, Plugin, TFolder } from "obsidian";
import { around } from "monkey-around";
import BelyalovCommanderView, { VIEW_TYPE, State} from "./BelyalovCommanderView.tsx";
import FileManager from "./FileManager.ts";
import { BelyalovCommanderSettingTab } from "./Settings.ts";

interface BelyalovCommanderSetting {
unfinishedChars: string;
}

const DEFAULT_SETTINGS: Partial<BelyalovCommanderSetting> = {
unfinishedChars: " /",
};

export default class BelyalovCommanderPlugin extends Plugin {
settings!: BelyalovCommanderSetting;
private removePatch!: Function; // TODO: rename
fileManager!: FileManager;
removeBMPatch!: Function; // TODO: rename

public override onload(): void {
public override async onload(): Promise<void> {
this.app.workspace.onLayoutReady(() => {
this.onLayoutReady.bind(this);
this.doPatch(); // TODO: how to test it?
this.registerEvents();
});

this.fileManager = new FileManager(this.app, VIEW_TYPE);
await this.loadSettings();
this.addSettingTab(new BelyalovCommanderSettingTab(this.app, this));

this.fileManager = new FileManager(this.app, VIEW_TYPE, this.settings.unfinishedChars);

this.registerView(
VIEW_TYPE,
Expand Down Expand Up @@ -218,4 +231,12 @@ export default class BelyalovCommanderPlugin extends Plugin {
return leaf.view as BelyalovCommanderView;
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

}
15 changes: 8 additions & 7 deletions src/FileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ export default class FileManager {
vault: Vault;
metadataCache: MetadataCache;
dragManager: any;
unfinishedChars: string;

constructor(app: App, viewType: string) {
constructor(app: App, viewType: string, unfinishedChars: string) {
this.app = app;
this.viewType = viewType;
// TODO: need refactor
this.vault = app.vault;
this.dragManager = (this.app as any).dragManager;
this.metadataCache = app.metadataCache;
this.unfinishedChars = unfinishedChars;
}

// async listFiles(path: string) {
Expand Down Expand Up @@ -156,27 +158,26 @@ export default class FileManager {
}

getTasksStat(
metadata: CachedMetadata,
closedChars = ["x", "-"]
metadata: CachedMetadata
): TaskStat | null {
if (!metadata.listItems) return null;

let total = 0;
let closed = 0;
let unfinished = 0;

metadata.listItems.forEach((item) => {
if (item.task) {
total += 1;

if (closedChars.includes(item.task)) {
closed += 1;
if (this.unfinishedChars.includes(item.task)) {
unfinished += 1;
}
}
});

if (total === 0) return null;

return { total, closed };
return { total, closed: total - unfinished };
}

async getFileWithContent(filepath: string): Promise<FileData> {
Expand Down
32 changes: 32 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import BelyalovCommanderPlugin from "./BelyalovCommanderPlugin.ts";
import { App, PluginSettingTab, Setting } from "obsidian";

export class BelyalovCommanderSettingTab extends PluginSettingTab {
override plugin: BelyalovCommanderPlugin;

constructor(app: App, plugin: BelyalovCommanderPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
let { containerEl } = this;

containerEl.empty();

containerEl.createEl("h1", { text: "You need to reload plugin after setting change", "cls": "notice" });

new Setting(containerEl)
.setName("Unfinished tasks characters")
.setDesc("a string of characters used in checkboxes for unfinished tasks")
.addText((text) =>
text
.setPlaceholder("")
.setValue(this.plugin.settings.unfinishedChars)
.onChange(async (value) => {
this.plugin.settings.unfinishedChars = value;
await this.plugin.saveSettings();
})
);
}
}

0 comments on commit 6e38a41

Please sign in to comment.