diff --git a/shared/packages/api/src/__tests__/lib.spec.ts b/shared/packages/api/src/__tests__/lib.spec.ts index 00c944f8..edcad342 100644 --- a/shared/packages/api/src/__tests__/lib.spec.ts +++ b/shared/packages/api/src/__tests__/lib.spec.ts @@ -1,4 +1,4 @@ -import { deferGets, diff, promiseTimeout, stringifyError, waitTime } from '../lib' +import { deferGets, diff, promiseTimeout, stringMaxLength, stringifyError, waitTime } from '../lib' describe('lib', () => { test('diff', () => { @@ -218,5 +218,13 @@ describe('lib', () => { ]) expect(i).toBe(5) }) + test('stringMaxLength', () => { + expect(stringMaxLength('abc', 10)).toBe('abc') + expect(stringMaxLength('0123456789abcdefg', 17)).toBe('0123456789abcdefg') + expect(stringMaxLength('0123456789abcdefg', 15)).toBe('012345...bcdefg') + expect(stringMaxLength('0123456789abcdefg', 10)).toBe('012...defg') + expect(stringMaxLength('0123456789abcdefg', 9)).toBe('012...efg') + expect(stringMaxLength('0123456789abcdefg', 8)).toBe('01...efg') + }) }) export {} diff --git a/shared/packages/api/src/lib.ts b/shared/packages/api/src/lib.ts index 47bccfa7..f2cfa83d 100644 --- a/shared/packages/api/src/lib.ts +++ b/shared/packages/api/src/lib.ts @@ -361,3 +361,17 @@ export function startTimer(): { }, } } +/** + * If the string is longer than maxLength, it will be shortened to maxLength, with '...' in the middle + */ +export function stringMaxLength(str: string, maxLength: number): string { + str = `${str}` + if (str.length > maxLength) { + maxLength = maxLength - 3 // make space for '...' + const length0 = Math.floor(maxLength / 2) + const length1 = maxLength - length0 + + return str.slice(0, length0) + '...' + str.slice(-length1) + } + return str +} diff --git a/shared/packages/expectationManager/src/lib/trackedExpectation.ts b/shared/packages/expectationManager/src/lib/trackedExpectation.ts index 91b614c5..8ee2ce44 100644 --- a/shared/packages/expectationManager/src/lib/trackedExpectation.ts +++ b/shared/packages/expectationManager/src/lib/trackedExpectation.ts @@ -1,6 +1,6 @@ // eslint-disable-next-line node/no-extraneous-import import { ExpectedPackageStatusAPI } from '@sofie-automation/shared-lib/dist/package-manager/package' -import { Expectation, ExpectationId, Reason, WorkerAgentId } from '@sofie-package-manager/api' +import { Expectation, ExpectationId, Reason, WorkerAgentId, stringMaxLength } from '@sofie-package-manager/api' import { ExpectationStateHandlerSession } from '../lib/types' import { ExpectationTrackerConstants } from './constants' @@ -59,12 +59,7 @@ export interface TrackedExpectation { } export function expLabel(exp: TrackedExpectation): string { - let id = `${exp.id}` - if (id.length > 16) { - id = id.slice(0, 8) + '...' + id.slice(-8) - } - - return `${id} ${exp.exp.statusReport.label.slice(0, 50)}` + return stringMaxLength(exp.id, 16) + ' ' + stringMaxLength(exp.exp.statusReport.label, 80) } export function sortTrackedExpectations(