shipthis
Version:
ShipThis manages building and uploading your Godot games to the App Store and Google Play.
102 lines (99 loc) • 3.85 kB
JavaScript
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { Box, Text } from 'ink';
import React from 'react';
const BASE_HEADER_PROPS = {
bold: true,
color: "blue"
};
const BASE_TEXT_PROPS = {
color: "white"
};
function generateHeaders(data) {
const headers = {};
for (const row of data) {
for (const key of Object.keys(row)) {
headers[key] = key;
}
}
return headers;
}
const Table = ({ columnTextProps, data, getTextProps, headerTextProps, showHeaders = true }) => {
const columns = getColumns(data);
const fullHeaderTextProps = {
...BASE_HEADER_PROPS,
...headerTextProps
};
return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
renderHeaderSeparators(columns),
showHeaders && /* @__PURE__ */ jsxs(Fragment, { children: [
renderRow(generateHeaders(data), columns, fullHeaderTextProps),
renderRowSeparators(columns)
] }),
data.map((row, index) => /* @__PURE__ */ jsxs(React.Fragment, { children: [
index !== 0 && renderRowSeparators(columns),
renderRow(row, columns, BASE_TEXT_PROPS, columnTextProps, getTextProps)
] }, `row-${index}`)),
renderFooterSeparators(columns)
] });
};
function getColumns(data) {
const columnWidths = {};
for (const row of data) {
for (const key of Object.keys(row)) {
const valueLength = row[key]?.toString().length || 0;
columnWidths[key] = Math.max(columnWidths[key] || key.length, valueLength);
}
}
return Object.keys(columnWidths).map((key) => ({
key,
width: (columnWidths[key] ?? 0) + 2
// adding padding
}));
}
function renderRow(row, columns, baseCellTextProps, columnTextProps, getTextProps) {
const getDisplayValue = (row2, column) => {
const value = row2[column.key];
if (typeof value === "boolean") return value ? "YES" : "NO";
return value?.toString() || "";
};
const getTextPropsForCell = (row2, column) => {
const { key } = column;
const value = row2[key];
const columnTextPropsForCell = columnTextProps?.[key] || {};
const valueBasedProps = typeof row2[column.key] === "boolean" ? { color: value == false ? "red" : "green" } : {};
const callbackBasedProps = getTextProps ? getTextProps(column, value) : {};
return {
...baseCellTextProps,
...columnTextPropsForCell,
...valueBasedProps,
...callbackBasedProps
};
};
return /* @__PURE__ */ jsxs(Box, { flexDirection: "row", children: [
/* @__PURE__ */ jsx(Text, { children: "\u2502" }),
columns.map((column, index) => {
const cellTextProps = getTextPropsForCell(row, column);
return /* @__PURE__ */ jsxs(React.Fragment, { children: [
index !== 0 && /* @__PURE__ */ jsx(Text, { children: "\u2502" }),
/* @__PURE__ */ jsx(Box, { justifyContent: "center", width: column.width, children: /* @__PURE__ */ jsx(Text, { ...cellTextProps, children: getDisplayValue(row, column) }) })
] }, column.key);
}),
/* @__PURE__ */ jsx(Text, { children: "\u2502" })
] });
}
function renderHeaderSeparators(columns) {
return renderRowSeparators(columns, "\u250C", "\u252C", "\u2510");
}
function renderFooterSeparators(columns) {
return renderRowSeparators(columns, "\u2514", "\u2534", "\u2518");
}
function renderRowSeparators(columns, leftChar = "\u251C", midChar = "\u253C", rightChar = "\u2524") {
return /* @__PURE__ */ jsxs(Box, { flexDirection: "row", children: [
/* @__PURE__ */ jsx(Text, { children: leftChar }),
columns.map((column, index) => /* @__PURE__ */ jsxs(React.Fragment, { children: [
/* @__PURE__ */ jsx(Text, { children: "\u2500".repeat(column.width) }),
index < columns.length - 1 ? /* @__PURE__ */ jsx(Text, { children: midChar }) : /* @__PURE__ */ jsx(Text, { children: rightChar })
] }, column.key))
] });
}
export { Table as T };