Skip to content

Commit e1fa13e

Browse files
committed
fix: improve item layout on narrow terminals
1 parent 8d144bc commit e1fa13e

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/makeSelector.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,40 @@ function makeCreateTitle<Item>(columns: Columns<Item>): (item: Item) => string {
310310
})
311311
}
312312
}
313-
return (item: Item): string => {
314-
const separator = ' '
315-
const fixedWidth = normalizedColumns.reduce(
316-
(total, c) => total + (c.width ?? 0),
317-
separator.length * (normalizedColumns.length - 1)
318-
)
319-
const totalGrow = Math.max(
320-
1,
321-
normalizedColumns.reduce(
322-
(total, c) => total + (c.grow ?? (c.width != null ? 0 : 1)),
323-
0
324-
)
325-
)
326-
const remainingWidth = Math.max(0, process.stdout.columns - fixedWidth - 4)
327-
const columnWidths = normalizedColumns.map(
328-
({ width, minWidth, grow = 1 }) =>
329-
width ??
330-
Math.max(minWidth ?? 0, Math.floor((remainingWidth * grow) / totalGrow))
313+
const separator = ' '
314+
const availableWidth = process.stdout.columns - 4
315+
let fixedWidth = normalizedColumns.reduce(
316+
(total, c) =>
317+
total + Math.max(c.minWidth ?? 0, c.width ?? 0) + separator.length,
318+
0
319+
)
320+
let excess: number
321+
while ((excess = fixedWidth - availableWidth) > 0) {
322+
const c = normalizedColumns[normalizedColumns.length - 1]
323+
const minWidth = Math.max(c.minWidth ?? 0, c.width ?? 0)
324+
if (minWidth > excess) {
325+
if (c.minWidth != null) c.minWidth = Math.max(0, c.minWidth - excess)
326+
if (c.width != null) c.width = Math.max(0, c.width - excess)
327+
break
328+
}
329+
fixedWidth -= minWidth + separator.length
330+
normalizedColumns.pop()
331+
}
332+
333+
const totalGrow = Math.max(
334+
1,
335+
normalizedColumns.reduce(
336+
(total, c) => total + (c.grow ?? (c.width != null ? 0 : 1)),
337+
0
331338
)
339+
)
340+
const remainingWidth = Math.max(0, availableWidth - fixedWidth)
341+
const columnWidths = normalizedColumns.map(
342+
({ width, minWidth, grow = 1 }) =>
343+
width ?? (minWidth ?? 0) + Math.floor((remainingWidth * grow) / totalGrow)
344+
)
345+
346+
return (item: Item): string => {
332347
return normalizedColumns
333348
.map((c, i) => {
334349
const width = columnWidths[i]

src/selectCloudFormationStack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const selectCloudFormationStack = makeSelector({
2020
.send(new DescribeStacksCommand({ StackName }), { abortSignal })
2121
.then((r) => r.Stacks?.[0]),
2222
columns: {
23-
StackName: { grow: 2 },
23+
StackName: { minWidth: 30, grow: 2 },
2424
StackStatus: {
2525
minWidth: 'UPDATE_ROLLBACK_FAILED'.length,
2626
grow: 1,

0 commit comments

Comments
 (0)