Skip to content

Commit

Permalink
fix: handle negative margins (#31)
Browse files Browse the repository at this point in the history
* fix: handle negative margins

* fix: ensure margin is never NaN
  • Loading branch information
mdonnalley authored Oct 25, 2024
1 parent 9d3045a commit 5c6395f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
80 changes: 42 additions & 38 deletions examples/sf-specific/org-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import ansis from 'ansis'

import {printTable} from '../../src'

function addColor(row: (typeof data)[number], key: string) {
return row[key]
? row.isSandbox
? ansis.yellowBright(row[key])
: row.isDevHub
? ansis.cyanBright(row[key])
: row.isScratch
? row[key]
: ansis.magentaBright(row[key])
: null
}

const data = [
{
alias: 'devhub',
Expand Down Expand Up @@ -111,7 +126,7 @@ const data = [
isScratch: true,
lastUsed: '2024-10-09T18:44:31.494Z',
loginUrl: 'https://nosoftware-platform-8292-dev-ed.scratch.my.salesforce.com',
namespace: null,
namespace: 'myNamespace',
orgId: '00DO2000004SuOLMA0',
orgName: 'Company',
signupUsername: '[email protected]',
Expand All @@ -124,45 +139,34 @@ const data = [

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,
data: data
.map((row) => ({
...row,
alias: addColor(row, 'alias'),
connectedStatus: row.connectedStatus.startsWith('Unable')
? ansis.red(row.connectedStatus)
: ansis.green(row.connectedStatus),
instanceUrl: addColor(row, 'instanceUrl'),
namespace: addColor(row, 'namespace'),
orgId: addColor(row, 'orgId'),
type: addColor(row, 'type'),
username: addColor(row, 'username'),
}))
.map((row) => ({
' ': row.defaultMarker,
Alias: row.alias,
'Instance URL': row.instanceUrl,
Namespace: row.namespace,
'Org ID': row.orgId,
Status: row.connectedStatus,
Type: row.type,
Username: row.username,
...('devHubOrgId' in row ? {'Dev Hub ID': row.devHubOrgId} : {}),
...('createdDate' in row ? {Created: row.createdDate} : {}),
...('expirationDate' in row ? {Expires: row.expirationDate} : {}),
})),
headerOptions: {
formatter: 'capitalCase',
},
maxWidth: '100%',
overflow: 'wrap',
verticalAlignment: 'center',
})
16 changes: 14 additions & 2 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export function formatTextWithMargins({
function calculateMargins(spaces: number): {marginLeft: number; marginRight: number} {
let marginLeft: number
let marginRight: number
if (spaces <= 0 || Number.isNaN(spaces)) {
return {marginLeft: 0, marginRight: 0}
}

if (horizontalAlignment === 'left') {
marginLeft = padding
marginRight = spaces - marginLeft
Expand All @@ -123,7 +127,11 @@ export function formatTextWithMargins({
marginLeft = spaces - marginRight
}

return {marginLeft, marginRight}
return {
// Ensure that the margin is never negative
marginLeft: Math.max(0, marginLeft),
marginRight: Math.max(0, marginRight),
}
}

// Some terminals don't play nicely with zero-width characters, so we replace them with spaces.
Expand All @@ -141,7 +149,11 @@ export function formatTextWithMargins({
}

if (overflow === 'wrap') {
const wrappedText = wrapAnsi(valueWithNoZeroWidthChars, spaceForText, {hard: true, trim: true, wordWrap: true})
const wrappedText = wrapAnsi(valueWithNoZeroWidthChars, spaceForText, {
hard: true,
trim: true,
wordWrap: true,
}).replace(/^\n/, '') // remove leading newline (wrapAnsi adds it to emojis)
const {marginLeft, marginRight} = calculateMargins(width - determineWidthOfWrappedText(stripAnsi(wrappedText)))

const lines = wrappedText.split('\n').map((line, idx) => {
Expand Down

0 comments on commit 5c6395f

Please sign in to comment.