Skip to content

Commit

Permalink
fix: handle very wide tables
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 9, 2024
1 parent 9fde53e commit 0627af9
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 3 deletions.
168 changes: 168 additions & 0 deletions examples/sf-specific/org-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import {printTable} from '../../src'
const data = [
{
alias: 'devhub',
connectedStatus: 'Connected',
defaultMarker: undefined,
instanceApiVersion: '62.0',
instanceApiVersionLastRetrieved: '10/9/2024, 12:44:28 PM',
instanceUrl: 'https://su0503.my.salesforce.com',
isDefaultDevHubUsername: false,
isDefaultUsername: false,
isDevHub: true,
lastUsed: '2024-10-09T18:44:31.494Z',
loginUrl: 'https://login.salesforce.com',
orgId: '00DB0000000Ih65MAC',
timestamp: '2022-05-10T19:26:45.436Z',
type: 'DevHub',
username: 'md@su-blitz.org',
},
{
alias: 'na40devhub',
connectedStatus: 'Connected',
defaultMarker: '🌳',
instanceApiVersion: '61.0',
instanceApiVersionLastRetrieved: '10/9/2024, 12:44:29 PM',
instanceUrl: 'https://na40-dev-hub.my.salesforce.com',
isDefaultDevHubUsername: true,
isDefaultUsername: false,
isDevHub: true,
lastUsed: '2024-10-09T18:44:31.495Z',
loginUrl: 'https://login.salesforce.com',
orgId: '00D460000019MkyEAE',
privateKey: '/Users/mdonnalley/secrets/jwt/na40.key',
type: 'DevHub',
username: 'admin@integrationtesthubna40.org',
},
{
alias: undefined,
connectedStatus:
'Unable to refresh session due to: Error authenticating with the refresh token due to: expired access/refresh token',
defaultMarker: undefined,
instanceApiVersion: '62.0',
instanceApiVersionLastRetrieved: '10/9/2024, 12:44:28 PM',
instanceUrl: 'https://cristianalexisdominguez-devhub.my.salesforce.com',
isDefaultDevHubUsername: false,
isDefaultUsername: false,
isDevHub: true,
lastUsed: '2024-10-09T18:44:29.234Z',
loginUrl: 'https://cristianalexisdominguez-devhub.my.salesforce.com/',
orgId: '00DB00000006Mq3MAE',
type: 'DevHub',
username: 'cdominguez@gs0-dev-hub-salesforce.com',
},
{
alias: undefined,
connectedStatus:
'Unable to refresh session due to: Error authenticating with the refresh token due to: expired access/refresh token',
defaultMarker: undefined,
instanceApiVersion: '62.0',
instanceApiVersionLastRetrieved: '10/9/2024, 12:44:28 PM',
instanceUrl: 'https://su0503.my.salesforce.com',
isDefaultDevHubUsername: false,
isDefaultUsername: false,
isDevHub: true,
lastUsed: '2024-10-09T18:44:29.986Z',
loginUrl: 'https://login.salesforce.com',
orgId: '00DB0000000Ih65MAC',
type: 'DevHub',
username: 'shetzel@gs0.org',
},
{
alias: undefined,
connectedStatus: 'Connected',
defaultMarker: undefined,
instanceApiVersion: '62.0',
instanceApiVersionLastRetrieved: '10/9/2024, 12:44:28 PM',
instanceUrl: 'https://su0503--sbxgs01.sandbox.my.salesforce.com',
isDefaultDevHubUsername: false,
isDefaultUsername: false,
isDevHub: false,
isSandbox: true,
isScratch: false,
lastUsed: '2024-10-09T18:47:21.186Z',
loginUrl: 'https://test.salesforce.com',
orgId: '00D3I0000008poXUAQ',
tracksSource: false,
type: 'Sandbox',
username: 'shetzel@gs0.org.sbxgs01',
},
{
alias: 'ink',
connectedStatus: 'Active',
created: '1728401061000',
createdBy: 'md@su-blitz.org',
createdDate: '2024-10-08T15:24:21.000+0000',
createdOrgInstance: 'USA256S',
defaultMarker: '🍁',
devHubId: '00DB0000000Ih65MAC',
devHubOrgId: '00DB0000000Ih65MAC',
devHubUsername: 'md@su-blitz.org',
edition: 'Developer',
expirationDate: '2024-10-09',
instanceApiVersion: '62.0',
instanceApiVersionLastRetrieved: '10/9/2024, 12:23:19 PM',
instanceUrl: 'https://nosoftware-platform-8292-dev-ed.scratch.my.salesforce.com',
isDefaultDevHubUsername: false,
isDefaultUsername: true,
isDevHub: false,
isExpired: false,
isSandbox: false,
isScratch: true,
lastUsed: '2024-10-09T18:44:31.494Z',
loginUrl: 'https://nosoftware-platform-8292-dev-ed.scratch.my.salesforce.com',
namespace: null,
orgId: '00DO2000004SuOLMA0',
orgName: 'Company',
signupUsername: 'test-1yomelh1c0ha@example.com',
status: 'Active',
tracksSource: true,
type: 'Scratch',
username: 'test-1yomelh1c0ha@example.com',
},
]

printTable({
borderStyle: 'vertical-with-outline',
columns: [
{
key: 'defaultMarker',
name: ' ',
},
'type',
'alias',
'username',
{
key: 'instanceUrl',
name: 'Instance URL',
},
{
key: 'orgId',
name: 'Org ID',
},
{
key: 'connectedStatus',
name: 'Status',
},
'namespace',
{
key: 'devHubId',
name: 'Devhub ID',
},
{
key: 'createdDate',
name: 'Created',
},
{
key: 'expirationDate',
name: 'Expires',
},
],
data,
headerOptions: {
formatter: 'capitalCase',
},
maxWidth: '100%',
overflow: 'wrap',
verticalAlignment: 'center',
})
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,24 @@ export function getColumns<T extends Record<string, unknown>>(config: Config<T>,
const numberOfBorders = widths.length + 1

const calculateTableWidth = (widths: Column<T>[]) =>
widths.map((w) => w.width).reduce((a, b) => a + b) + numberOfBorders
widths.map((w) => w.width + w.padding * 2).reduce((a, b) => a + b) + numberOfBorders

// If the table is too wide, reduce the width of the largest column as little as possible to fit the table.
// At most, it will reduce the width to the length of the column's header plus padding.
// If the table is still too wide, it will reduce the width of the next largest column and so on
let tableWidth = calculateTableWidth(widths)
const seen = new Set<string>()
while (tableWidth > maxWidth) {
const largestColumn = widths.reduce((a, b) => (a.width > b.width ? a : b))
const largestColumn = widths.filter((w) => !seen.has(w.key)).sort((a, b) => b.width - a.width)[0]
if (!largestColumn) break
if (seen.has(largestColumn.key)) break
const header = String(headings[largestColumn.key]).length
// The minimum width of a column is the width of the header plus padding on both sides
const minWidth = header + largestColumn.padding * 2
const difference = tableWidth - maxWidth
const newWidth = largestColumn.width - difference < minWidth ? minWidth : largestColumn.width - difference
largestColumn.width = newWidth
tableWidth = calculateTableWidth(widths)
if (seen.has(largestColumn.key)) break
seen.add(largestColumn.key)
}

Expand Down

0 comments on commit 0627af9

Please sign in to comment.