Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bun.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"lockfileVersion": 1,
"configVersion": 0,
"workspaces": {
"": {
"name": "ccstatusline",
Expand Down
1 change: 1 addition & 0 deletions src/utils/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const widgetRegistry = new Map<WidgetItemType, Widget>([
['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()],
Expand Down
80 changes: 80 additions & 0 deletions src/widgets/GitRootDir.ts
Original file line number Diff line number Diff line change
@@ -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; }
}
1 change: 1 addition & 0 deletions src/widgets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down