|
| 1 | +import { |
| 2 | + describe, |
| 3 | + expect, |
| 4 | + it |
| 5 | +} from 'vitest'; |
| 6 | + |
| 7 | +import type { RenderContext } from '../../types/RenderContext'; |
| 8 | +import { |
| 9 | + DEFAULT_SETTINGS, |
| 10 | + type Settings |
| 11 | +} from '../../types/Settings'; |
| 12 | +import type { WidgetItem } from '../../types/Widget'; |
| 13 | +import { getVisibleWidth } from '../ansi'; |
| 14 | +import { |
| 15 | + calculateMaxWidthsFromPreRendered, |
| 16 | + preRenderAllWidgets, |
| 17 | + renderStatusLine, |
| 18 | + type PreRenderedWidget |
| 19 | +} from '../renderer'; |
| 20 | + |
| 21 | +function createSettings(overrides: Partial<Settings> = {}): Settings { |
| 22 | + return { |
| 23 | + ...DEFAULT_SETTINGS, |
| 24 | + defaultPadding: '', |
| 25 | + flexMode: 'full', |
| 26 | + ...overrides, |
| 27 | + powerline: { |
| 28 | + ...DEFAULT_SETTINGS.powerline, |
| 29 | + ...(overrides.powerline ?? {}) |
| 30 | + } |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function pre(content: string, extra: Partial<WidgetItem> = {}): PreRenderedWidget { |
| 35 | + return { content, plainLength: content.length, widget: { id: content, type: 'custom-text', ...extra } }; |
| 36 | +} |
| 37 | + |
| 38 | +function text(content: string, extra: Partial<WidgetItem> = {}): WidgetItem { |
| 39 | + return { id: content, type: 'custom-text', customText: content, ...extra }; |
| 40 | +} |
| 41 | + |
| 42 | +describe('calculateMaxWidthsFromPreRendered with excludeFromAutoAlign', () => { |
| 43 | + it.each([ |
| 44 | + { name: 'lets a wide widget inflate the shared column by default', exclude: false, expected: [5, 14] }, |
| 45 | + { name: 'drops an excluded widget and the rest of its line', exclude: true, expected: [5, 1] } |
| 46 | + ])('$name', ({ exclude, expected }) => { |
| 47 | + const lines = [ |
| 48 | + [pre('short'), pre('VERYLONGWIDGET', exclude ? { excludeFromAutoAlign: true } : {})], |
| 49 | + [pre('x'), pre('y')] |
| 50 | + ]; |
| 51 | + |
| 52 | + expect(calculateMaxWidthsFromPreRendered(lines, createSettings())).toEqual(expected); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keeps columns before the excluded widget aligned', () => { |
| 56 | + const lines = [ |
| 57 | + [pre('a'), pre('wide', { excludeFromAutoAlign: true }), pre('tail')], |
| 58 | + [pre('AAAAA'), pre('BBBBB'), pre('CCCCC')] |
| 59 | + ]; |
| 60 | + |
| 61 | + expect(calculateMaxWidthsFromPreRendered(lines, createSettings())).toEqual([5, 5, 5]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('ignores exclusions on widgets merged into a previous widget', () => { |
| 65 | + const linesWithoutExclude = [ |
| 66 | + [pre('a', { merge: true }), pre('VERYLONGWIDGET')], |
| 67 | + [pre('x'), pre('y')] |
| 68 | + ]; |
| 69 | + const linesWithMergedExclude = [ |
| 70 | + [pre('a', { merge: true }), pre('VERYLONGWIDGET', { excludeFromAutoAlign: true })], |
| 71 | + [pre('x'), pre('y')] |
| 72 | + ]; |
| 73 | + |
| 74 | + expect(calculateMaxWidthsFromPreRendered(linesWithMergedExclude, createSettings())) |
| 75 | + .toEqual(calculateMaxWidthsFromPreRendered(linesWithoutExclude, createSettings())); |
| 76 | + }); |
| 77 | + |
| 78 | + it('honors exclusions on the first widget in a merged chain', () => { |
| 79 | + const lines = [ |
| 80 | + [pre('a', { merge: true, excludeFromAutoAlign: true }), pre('VERYLONGWIDGET')], |
| 81 | + [pre('x'), pre('y')] |
| 82 | + ]; |
| 83 | + |
| 84 | + expect(calculateMaxWidthsFromPreRendered(lines, createSettings())).toEqual([1, 1]); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('renderStatusLine auto-align exemption', () => { |
| 89 | + const settings = createSettings({ powerline: { ...DEFAULT_SETTINGS.powerline, enabled: true, autoAlign: true } }); |
| 90 | + |
| 91 | + function renderFirstLine(exclude: boolean): string { |
| 92 | + const lines = [ |
| 93 | + [text('a'), text('y', exclude ? { excludeFromAutoAlign: true } : {}), text('z')], |
| 94 | + [text('AAAAA'), text('BBBBB'), text('CCCCC')] |
| 95 | + ]; |
| 96 | + const context: RenderContext = { isPreview: false, terminalWidth: 200, lineIndex: 0 }; |
| 97 | + const preRendered = preRenderAllWidgets(lines, settings, context); |
| 98 | + const maxWidths = calculateMaxWidthsFromPreRendered(preRendered, settings); |
| 99 | + return renderStatusLine(lines[0] ?? [], settings, context, preRendered[0] ?? [], maxWidths); |
| 100 | + } |
| 101 | + |
| 102 | + it('exempts the excluded widget and the rest of its line from alignment padding', () => { |
| 103 | + expect(getVisibleWidth(renderFirstLine(false))).toBeGreaterThan(getVisibleWidth(renderFirstLine(true))); |
| 104 | + }); |
| 105 | +}); |
0 commit comments