11import { EventRef , Notice , Platform , Plugin , TAbstractFile , TFile , FileSystemAdapter } from "obsidian" ;
22import { AutoGitSettings , AutoGitSettingTab , DEFAULT_SETTINGS } from "./settings" ;
3- import { getChangedFiles , commitAll , push } from "./git" ;
3+ import { getChangedFiles , commitAll , push , getFileStatuses , FileStatus } from "./git" ;
44import { renderTemplate } from "./template" ;
55import { t } from "./i18n" ;
66
@@ -11,6 +11,8 @@ export default class AutoGitPlugin extends Plugin {
1111 private isCommitting = false ;
1212 private pendingRerun = false ;
1313 private vaultEventRefs : EventRef [ ] = [ ] ;
14+ private statusRefreshInterval : number | null = null ;
15+ private currentStatuses : Map < string , FileStatus > = new Map ( ) ;
1416
1517 async onload ( ) {
1618 await this . loadSettings ( ) ;
@@ -34,11 +36,16 @@ export default class AutoGitPlugin extends Plugin {
3436 } ) ;
3537
3638 this . setupVaultListeners ( ) ;
39+ this . setupStatusBadges ( ) ;
3740 }
3841
3942 onunload ( ) {
4043 this . clearDebounce ( ) ;
4144 this . removeVaultListeners ( ) ;
45+ this . clearStatusBadges ( ) ;
46+ if ( this . statusRefreshInterval ) {
47+ window . clearInterval ( this . statusRefreshInterval ) ;
48+ }
4249 }
4350
4451 async loadSettings ( ) {
@@ -81,6 +88,7 @@ export default class AutoGitPlugin extends Plugin {
8188 if ( this . shouldIgnore ( file . path ) ) return ;
8289
8390 this . scheduleCommit ( ) ;
91+ this . scheduleStatusRefresh ( ) ;
8492 }
8593
8694 private shouldIgnore ( path : string ) : boolean {
@@ -164,6 +172,9 @@ export default class AutoGitPlugin extends Plugin {
164172 if ( this . settings . autoPush ) {
165173 await this . doPush ( ) ;
166174 }
175+
176+ // Refresh badges after commit
177+ this . refreshStatusBadges ( ) ;
167178 } catch ( e ) {
168179 new Notice ( t ( ) . noticeAutoGitError ( ( e as Error ) . message ) ) ;
169180 } finally {
@@ -185,4 +196,75 @@ export default class AutoGitPlugin extends Plugin {
185196 new Notice ( t ( ) . noticePushFailed ( ( e as Error ) . message ) ) ;
186197 }
187198 }
199+
200+ // Status badge functionality
201+ private setupStatusBadges ( ) {
202+ if ( Platform . isMobileApp || ! this . settings . showStatusBadge ) return ;
203+
204+ // Initial refresh
205+ this . refreshStatusBadges ( ) ;
206+
207+ // Refresh every 5 seconds
208+ this . statusRefreshInterval = window . setInterval ( ( ) => {
209+ this . refreshStatusBadges ( ) ;
210+ } , 5000 ) ;
211+ }
212+
213+ private scheduleStatusRefresh ( ) {
214+ if ( ! this . settings . showStatusBadge ) return ;
215+ // Debounced refresh after file change
216+ window . setTimeout ( ( ) => this . refreshStatusBadges ( ) , 500 ) ;
217+ }
218+
219+ refreshStatusBadges ( ) {
220+ if ( ! this . settings . showStatusBadge ) {
221+ this . clearStatusBadges ( ) ;
222+ return ;
223+ }
224+
225+ const cwd = this . getVaultPathSafe ( ) ;
226+ if ( ! cwd ) return ;
227+
228+ getFileStatuses ( cwd , this . settings . gitPath ) . then ( ( statuses ) => {
229+ this . currentStatuses = statuses ;
230+ this . updateBadgesInDOM ( ) ;
231+ } ) ;
232+ }
233+
234+ private clearStatusBadges ( ) {
235+ document . querySelectorAll ( ".git-status-badge" ) . forEach ( ( el ) => el . remove ( ) ) ;
236+ this . currentStatuses . clear ( ) ;
237+ }
238+
239+ private updateBadgesInDOM ( ) {
240+ // Remove old badges
241+ document . querySelectorAll ( ".git-status-badge" ) . forEach ( ( el ) => el . remove ( ) ) ;
242+
243+ // Find file explorer items
244+ const fileItems = document . querySelectorAll ( ".nav-file-title" ) ;
245+
246+ fileItems . forEach ( ( item ) => {
247+ const pathAttr = item . getAttribute ( "data-path" ) ;
248+ if ( ! pathAttr ) return ;
249+
250+ const status = this . currentStatuses . get ( pathAttr ) ;
251+ if ( ! status ) return ;
252+
253+ const badge = document . createElement ( "span" ) ;
254+ badge . className = "git-status-badge" ;
255+ badge . textContent = status === "A" ? "A" : status ;
256+
257+ if ( status === "M" ) {
258+ badge . classList . add ( "modified" ) ;
259+ } else if ( status === "A" ) {
260+ badge . classList . add ( "added" ) ;
261+ } else if ( status === "D" ) {
262+ badge . classList . add ( "deleted" ) ;
263+ } else if ( status === "R" ) {
264+ badge . classList . add ( "renamed" ) ;
265+ }
266+
267+ item . appendChild ( badge ) ;
268+ } ) ;
269+ }
188270}
0 commit comments