diff --git a/bun.lock b/bun.lock index bdc89ca..32c3593 100644 --- a/bun.lock +++ b/bun.lock @@ -1,5 +1,6 @@ { "lockfileVersion": 1, + "configVersion": 0, "workspaces": { "": { "name": "ccstatusline", diff --git a/src/utils/widgets.ts b/src/utils/widgets.ts index 15a2510..a31793a 100644 --- a/src/utils/widgets.ts +++ b/src/utils/widgets.ts @@ -11,6 +11,7 @@ const widgetRegistry = new Map([ ['output-style', new widgets.OutputStyleWidget()], ['git-branch', new widgets.GitBranchWidget()], ['git-changes', new widgets.GitChangesWidget()], + ['git-root-dir', new widgets.GitRootDirWidget()], ['git-worktree', new widgets.GitWorktreeWidget()], ['current-working-dir', new widgets.CurrentWorkingDirWidget()], ['tokens-input', new widgets.TokensInputWidget()], diff --git a/src/widgets/GitRootDir.ts b/src/widgets/GitRootDir.ts new file mode 100644 index 0000000..e96cdd7 --- /dev/null +++ b/src/widgets/GitRootDir.ts @@ -0,0 +1,80 @@ +import { execSync } from 'child_process'; + +import type { RenderContext } from '../types/RenderContext'; +import type { Settings } from '../types/Settings'; +import type { + CustomKeybind, + Widget, + WidgetEditorDisplay, + WidgetItem +} from '../types/Widget'; + +export class GitRootDirWidget implements Widget { + getDefaultColor(): string { return 'cyan'; } + getDescription(): string { return 'Shows the git repository root directory name'; } + getDisplayName(): string { return 'Git Root Dir'; } + getEditorDisplay(item: WidgetItem): WidgetEditorDisplay { + 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 'my-repo'; + } + + const rootDir = this.getGitRootDir(); + if (rootDir) { + const dirName = rootDir.split('/').pop() ?? rootDir; + return dirName; + } + + return hideNoGit ? null : 'no git'; + } + + private getGitRootDir(): string | null { + try { + const rootDir = execSync('git rev-parse --show-toplevel', { + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'ignore'] + }).trim(); + return rootDir || null; + } catch { + return null; + } + } + + 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/index.ts b/src/widgets/index.ts index faaa705..e7d2d15 100644 --- a/src/widgets/index.ts +++ b/src/widgets/index.ts @@ -2,6 +2,7 @@ export { ModelWidget } from './Model'; export { OutputStyleWidget } from './OutputStyle'; export { GitBranchWidget } from './GitBranch'; export { GitChangesWidget } from './GitChanges'; +export { GitRootDirWidget } from './GitRootDir'; export { GitWorktreeWidget } from './GitWorktree'; export { TokensInputWidget } from './TokensInput'; export { TokensOutputWidget } from './TokensOutput';