From ab3f94b12f6385a4e31b00c43d5303f753da3413 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 3 Oct 2024 15:06:06 -0600 Subject: [PATCH] fix: right alignment --- src/table.tsx | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/table.tsx b/src/table.tsx index c699bcb..642aa1a 100644 --- a/src/table.tsx +++ b/src/table.tsx @@ -138,27 +138,42 @@ function formatTextWithMargins({ if (overflow === 'wrap') { const wrappedText = wrapAnsi(valueWithNoZeroWidthChars, spaceForText, {hard: true, trim: true, wordWrap: true}) - const {marginLeft, marginRight} = calculateMargins(width - determineWidthOfWrappedText(stripAnsi(wrappedText))) + const spaces = width - determineWidthOfWrappedText(stripAnsi(wrappedText)) + const {marginLeft, marginRight} = calculateMargins(spaces) const lines = wrappedText.split('\n').map((line, idx) => { const {marginLeft: lineSpecificLeftMargin} = calculateMargins(width - stripAnsi(line).length) - if (idx === 0) { - // if it's the first line, only add margin to the right side (The left margin will be applied later) - return `${line}${' '.repeat(marginRight)}` - } + // if (idx === 0) { + // // if it's the first line, only add margin to the right side (The left margin will be applied later) + // return `${line}${' '.repeat(marginRight)}` + // } if (horizontalAlignment === 'left') { - // if left alignment, add the overall margin to the left side only - return `${' '.repeat(marginLeft)}${line}` + if (idx === 0) { + // if it's the first line, only add margin to the right side (The left margin will be applied later) + return `${line}${' '.repeat(marginRight)}` + } + + // if left alignment, add the overall margin to the left side and right sides + return `${' '.repeat(marginLeft)}${line}${' '.repeat(marginRight)}` } - if (horizontalAlignment === 'right') { - // if right alignment, add line specific margin to the left side only - return `${' '.repeat(lineSpecificLeftMargin)}${line}` + if (horizontalAlignment === 'center') { + if (idx === 0) { + // if it's the first line, only add margin to the right side (The left margin will be applied later) + return `${line}${' '.repeat(marginRight)}` + } + + // if center alignment, add line specific margin to the left side and the overall margin to the right side + return `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(marginRight)}` + } + + // right alignment + if (idx === 0) { + return `${' '.repeat(Math.max(0, lineSpecificLeftMargin - marginLeft))}${line}${' '.repeat(marginRight)}` } - // if center alignment, add line specific margin to the left side - return `${' '.repeat(lineSpecificLeftMargin)}${line}` + return `${' '.repeat(lineSpecificLeftMargin)}${line}${' '.repeat(marginRight)}` }) return { @@ -310,6 +325,10 @@ function row>(config: RowConfig): (props: RowP width, }) + // console.log( + // {marginLeft, marginRight, text}, + // JSON.stringify(`${skeleton.line.repeat(marginLeft)}${text}${skeleton.line.repeat(marginRight)}`), + // ) const alignItems = verticalAlignment === 'top' ? 'flex-start' : verticalAlignment === 'center' ? 'center' : 'flex-end' return (