Skip to content

Commit

Permalink
fix: center wrapped text
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 4, 2024
1 parent 51ef995 commit 53d51bc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cliTruncate from 'cli-truncate'
import {Box, Text, render} from 'ink'
import {EOL} from 'node:os'
import {sha1} from 'object-hash'
import React from 'react'
import stripAnsi from 'strip-ansi'
Expand Down Expand Up @@ -156,9 +155,7 @@ export function formatTextWithMargins({
}

if (horizontalAlignment === 'center') {
return idx === 0
? `${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`
: `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`
return `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(lineSpecificRightMargin - marginRight)}`
}

// right alignment
Expand All @@ -174,7 +171,7 @@ export function formatTextWithMargins({
}
}

const text = cliTruncate(valueWithNoZeroWidthChars.replaceAll(EOL, ' '), spaceForText, {
const text = cliTruncate(valueWithNoZeroWidthChars.replaceAll('\n', ' '), spaceForText, {
position: determineTruncatePosition(overflow),
})
const spaces = width - stripAnsi(text).length
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {camelCase, capitalCase, constantCase, kebabCase, pascalCase, sentenceCase, snakeCase} from 'change-case'
import {orderBy} from 'natural-orderby'
import {EOL} from 'node:os'
import stripAnsi from 'strip-ansi'

import {Column, ColumnProps, Config, Sort} from './types.js'
Expand Down Expand Up @@ -47,7 +46,7 @@ export function allKeysInCollection<T extends Record<string, unknown>>(data: T[]
}

export function determineWidthOfWrappedText(text: string): number {
const lines = text.split(EOL)
const lines = text.split('\n')
return lines.reduce((max, line) => Math.max(max, line.length), 0)
}

Expand Down
4 changes: 2 additions & 2 deletions test/table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ scing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`,
).to.deep.equal({
marginLeft: 1,
marginRight: 1,
text: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\n iiiiiiiiiiiiiiiiiiiiii ',
text: ' iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\n iiiiiiiiiiiiiiiiiiiiii ',
})
})

Expand All @@ -632,7 +632,7 @@ scing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`,
).to.deep.equal({
marginLeft: 1,
marginRight: 2,
text: 'Lorem ipsum dolor sit amet, consectetur \n adipi \n scing elit. Sed do eiusmod tempor incididunt ut\n labore et dolore magna aliqua. ',
text: ' Lorem ipsum dolor sit amet, consectetur \n adipi \n scing elit. Sed do eiusmod tempor incididunt ut\n labore et dolore magna aliqua. ',
})
})
})
Expand Down

0 comments on commit 53d51bc

Please sign in to comment.