Skip to content

Commit

Permalink
fix: Resize issues. Closes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
tecfu committed Feb 12, 2020
1 parent dc928c6 commit 8c4d5f2
Show file tree
Hide file tree
Showing 4 changed files with 964 additions and 6 deletions.
47 changes: 47 additions & 0 deletions examples/resize-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// table resize test for tty-table
const Table = require("../")
const Chalk = require("chalk")

let failures = []

function test (width, truncate, marginLeft) {
let output = new Table([
{minWidth: 6, shrink: 0},
{minWidth: 12, shrink: 1},
{minWidth: 24, shrink: 1000}
], [[
"aaa bbb ccc",
"aaa bbb ccc ddd eee fff ggg hhh",
"aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll mmm nnn ooo ppp qqq rrr sss ttt"
]], {
truncate: false,
paddingLeft: 0,
paddingRight: 0,
marginTop: 0,
marginLeft: typeof marginLeft === "number" ? marginLeft : 2
}).render()

let widest = Math.max(...output.split("\n").map(s => s.length))

if (widest > width) {
failures.unshift(`Table Too Wide: target=${width} widest-line=${widest}`)
console.log(Chalk.red(failures[0]))
}
console.log(output)
}

for (let w = 25; w < 120; w++) {
console.log(`\n${`=== width=${w} ${new Array(w).fill("=").join("")}`.substr(0, w - 1) }|`)
process.stdout.columns = w // fake width
let leftMargin = /^\d+$/.test(process.argv[3]) ? parseInt(process.argv[3]) : 2
test(w, process.argv[2], leftMargin)
}

//let w = 64
//console.log(`\n${`=== width=${w} ${new Array(w).fill("=").join("")}`.substr(0, w - 1) }|`)
//process.stdout.columns = w // fake width
//test(w, false, 3)

if (failures.length) {
process.stderr.write(Chalk.redBright(failures.join("\n")))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tty-table",
"version": "2.8.7",
"version": "2.8.8",
"description": "Node cli table",
"main": "src/main.js",
"engines": {
Expand Down
8 changes: 3 additions & 5 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Format.wrapCellContent = (
const innerWidth = columnWidth -cellOptions.paddingLeft -cellOptions.paddingRight -config.GUTTER

switch(true) {
//no wrap, truncate
//no wrap, truncate
case(typeof config.truncate === "string"):
string = Format.handleTruncatedValue(string, cellOptions, innerWidth)
break
Expand Down Expand Up @@ -152,6 +152,7 @@ Format.handleWideChars = (string, cellOptions, innerWidth) => {
Format.handleNonWideChars = (string, cellOptions, innerWidth) => {
let outstring = Smartwrap(string, {
width: innerWidth,
minWidth: 1,
trim: true//,
//indent : '',
//cut : true
Expand Down Expand Up @@ -231,13 +232,10 @@ Format.getColumnWidths = (config, rows) => {
return prev + curr
})

//add marginLeft to totalWidth
totalWidth += config.marginLeft

//if sum of all widths exceeds viewport, resize proportionately to fit
if(process && process.stdout && totalWidth > process.stdout.columns) {
//recalculate proportionately to fit size
let prop = process.stdout.columns / totalWidth
let prop = (process.stdout.columns - config.marginLeft) / totalWidth

prop = prop.toFixed(2)-0.01

Expand Down

0 comments on commit 8c4d5f2

Please sign in to comment.