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 src/utils/__tests__/widgets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('widget catalog', () => {

expect(categories).toContain('Core');
expect(categories).toContain('Git');
expect(categories).toContain('Jujutsu');
expect(categories).toContain('Context');
expect(categories).toContain('Tokens');
expect(categories).toContain('Session');
Expand Down
23 changes: 23 additions & 0 deletions src/utils/jj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { execSync } from 'child_process';

import type { RenderContext } from '../types/RenderContext';
import { resolveGitCwd } from './git';

export function runJj(command: string, context: RenderContext): string | null {
try {
const cwd = resolveGitCwd(context);
const output = execSync(`jj ${command}`, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore'],
...(cwd ? { cwd } : {})
}).trim();

return output.length > 0 ? output : null;
} catch {
return null;
}
}

export function isInsideJjRepo(context: RenderContext): boolean {
return runJj('root', context) !== null;
}
7 changes: 6 additions & 1 deletion src/utils/widgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const widgetRegistry = new Map<WidgetItemType, Widget>([
['git-deletions', new widgets.GitDeletionsWidget()],
['git-root-dir', new widgets.GitRootDirWidget()],
['git-worktree', new widgets.GitWorktreeWidget()],
['jj-bookmarks', new widgets.JjBookmarksWidget()],
['jj-workspace', new widgets.JjWorkspaceWidget()],
['jj-root-dir', new widgets.JjRootDirWidget()],
['jj-changes', new widgets.JjChangesWidget()],
['jj-revision', new widgets.JjRevisionWidget()],
['current-working-dir', new widgets.CurrentWorkingDirWidget()],
['tokens-input', new widgets.TokensInputWidget()],
['tokens-output', new widgets.TokensOutputWidget()],
Expand Down Expand Up @@ -186,4 +191,4 @@ export function isKnownWidgetType(type: string): boolean {
return widgetRegistry.has(type)
|| type === 'separator'
|| type === 'flex-separator';
}
}
88 changes: 88 additions & 0 deletions src/widgets/JjBookmarks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { RenderContext } from '../types/RenderContext';
import type { Settings } from '../types/Settings';
import type {
CustomKeybind,
Widget,
WidgetEditorDisplay,
WidgetItem
} from '../types/Widget';
import {
isInsideJjRepo,
runJj
} from '../utils/jj';

export class JjBookmarksWidget implements Widget {
getDefaultColor(): string { return 'magenta'; }
getDescription(): string { return 'Shows the current jujutsu bookmark(s)'; }
getDisplayName(): string { return 'JJ Bookmarks'; }
getCategory(): string { return 'Jujutsu'; }
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
const hideNoJj = item.metadata?.hideNoJj === 'true';
const modifiers: string[] = [];

if (hideNoJj) {
modifiers.push('hide \'no jj\'');
}

return {
displayText: this.getDisplayName(),
modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined
};
}

handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
if (action === 'toggle-nojj') {
const currentState = item.metadata?.hideNoJj === 'true';
return {
...item,
metadata: {
...item.metadata,
hideNoJj: (!currentState).toString()
}
};
}
return null;
}

render(item: WidgetItem, context: RenderContext, _settings: Settings): string | null {
const hideNoJj = item.metadata?.hideNoJj === 'true';

if (context.isPreview) {
return item.rawValue ? 'main' : '🔖 main';
}

if (!isInsideJjRepo(context)) {
return hideNoJj ? null : '🔖 no jj';
}

const bookmarks = this.getJjBookmarks(context);
if (bookmarks) {
return item.rawValue ? bookmarks : `🔖 ${bookmarks}`;
}

return hideNoJj ? null : '🔖 (none)';
}

private getJjBookmarks(context: RenderContext): string | null {
const output = runJj('log --no-graph -r \'heads(::@ & bookmarks())\' --template bookmarks', context);
if (!output) {
return null;
}

const bookmarks = output.split(/\s+/).filter(Boolean);
if (bookmarks.length === 0) {
return null;
}

return bookmarks.join(', ');
}

getCustomKeybinds(): CustomKeybind[] {
return [
{ key: 'h', label: '(h)ide \'no jj\' message', action: 'toggle-nojj' }
];
}

supportsRawValue(): boolean { return true; }
supportsColors(): boolean { return true; }
}
94 changes: 94 additions & 0 deletions src/widgets/JjChanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import type { RenderContext } from '../types/RenderContext';
import type { Settings } from '../types/Settings';
import type {
CustomKeybind,
Widget,
WidgetEditorDisplay,
WidgetItem
} from '../types/Widget';
import {
isInsideJjRepo,
runJj
} from '../utils/jj';

export class JjChangesWidget implements Widget {
getDefaultColor(): string { return 'yellow'; }
getDescription(): string { return 'Shows jujutsu changes count (+insertions, -deletions)'; }
getDisplayName(): string { return 'JJ Changes'; }
getCategory(): string { return 'Jujutsu'; }
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
const hideNoJj = item.metadata?.hideNoJj === 'true';
const modifiers: string[] = [];

if (hideNoJj) {
modifiers.push('hide \'no jj\'');
}

return {
displayText: this.getDisplayName(),
modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined
};
}

handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
if (action === 'toggle-nojj') {
const currentState = item.metadata?.hideNoJj === 'true';
return {
...item,
metadata: {
...item.metadata,
hideNoJj: (!currentState).toString()
}
};
}
return null;
}

render(item: WidgetItem, context: RenderContext, _settings: Settings): string | null {
const hideNoJj = item.metadata?.hideNoJj === 'true';

if (context.isPreview) {
return '(+42,-10)';
}

if (!isInsideJjRepo(context)) {
return hideNoJj ? null : '(no jj)';
}

const changes = this.getJjChanges(context);
if (changes) {
return `(+${changes.insertions},-${changes.deletions})`;
}

return hideNoJj ? null : '(no jj)';
}

private getJjChanges(context: RenderContext): { insertions: number; deletions: number } | null {
const stat = runJj('diff --stat', context);

let totalInsertions = 0;
let totalDeletions = 0;

if (stat) {
const lines = stat.split('\n');
const summaryLine = lines[lines.length - 1];
if (summaryLine) {
const insertMatch = /(\d+) insertion/.exec(summaryLine);
const deleteMatch = /(\d+) deletion/.exec(summaryLine);
totalInsertions += insertMatch?.[1] ? parseInt(insertMatch[1], 10) : 0;
totalDeletions += deleteMatch?.[1] ? parseInt(deleteMatch[1], 10) : 0;
}
}

return { insertions: totalInsertions, deletions: totalDeletions };
}

getCustomKeybinds(): CustomKeybind[] {
return [
{ key: 'h', label: '(h)ide \'no jj\' message', action: 'toggle-nojj' }
];
}

supportsRawValue(): boolean { return false; }
supportsColors(): boolean { return true; }
}
78 changes: 78 additions & 0 deletions src/widgets/JjRevision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { RenderContext } from '../types/RenderContext';
import type { Settings } from '../types/Settings';
import type {
CustomKeybind,
Widget,
WidgetEditorDisplay,
WidgetItem
} from '../types/Widget';
import {
isInsideJjRepo,
runJj
} from '../utils/jj';

export class JjRevisionWidget implements Widget {
getDefaultColor(): string { return 'green'; }
getDescription(): string { return 'Shows the current jujutsu change ID (short)'; }
getDisplayName(): string { return 'JJ Revision'; }
getCategory(): string { return 'Jujutsu'; }
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
const hideNoJj = item.metadata?.hideNoJj === 'true';
const modifiers: string[] = [];

if (hideNoJj) {
modifiers.push('hide \'no jj\'');
}

return {
displayText: this.getDisplayName(),
modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined
};
}

handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
if (action === 'toggle-nojj') {
const currentState = item.metadata?.hideNoJj === 'true';
return {
...item,
metadata: {
...item.metadata,
hideNoJj: (!currentState).toString()
}
};
}
return null;
}

render(item: WidgetItem, context: RenderContext, _settings: Settings): string | null {
const hideNoJj = item.metadata?.hideNoJj === 'true';

if (context.isPreview) {
return item.rawValue ? 'kkmpptxz' : ' kkmpptxz';
}

if (!isInsideJjRepo(context)) {
return hideNoJj ? null : ' no jj';
}

const changeId = this.getJjRevision(context);
if (changeId) {
return item.rawValue ? changeId : ` ${changeId}`;
}

return hideNoJj ? null : ' no jj';
}

private getJjRevision(context: RenderContext): string | null {
return runJj('log --no-graph -r @ -T \'change_id.shortest()\'', context);
}

getCustomKeybinds(): CustomKeybind[] {
return [
{ key: 'h', label: '(h)ide \'no jj\' message', action: 'toggle-nojj' }
];
}

supportsRawValue(): boolean { return true; }
supportsColors(): boolean { return true; }
}
82 changes: 82 additions & 0 deletions src/widgets/JjRootDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { RenderContext } from '../types/RenderContext';
import type { Settings } from '../types/Settings';
import type {
CustomKeybind,
Widget,
WidgetEditorDisplay,
WidgetItem
} from '../types/Widget';
import {
isInsideJjRepo,
runJj
} from '../utils/jj';

export class JjRootDirWidget implements Widget {
getDefaultColor(): string { return 'cyan'; }
getDescription(): string { return 'Shows the jujutsu repository root directory name'; }
getDisplayName(): string { return 'JJ Root Dir'; }
getCategory(): string { return 'Jujutsu'; }
getEditorDisplay(item: WidgetItem): WidgetEditorDisplay {
const hideNoJj = item.metadata?.hideNoJj === 'true';
const modifiers: string[] = [];

if (hideNoJj) {
modifiers.push('hide \'no jj\'');
}

return {
displayText: this.getDisplayName(),
modifierText: modifiers.length > 0 ? `(${modifiers.join(', ')})` : undefined
};
}

handleEditorAction(action: string, item: WidgetItem): WidgetItem | null {
if (action === 'toggle-nojj') {
const currentState = item.metadata?.hideNoJj === 'true';
return {
...item,
metadata: {
...item.metadata,
hideNoJj: (!currentState).toString()
}
};
}
return null;
}

render(item: WidgetItem, context: RenderContext, _settings: Settings): string | null {
const hideNoJj = item.metadata?.hideNoJj === 'true';

if (context.isPreview) {
return 'my-repo';
}

if (!isInsideJjRepo(context)) {
return hideNoJj ? null : 'no jj';
}

const rootDir = runJj('root', context);
if (rootDir) {
return this.getRootDirName(rootDir);
}

return hideNoJj ? null : 'no jj';
}

private getRootDirName(rootDir: string): string {
const trimmedRootDir = rootDir.replace(/[\\/]+$/, '');
const normalizedRootDir = trimmedRootDir.length > 0 ? trimmedRootDir : rootDir;
const parts = normalizedRootDir.split(/[\\/]/).filter(Boolean);
const lastPart = parts[parts.length - 1];
return lastPart && lastPart.length > 0 ? lastPart : normalizedRootDir;
}

getCustomKeybinds(): CustomKeybind[] {
return [
{ key: 'h', label: '(h)ide \'no jj\' message', action: 'toggle-nojj' }
];
}

supportsRawValue(): boolean { return false; }
supportsColors(): boolean { return true; }
}
Loading