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
57 changes: 27 additions & 30 deletions src/widgets/CurrentWorkingDir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,11 @@ export class CurrentWorkingDirWidget implements Widget {

if (fishStyle) {
previewPath = '~/D/P/my-project';
} else if (abbreviateHome && segments && segments > 0) {
if (segments === 1) {
previewPath = '~/.../my-project';
} else {
previewPath = '~/.../Projects/my-project';
}
} else if (abbreviateHome) {
previewPath = '~/Documents/Projects/my-project';
} else if (segments && segments > 0) {
if (segments === 1) {
previewPath = '.../project';
} else {
previewPath = '.../example/project';
}
} else {
previewPath = '/Users/example/Documents/Projects/my-project';
previewPath = abbreviateHome ? '~/Documents/Projects/my-project' : '/Users/example/Documents/Projects/my-project';
if (segments && segments > 0) {
previewPath = this.keepLastSegments(previewPath, segments);
}
}

return `${symbolPrefix}${formatRawOrLabeledValue(item, this.getLabelPrefix(), previewPath)}`;
Expand All @@ -158,21 +147,7 @@ export class CurrentWorkingDirWidget implements Widget {

// Then apply segments truncation
if (segments && segments > 0) {
// Support both POSIX ('/') and Windows ('\\') separators; preserve original separator in output
const useBackslash = displayPath.includes('\\') && !displayPath.includes('/');
const outSep = useBackslash ? '\\' : '/';
const pathParts = displayPath.split(/[\\/]+/);

// Remove empty strings from splitting (e.g., leading slash or UNC leading separators)
const filteredParts = pathParts.filter(part => part !== '');

if (filteredParts.length > segments) {
// Take the last N segments and join with the detected separator
const selectedSegments = filteredParts.slice(-segments);
// Preserve ~ prefix when combined with segments
const prefix = displayPath.startsWith('~') ? `~${outSep}` : '';
displayPath = prefix + '...' + outSep + selectedSegments.join(outSep);
}
displayPath = this.keepLastSegments(displayPath, segments);
}
}

Expand All @@ -198,6 +173,28 @@ export class CurrentWorkingDirWidget implements Widget {
supportsRawValue(): boolean { return true; }
supportsColors(item: WidgetItem): boolean { return true; }

private keepLastSegments(path: string, segments: number): string {
// Support both POSIX ('/') and Windows ('\\') separators; preserve original separator in output
const useBackslash = path.includes('\\') && !path.includes('/');
const outSep = useBackslash ? '\\' : '/';
const pathParts = path.split(/[\\/]+/);

// Remove empty strings from splitting (e.g., leading slash or UNC leading separators)
const filteredParts = pathParts.filter(part => part !== '');

// Preserve ~ prefix when combined with segments; it stands for the home
// directory, so it isn't one of the segments
const prefix = filteredParts[0] === '~' ? `~${outSep}` : '';
const pathSegments = prefix ? filteredParts.slice(1) : filteredParts;

if (pathSegments.length <= segments) {
return path;
}

// Take the last N segments and join with the detected separator
return prefix + '...' + outSep + pathSegments.slice(-segments).join(outSep);
}

private abbreviateHomeDir(path: string): string {
const homeDir = os.homedir();
if (path === homeDir) {
Expand Down
34 changes: 34 additions & 0 deletions src/widgets/__tests__/CurrentWorkingDir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ describe('CurrentWorkingDirWidget', () => {
expect(result).toBe('~/.../Projects/my-project');
});

it.each([
{ path: 'Projects/my-project', segments: '2', expected: '~/Projects/my-project' },
{ path: 'Projects', segments: '1', expected: '~/Projects' }
])('should not count ~ as a segment ($segments of ~/$path)', ({ path, segments, expected }) => {
const item = createItem({ abbreviateHome: 'true', segments }, true);
const result = widget.render(
item,
createContext(`${defaultHomeDir}/${path}`),
defaultSettings
);
expect(result).toBe(expected);
});

it('should show correct preview when abbreviateHome is enabled', () => {
const item = createItem({ abbreviateHome: 'true' }, true);
const result = widget.render(
Expand Down Expand Up @@ -170,6 +183,27 @@ describe('CurrentWorkingDirWidget', () => {
});
});

describe('segments preview', () => {
it.each([
{ segments: '1', expected: '.../my-project' },
{ segments: '2', expected: '.../Projects/my-project' },
{ segments: '3', expected: '.../Documents/Projects/my-project' },
{ segments: '4', expected: '.../example/Documents/Projects/my-project' },
{ segments: '5', expected: '/Users/example/Documents/Projects/my-project' }
])('should preview segments=$segments on the sample path', ({ segments, expected }) => {
const item = createItem({ segments }, true);
expect(widget.render(item, createContext(undefined, true), defaultSettings)).toBe(expected);
});

it.each([
{ segments: '3', expected: '~/Documents/Projects/my-project' },
{ segments: '4', expected: '~/Documents/Projects/my-project' }
])('should preview segments=$segments on the sample path after ~', ({ segments, expected }) => {
const item = createItem({ abbreviateHome: 'true', segments }, true);
expect(widget.render(item, createContext(undefined, true), defaultSettings)).toBe(expected);
});
});

describe('fishStyle', () => {
it('should replace home directory with ~ and abbreviate middle segments', () => {
const item = createItem({ fishStyle: 'true' }, true);
Expand Down
Loading