Skip to content

Commit

Permalink
fix: better fix for center aligned wrapped text
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 4, 2024
1 parent 53d51bc commit ae049a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
48 changes: 24 additions & 24 deletions examples/overflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ printTable({
titleOptions: {bold: true},
})

printTable({
columns: ['id', 'name', 'age', 'description'],
data,
headerOptions: {
formatter: 'capitalCase',
},
horizontalAlignment: 'left',
overflow: 'wrap',
title: 'Wrap (aligned left)',
titleOptions: {bold: true},
verticalAlignment: 'center',
})
// printTable({
// columns: ['id', 'name', 'age', 'description'],
// data,
// headerOptions: {
// formatter: 'capitalCase',
// },
// horizontalAlignment: 'left',
// overflow: 'wrap',
// title: 'Wrap (aligned left)',
// titleOptions: {bold: true},
// verticalAlignment: 'center',
// })

printTable({
columns: ['id', 'name', 'age', 'description'],
data,
headerOptions: {
formatter: 'capitalCase',
},
horizontalAlignment: 'right',
overflow: 'wrap',
title: 'Wrap (aligned right)',
titleOptions: {bold: true},
verticalAlignment: 'bottom',
})
// printTable({
// columns: ['id', 'name', 'age', 'description'],
// data,
// headerOptions: {
// formatter: 'capitalCase',
// },
// horizontalAlignment: 'right',
// overflow: 'wrap',
// title: 'Wrap (aligned right)',
// titleOptions: {bold: true},
// verticalAlignment: 'bottom',
// })
4 changes: 3 additions & 1 deletion src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ export function formatTextWithMargins({
}

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

// right alignment
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 ae049a8

Please sign in to comment.