Skip to content

Commit

Permalink
feat(string): add utility fn to trim string
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Jan 13, 2022
1 parent 848a30a commit c193cd4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
17 changes: 17 additions & 0 deletions static/app/utils/profiling/gl/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,20 @@ export function findRangeBinarySearch(
}
}
}

export const ELLIPSIS = '\u2026';
export function trimTextCenter(text: string, low: number) {
if (low > text.length) {
return text;
}

const prefixLength = Math.floor(low / 2);
// Use 1 character less than the low value to account for ellipsis
// and favor displaying the prefix
const postfixLength = low - prefixLength - 1;

return `${text.substring(0, prefixLength)}${ELLIPSIS}${text.substring(
text.length - postfixLength,
text.length
)}`;
}
38 changes: 19 additions & 19 deletions static/app/utils/profiling/renderers/textRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import {mat3} from 'gl-matrix';

import {Flamegraph} from '../Flamegraph';
import {FlamegraphTheme} from '../flamegraph/FlamegraphTheme';
import {getContext, Rect, resizeCanvasToDisplaySize} from '../gl/utils';

const ELLIPSIS = '\u2026';
import {
findRangeBinarySearch,
getContext,
Rect,
resizeCanvasToDisplaySize,
trimTextCenter,
} from '../gl/utils';

class TextRenderer {
textCache: Record<string, number> = {};
Expand Down Expand Up @@ -97,23 +101,19 @@ class TextRenderer {

if (paddedWidth >= minWidth) {
if (this.measureText(this.context, text) > paddedWidth) {
const [low] = findValueBisect(
0,
text.length,
n => this.measureText(this.context, text.substring(0, n)),
paddedWidth
);

const prefixLength = Math.floor(low / 2);
const postfixLength = low - prefixLength - 1;

const string = `${text.substr(0, prefixLength)}${ELLIPSIS}${text.substr(
text.length - postfixLength,
postfixLength
)}`;

this.context.fillStyle = this.theme.COLORS.LABEL_FONT_COLOR;
this.context.fillText(string, x, y);
this.context.fillText(
trimTextCenter(
text,
findRangeBinarySearch(
{low: 0, high: text.length},
n => this.measureText(this.context, text.substring(0, n)),
paddedWidth
)[0]
),
x,
y
);
} else {
this.context.fillStyle = this.theme.COLORS.LABEL_FONT_COLOR;
this.context.fillText(text, x, y);
Expand Down
14 changes: 14 additions & 0 deletions tests/js/spec/utils/profiling/gl/utils.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {mat3, vec2} from 'gl-matrix';
import {
createProgram,
createShader,
ELLIPSIS,
findRangeBinarySearch,
getContext,
makeProjectionMatrix,
Rect,
Transform,
trimTextCenter,
} from 'sentry/utils/profiling/gl/utils';

describe('makeProjectionMatrix', () => {
Expand Down Expand Up @@ -354,3 +356,15 @@ describe('findRangeBinarySearch', () => {
expect(text.substring(0, low)).toBe('abc');
});
});

describe('trimTextCenter', () => {
it('trims nothing if low > length', () => {
expect(trimTextCenter('abc', 4)).toBe('abc');
});
it('trims center perfectly', () => {
expect(trimTextCenter('abcdef', 5)).toBe(`ab${ELLIPSIS}ef`);
});
it('favors prefix length', () => {
expect(trimTextCenter('abcdef', 4)).toBe(`ab${ELLIPSIS}f`);
});
});

0 comments on commit c193cd4

Please sign in to comment.