-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
276 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,79 @@ | ||
import { | ||
calculateCellWidths, | ||
} from './calculateCellWidths'; | ||
import type { | ||
Row, | ||
} from './types/internal'; | ||
ColumnUserConfig, Indexable, | ||
} from './types/api'; | ||
import { | ||
distributeUnevenly, | ||
} from './utils'; | ||
|
||
const DEFAULT_PADDING = 1; | ||
type ColumnConfigs = Indexable<ColumnUserConfig>; | ||
|
||
const calculateRemainWidth = ( | ||
{widths, totalWidth, columnConfigs, columnDefault}: { | ||
widths: Array<number | 'auto'>, | ||
totalWidth: number, | ||
columnConfigs?: ColumnConfigs, | ||
columnDefault?: ColumnUserConfig, | ||
}, | ||
): number => { | ||
const totalBorderWidth = widths.length + 1; | ||
const totalPadding = widths.reduce<number>((total, _, columnIndex) => { | ||
const paddingLeft = columnConfigs?.[columnIndex]?.paddingLeft ?? columnDefault?.paddingLeft ?? DEFAULT_PADDING; | ||
const paddingRight = columnConfigs?.[columnIndex]?.paddingRight ?? columnDefault?.paddingRight ?? DEFAULT_PADDING; | ||
|
||
return total + paddingLeft + paddingRight; | ||
}, 0); | ||
const totalFixedWidth = widths.reduce<number>((total, width) => { | ||
return total + (width === 'auto' ? 0 : width); | ||
}, 0); | ||
|
||
return totalWidth - totalBorderWidth - totalPadding - totalFixedWidth; | ||
}; | ||
|
||
/** | ||
* Produces an array of values that describe the largest value length (width) in every column. | ||
* Creates a configuration for every column using default | ||
* values for the missing configuration properties. | ||
*/ | ||
export default (rows: Row[]): number[] => { | ||
const columnWidths = new Array(rows[0].length).fill(0); | ||
export const calculateColumnWidths = (maxColumnWidths: ReadonlyArray<number | 'auto'>, | ||
columnsConfig?: ColumnConfigs, | ||
columnDefault?: ColumnUserConfig, | ||
totalWidth = process.stdout.columns): number[] => { | ||
const widths = maxColumnWidths.map((maxColumnWidth, columnIndex) => { | ||
return columnsConfig?.[columnIndex]?.width ?? columnDefault?.width ?? maxColumnWidth; | ||
}); | ||
|
||
rows.forEach((row) => { | ||
const cellWidths = calculateCellWidths(row); | ||
const remainWidth = calculateRemainWidth( | ||
{columnConfigs: columnsConfig, | ||
columnDefault, | ||
totalWidth, | ||
widths}, | ||
); | ||
const autoColumnCount = maxColumnWidths.filter((_, columnIndex) => { | ||
return 'auto' === (columnsConfig?.[columnIndex]?.width ?? columnDefault?.width); | ||
}).length; | ||
const autoWidths = distributeUnevenly(remainWidth, autoColumnCount); | ||
|
||
cellWidths.forEach((cellWidth, cellIndex) => { | ||
columnWidths[cellIndex] = Math.max(columnWidths[cellIndex], cellWidth); | ||
}); | ||
}); | ||
return widths.map((width, index) => { | ||
if (width !== 'auto') { | ||
return width; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const autoWidth = autoWidths.shift()!; | ||
|
||
if (autoWidth >= 0) { | ||
return autoWidth; | ||
} | ||
|
||
return columnWidths; | ||
if (typeof columnDefault?.width === 'number') { | ||
return columnDefault.width; | ||
} | ||
|
||
const maxWidth = maxColumnWidths[index]; | ||
if (typeof maxWidth === 'number') { | ||
return maxWidth; | ||
} | ||
|
||
throw new Error('There is not available space for draw the table'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { | ||
calculateCellWidths, | ||
} from './calculateCellWidths'; | ||
import type { | ||
Row, | ||
} from './types/internal'; | ||
|
||
/** | ||
* Produces an array of values that describe the largest value length (width) in every column. | ||
*/ | ||
export const calculateMaximumColumnWidths = (rows: Row[]): number[] => { | ||
const columnWidths = new Array(rows[0].length).fill(0); | ||
|
||
rows.forEach((row) => { | ||
const cellWidths = calculateCellWidths(row); | ||
|
||
cellWidths.forEach((cellWidth, cellIndex) => { | ||
columnWidths[cellIndex] = Math.max(columnWidths[cellIndex], cellWidth); | ||
}); | ||
}); | ||
|
||
return columnWidths; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.