Skip to content
Merged
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
51 changes: 51 additions & 0 deletions src/tui/components/__tests__/ItemsEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,57 @@ describe('ItemsEditor', () => {
}
});

it('turns fish style off when a cwd segment count is saved', async () => {
const stdin = createMockStdin();
const stdout = createMockStdout();
const stderr = createMockStdout();
// Debug mode writes whole frames; the last one starts at the last title
const lastFrame = () => {
const output = stripAnsi(stdout.getOutput());
return output.slice(output.lastIndexOf('Edit Line 1'));
};

const instance = render(
React.createElement(StatefulItemsEditor, { initialWidgets: [{ id: '1', type: 'current-working-dir', metadata: { fishStyle: 'true' } }] }),
{
stdin,
stdout,
stderr,
debug: true,
exitOnCtrlC: false,
patchConsole: false
}
);

try {
await waitFor(() => {
expect(lastFrame()).toContain('β–Ά 1. Current Working Dir (fish-style)');
});

stdin.write('s');
await waitFor(() => {
expect(lastFrame()).toContain('Enter number of segments to display');
});

stdin.write('2');
await waitFor(() => {
expect(lastFrame()).toContain('(blank for full path): 2');
});

stdin.write('\r');
await waitFor(() => {
expect(lastFrame()).toContain('β–Ά 1. Current Working Dir (segments: 2)');
});
expect(lastFrame()).not.toContain('fish-style');
} finally {
instance.unmount();
instance.cleanup();
stdin.destroy();
stdout.destroy();
stderr.destroy();
}
});

describe('widget picker', () => {
const ESC = '\x1b';
const ENTER = '\r';
Expand Down
30 changes: 16 additions & 14 deletions src/widgets/CurrentWorkingDir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getSymbolKeybind,
renderSymbolOverrideEditor
} from './shared/symbol-override';
import { getGraphemes } from './shared/text-cursor';

const LABEL = 'cwd: ';
const LINK_KEY = 'linkTo';
Expand Down Expand Up @@ -124,8 +125,9 @@ export class CurrentWorkingDirWidget implements Widget {
}
};
} else {
// When disabling fish-style
const { fishStyle, ...restMetadata } = item.metadata ?? {};
// When disabling fish-style, also drop a segment count saved
// next to it, so a count hidden behind fish style doesn't apply
const { fishStyle, segments, ...restMetadata } = item.metadata ?? {};

return {
...item,
Expand Down Expand Up @@ -309,22 +311,20 @@ export class CurrentWorkingDirWidget implements Widget {
return part; // Keep full
}

// Whole graphemes, so an emoji or accented letter isn't cut in half
const [first = '', second = ''] = getGraphemes(part);

// Hidden directories keep the dot
if (part.startsWith('.') && part.length > 1) {
return '.' + (part[1] ?? '');
if (first === '.' && second !== '') {
return first + second;
}

return part[0]; // Only first letter for others
return first; // Only first letter for others
});

// Rebuild path
if (normalizedPath.startsWith('~')) {
return abbreviated.join(sep);
} else if (normalizedPath.startsWith('/')) {
return sep + abbreviated.join(sep);
} else {
return abbreviated.join(sep);
}
// Rebuild path on its root: '/', or the '\\' or '//' of a UNC path
const root = /^[\\/]+/.exec(normalizedPath)?.[0] ?? '';
return root + abbreviated.join(sep);
}
}

Expand All @@ -336,10 +336,12 @@ const CurrentWorkingDirEditor: React.FC<WidgetEditorProps> = ({ widget, onComple
if (key.return) {
const segments = Number.parseInt(segmentsInput, 10);
if (!Number.isNaN(segments) && segments > 0) {
// A segment count disables fishStyle (mutually exclusive)
const { fishStyle, ...restMetadata } = widget.metadata ?? {};
onComplete({
...widget,
metadata: {
...widget.metadata,
...restMetadata,
segments: segments.toString()
}
});
Expand Down
38 changes: 38 additions & 0 deletions src/widgets/__tests__/CurrentWorkingDir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,37 @@ describe('CurrentWorkingDirWidget', () => {

expect(result).toBe('/Users/a/project');
});

it.each([
{ name: 'an emoji', path: '/srv/πŸš€ rockets/sub/last', expected: '/srv/πŸš€/s/last' },
{ name: 'a joined emoji', path: '/srv/πŸ‘¨β€πŸ‘©β€πŸ‘§ family/sub/last', expected: '/srv/πŸ‘¨β€πŸ‘©β€πŸ‘§/s/last' },
{ name: 'an emoji after a dot', path: '/srv/.πŸ˜€x/sub/last', expected: '/srv/.πŸ˜€/s/last' },
{ name: 'a decomposed accent', path: '/srv/U\u0308ber/sub/last', expected: '/srv/U\u0308/s/last' }
])('should keep $name whole when abbreviating a middle segment', ({ path, expected }) => {
const item = createItem({ fishStyle: 'true' }, true);
const result = widget.render(
item,
createContext(path),
defaultSettings
);

expect(result).toBe(expected);
});

it.each([
{ path: '\\\\server\\share\\team\\repo', expected: '\\\\server\\s\\t\\repo' },
{ path: '//server/share/team/repo', expected: '//server/s/t/repo' },
{ path: 'C:\\Users\\me\\proj\\sub', expected: 'C:\\U\\m\\p\\sub' }
])('should keep the root of $path', ({ path, expected }) => {
const item = createItem({ fishStyle: 'true' }, true);
const result = widget.render(
item,
createContext(path),
defaultSettings
);

expect(result).toBe(expected);
});
});

describe('links', () => {
Expand Down Expand Up @@ -478,6 +509,13 @@ describe('CurrentWorkingDirWidget', () => {
expect(result?.metadata?.abbreviateHome).toBeUndefined();
});

it('should disable fishStyle and drop a segment count saved next to it', () => {
const item = createItem({ fishStyle: 'true', segments: '2' });
const result = widget.handleEditorAction('toggle-fish-style', item);
expect(result?.metadata?.fishStyle).toBeUndefined();
expect(result?.metadata?.segments).toBeUndefined();
});

it('should cycle file, VS Code, Cursor, Zed, then off without changing display metadata', () => {
const base = createItem({ abbreviateHome: 'true', segments: '2' });
const file = widget.handleEditorAction('cycle-link', base);
Expand Down
Loading