Skip to content

Commit

Permalink
fix(printStackEvents): improve layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Sep 26, 2022
1 parent 63d4090 commit d54d9c4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 39 deletions.
36 changes: 18 additions & 18 deletions src/printColumns.js → src/layoutColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
* @flow
*/

export function wrapIndex(s: string, maxWidth: number): number {
if (s.length <= maxWidth) return s.length
for (let i = maxWidth; i >= Math.min(3, maxWidth); i--) {
if (/\s/.test(s[i - 1])) return i
}
for (let i = maxWidth; i >= Math.min(3, maxWidth); i--) {
if (
/[a-z0-9]/i.test(s[i]) &&
(/[^a-z0-9]/i.test(s[i - 1]) ||
/[a-z0-9][A-Z]|[0-9][a-zA-Z]/.test(s.substring(i - 1, i + 1)))
) {
return i
}
}
return maxWidth
}

export default function layoutColumns({
columns,
widths,
Expand All @@ -17,24 +34,7 @@ export default function layoutColumns({
do {
const line = strColumns.map((text: string, index: number): string => {
if (text.length > widths[index]) {
let splitIndex = Math.min(
text.match(/\s/)?.index ??
text.match(/[^a-z0-9]/i)?.index ??
text.length,
widths[index]
)
if (splitIndex === 0) {
const lastIndex =
text.match(/[a-z0-9_]/i)?.index ?? text.match(/\S/)?.index ?? 1
const sub = text.substring(lastIndex)
splitIndex = Math.min(
(sub.match(/\s/)?.index ??
sub.match(/[^a-z0-9]/i)?.index ??
widths[index] ??
sub.length) + lastIndex,
widths[index]
)
}
const splitIndex = wrapIndex(text, widths[index])
const result = text.substring(0, splitIndex).trim()
strColumns[index] = text.substring(splitIndex).trim()
return result.padEnd(widths[index], ' ')
Expand Down
55 changes: 34 additions & 21 deletions src/printStackEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { type StackEvent } from './getCurrentStackEvents'
import { type Writable } from 'stream'
import printColumns from './printColumns'
import layoutColumns from './layoutColumns'
import chalk from 'chalk'

function statusColor(status: string): (text: string) => string {
Expand All @@ -20,29 +20,42 @@ export default async function printStackEvents({
events,
out = process.stderr,
printHeader,
width = Math.max(80, (out: any).columns || 200),
}: {|
events: AsyncIterable<StackEvent>,
out?: Writable,
printHeader?: boolean,
width?: number,
|}) {
const numColumns = 5
const trueWidth = (out: any).columns ?? 80
let width = trueWidth - numColumns + 1
let remWidth = width - (numColumns - 1) * 2

const minColWidth = width / numColumns
const statusWidth =
[
'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS',
'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS',
'UPDATE_ROLLBACK_IN_PROGRESS',
].find((s) => s.length < remWidth / 6)?.length ??
'UPDATE_IN_PROGRESS'.length
remWidth -= statusWidth

const timestampWidth = Math.min(24, minColWidth)
width -= timestampWidth
const stackNameWidth = Math.min(32, minColWidth)
width -= stackNameWidth
const resourceIdWidth = Math.min(32, minColWidth)
width -= resourceIdWidth
const statusWidth = Math.min(
'UPDATE_FAILED_ROLLBACK_IN_PROGRESS'.length,
minColWidth
)
width -= statusWidth
const reasonWidth = width
const timestampWidth =
'MM/dd/yyyy HH:mm:ss AM'.length < width / 5
? 'MM/dd/yyyy HH:mm:ss AM'.length
: 'HH:mm:ss AM'.length
remWidth -= timestampWidth
let stackNameWidth, resourceIdWidth, reasonWidth
if (remWidth / 3 < 32) {
stackNameWidth = resourceIdWidth = Math.floor(remWidth / 3)
remWidth -= stackNameWidth + resourceIdWidth
reasonWidth = remWidth
} else {
reasonWidth = Math.floor(remWidth / 2)
remWidth -= reasonWidth
resourceIdWidth = Math.floor(remWidth / 2)
remWidth -= resourceIdWidth
stackNameWidth = remWidth
}

const widths = [
timestampWidth,
Expand All @@ -53,7 +66,7 @@ export default async function printStackEvents({
]
if (printHeader) {
out.write(
printColumns({
layoutColumns({
columns: [
'Timestamp',
'Stack Name',
Expand All @@ -62,15 +75,15 @@ export default async function printStackEvents({
'Resource Status Reason',
],
widths,
delimiter: ' ',
delimiter: ' ',
})
)
out.write('='.repeat(trueWidth) + '\n')
out.write('='.repeat(width) + '\n')
}
for await (const event of events) {
out.write(
statusColor(event.ResourceStatus)(
printColumns({
layoutColumns({
columns: [
event.Timestamp.toLocaleString(),
event.StackName,
Expand All @@ -79,7 +92,7 @@ export default async function printStackEvents({
event.ResourceStatusReason,
],
widths,
delimiter: ' ',
delimiter: ' ',
})
)
)
Expand Down
31 changes: 31 additions & 0 deletions test/wrapIndex.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @flow
* @prettier
*/

import { describe, it } from 'mocha'
import { expect } from 'chai'
import { wrapIndex } from '../src/layoutColumns'

describe(`wrapIndex`, function () {
for (const [s, maxWidth, expected] of [
['abc-def', 5, 4],
['abcdefghijk', 5, 5],
['abcdef', 5, 5],
['abcd', 5, 4],
['', 5, 0],
[' ', 5, 5],
['-=-=2=3', 5, 4],
['abc def', 5, 4],
['abc def', 5, 5],
['ab def', 5, 3],
['AbcDef', 5, 3],
['Abc0ef', 5, 4],
['Abc05f', 5, 5],
['Abc 3-fjklskjdf', 10, 4],
]) {
it(`${s} ${maxWidth} ${expected}`, function () {
expect(wrapIndex(s, maxWidth)).to.equal(expected)
})
}
})

0 comments on commit d54d9c4

Please sign in to comment.