From 0a8168801075ba4c214a04083dbc69035dc24113 Mon Sep 17 00:00:00 2001 From: Matthew Breedlove Date: Wed, 27 Aug 2025 11:03:05 -0400 Subject: [PATCH] Added toggle to hide 'no git' message if not in a git repository. Fixes #48 --- src/widgets/GitBranch.ts | 37 +++++++++++++++++++++++++++++++++++-- src/widgets/GitChanges.ts | 37 +++++++++++++++++++++++++++++++++++-- src/widgets/GitWorktree.ts | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 105 insertions(+), 6 deletions(-) diff --git a/src/widgets/GitBranch.ts b/src/widgets/GitBranch.ts index c61984d..e96cd70 100644 --- a/src/widgets/GitBranch.ts +++ b/src/widgets/GitBranch.ts @@ -3,6 +3,7 @@ import { execSync } from 'child_process'; import type { RenderContext } from '../types/RenderContext'; import type { Settings } from '../types/Settings'; import type { + CustomKeybind, Widget, WidgetEditorDisplay, WidgetItem @@ -13,10 +14,36 @@ export class GitBranchWidget implements Widget { getDescription(): string { return 'Shows the current git branch name'; } getDisplayName(): string { return 'Git Branch'; } getEditorDisplay(item: WidgetItem): WidgetEditorDisplay { - return { displayText: this.getDisplayName() }; + const hideNoGit = item.metadata?.hideNoGit === 'true'; + const modifiers: string[] = []; + + if (hideNoGit) { + modifiers.push('hide \'no git\''); + } + + return { + displayText: this.getDisplayName(), + modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined + }; + } + + handleEditorAction(action: string, item: WidgetItem): WidgetItem | null { + if (action === 'toggle-nogit') { + const currentState = item.metadata?.hideNoGit === 'true'; + return { + ...item, + metadata: { + ...item.metadata, + hideNoGit: (!currentState).toString() + } + }; + } + return null; } render(item: WidgetItem, context: RenderContext, settings: Settings): string | null { + const hideNoGit = item.metadata?.hideNoGit === 'true'; + if (context.isPreview) { return item.rawValue ? 'main' : '⎇ main'; } @@ -25,7 +52,7 @@ export class GitBranchWidget implements Widget { if (branch) return item.rawValue ? branch : `⎇ ${branch}`; - return '⎇ no git'; + return hideNoGit ? null : '⎇ no git'; } private getGitBranch(): string | null { @@ -40,6 +67,12 @@ export class GitBranchWidget implements Widget { } } + getCustomKeybinds(): CustomKeybind[] { + return [ + { key: 'h', label: '(h)ide \'no git\' message', action: 'toggle-nogit' } + ]; + } + supportsRawValue(): boolean { return true; } supportsColors(item: WidgetItem): boolean { return true; } } \ No newline at end of file diff --git a/src/widgets/GitChanges.ts b/src/widgets/GitChanges.ts index c2d8609..922a268 100644 --- a/src/widgets/GitChanges.ts +++ b/src/widgets/GitChanges.ts @@ -3,6 +3,7 @@ import { execSync } from 'child_process'; import type { RenderContext } from '../types/RenderContext'; import type { Settings } from '../types/Settings'; import type { + CustomKeybind, Widget, WidgetEditorDisplay, WidgetItem @@ -13,10 +14,36 @@ export class GitChangesWidget implements Widget { getDescription(): string { return 'Shows git changes count (+insertions, -deletions)'; } getDisplayName(): string { return 'Git Changes'; } getEditorDisplay(item: WidgetItem): WidgetEditorDisplay { - return { displayText: this.getDisplayName() }; + const hideNoGit = item.metadata?.hideNoGit === 'true'; + const modifiers: string[] = []; + + if (hideNoGit) { + modifiers.push('hide \'no git\''); + } + + return { + displayText: this.getDisplayName(), + modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined + }; + } + + handleEditorAction(action: string, item: WidgetItem): WidgetItem | null { + if (action === 'toggle-nogit') { + const currentState = item.metadata?.hideNoGit === 'true'; + return { + ...item, + metadata: { + ...item.metadata, + hideNoGit: (!currentState).toString() + } + }; + } + return null; } render(item: WidgetItem, context: RenderContext, settings: Settings): string | null { + const hideNoGit = item.metadata?.hideNoGit === 'true'; + if (context.isPreview) { return '(+42,-10)'; } @@ -25,7 +52,7 @@ export class GitChangesWidget implements Widget { if (changes) return `(+${changes.insertions},-${changes.deletions})`; else - return '(no git)'; + return hideNoGit ? null : '(no git)'; } private getGitChanges(): { insertions: number; deletions: number } | null { @@ -63,6 +90,12 @@ export class GitChangesWidget implements Widget { } } + getCustomKeybinds(): CustomKeybind[] { + return [ + { key: 'h', label: '(h)ide \'no git\' message', action: 'toggle-nogit' } + ]; + } + supportsRawValue(): boolean { return false; } supportsColors(item: WidgetItem): boolean { return true; } } \ No newline at end of file diff --git a/src/widgets/GitWorktree.ts b/src/widgets/GitWorktree.ts index b9558d9..8c16fee 100644 --- a/src/widgets/GitWorktree.ts +++ b/src/widgets/GitWorktree.ts @@ -2,6 +2,7 @@ import { execSync } from 'child_process'; import type { RenderContext } from '../types/RenderContext'; import type { + CustomKeybind, Widget, WidgetEditorDisplay, WidgetItem @@ -12,10 +13,36 @@ export class GitWorktreeWidget implements Widget { getDescription(): string { return 'Shows the current git worktree name'; } getDisplayName(): string { return 'Git Worktree'; } getEditorDisplay(item: WidgetItem): WidgetEditorDisplay { - return { displayText: this.getDisplayName() }; + const hideNoGit = item.metadata?.hideNoGit === 'true'; + const modifiers: string[] = []; + + if (hideNoGit) { + modifiers.push('hide \'no git\''); + } + + return { + displayText: this.getDisplayName(), + modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined + }; + } + + handleEditorAction(action: string, item: WidgetItem): WidgetItem | null { + if (action === 'toggle-nogit') { + const currentState = item.metadata?.hideNoGit === 'true'; + return { + ...item, + metadata: { + ...item.metadata, + hideNoGit: (!currentState).toString() + } + }; + } + return null; } render(item: WidgetItem, context: RenderContext): string | null { + const hideNoGit = item.metadata?.hideNoGit === 'true'; + if (context.isPreview) return item.rawValue ? 'main' : '𖠰 main'; @@ -23,7 +50,7 @@ export class GitWorktreeWidget implements Widget { if (worktree) return item.rawValue ? worktree : `𖠰 ${worktree}`; - return '𖠰 no git'; + return hideNoGit ? null : '𖠰 no git'; } private getGitWorktree(): string | null { @@ -46,6 +73,12 @@ export class GitWorktreeWidget implements Widget { } } + getCustomKeybinds(): CustomKeybind[] { + return [ + { key: 'h', label: '(h)ide \'no git\' message', action: 'toggle-nogit' } + ]; + } + supportsRawValue(): boolean { return true; } supportsColors(item: WidgetItem): boolean { return true; } } \ No newline at end of file