Skip to content

Commit 5e28b94

Browse files
committed
feat: list changed files
1 parent 577fe8b commit 5e28b94

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

main.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawnSync } from "child_process";
2-
import { FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting, SuggestModal } from "obsidian";
2+
import { FileSystemAdapter, FuzzySuggestModal, Notice, Plugin, PluginSettingTab, Setting, SuggestModal } from "obsidian";
33
import simpleGit, { FileStatusResult, SimpleGit } from "simple-git";
44

55
enum PluginState {
@@ -68,6 +68,15 @@ export default class ObsidianGit extends Plugin {
6868
callback: () => new CustomMessageModal(this).open()
6969
});
7070

71+
this.addCommand({
72+
id: "list-changed-files",
73+
name: "List changed files",
74+
callback: async () => {
75+
const status = await this.git.status();
76+
new ChangedFilesModal(this, status.files).open();
77+
}
78+
});
79+
7180
this.init();
7281

7382
// init statusBar
@@ -596,3 +605,41 @@ class CustomMessageModal extends SuggestModal<string> {
596605
}
597606

598607
}
608+
class ChangedFilesModal extends FuzzySuggestModal<FileStatusResult> {
609+
plugin: ObsidianGit;
610+
changedFiles: FileStatusResult[];
611+
612+
constructor(plugin: ObsidianGit, changedFiles: FileStatusResult[]) {
613+
super(plugin.app);
614+
this.plugin = plugin;
615+
this.changedFiles = changedFiles;
616+
console.log(changedFiles);
617+
this.setPlaceholder("Only files in vault can be openend!");
618+
}
619+
620+
getItems(): FileStatusResult[] {
621+
return this.changedFiles;
622+
}
623+
624+
getItemText(item: FileStatusResult): string {
625+
if (item.index == "?" && item.working_dir == "?") {
626+
return `Untracked | ${item.path}`;
627+
}
628+
629+
let working_dir = "";
630+
let index = "";
631+
632+
if (item.working_dir != " ") working_dir = `Working dir: ${item.working_dir} `;
633+
if (item.index != " ") index = `Index: ${item.index}`;
634+
635+
return `${working_dir}${index} | ${item.path}`;
636+
}
637+
638+
onChooseItem(item: FileStatusResult, _: MouseEvent | KeyboardEvent): void {
639+
if (this.plugin.app.metadataCache.getFirstLinkpathDest(item.path, "") == null) {
640+
new Notice("Can't open file in Obsidian");
641+
} else {
642+
this.plugin.app.workspace.openLinkText(item.path, "/");
643+
}
644+
}
645+
}

0 commit comments

Comments
 (0)