1
0

Compare commits

...

10 Commits

9 changed files with 316 additions and 197 deletions

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright © 2022 Rokas Puzonas
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

123
package-lock.json generated
View File

@ -3062,6 +3062,11 @@
"resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz",
"integrity": "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==" "integrity": "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw=="
}, },
"array-uniq": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-1.0.3.tgz",
"integrity": "sha1-r2rId6Jcx/dOBYiUdThY39sk/bY="
},
"array.prototype.flat": { "array.prototype.flat": {
"version": "1.2.5", "version": "1.2.5",
"resolved": "https://registry.npmjs.org/array.prototype.flat/-/array.prototype.flat-1.2.5.tgz", "resolved": "https://registry.npmjs.org/array.prototype.flat/-/array.prototype.flat-1.2.5.tgz",
@ -4532,6 +4537,11 @@
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.65.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.65.tgz",
"integrity": "sha512-0/d8Skk8sW3FxXP0Dd6MnBlrwx7Qo9cqQec3BlIAlvKnrmS3pHsIbaroEi+nd0kZkGpQ6apMEre7xndzjlEnLw==" "integrity": "sha512-0/d8Skk8sW3FxXP0Dd6MnBlrwx7Qo9cqQec3BlIAlvKnrmS3pHsIbaroEi+nd0kZkGpQ6apMEre7xndzjlEnLw=="
}, },
"email-addresses": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/email-addresses/-/email-addresses-3.1.0.tgz",
"integrity": "sha512-k0/r7GrWVL32kZlGwfPNgB2Y/mMXVTq/decgLczm/j34whdaspNrZO8CnXPf1laaHxI6ptUlsnAxN+UAPw+fzg=="
},
"emittery": { "emittery": {
"version": "0.8.1", "version": "0.8.1",
"resolved": "https://registry.npmjs.org/emittery/-/emittery-0.8.1.tgz", "resolved": "https://registry.npmjs.org/emittery/-/emittery-0.8.1.tgz",
@ -5336,6 +5346,21 @@
"minimatch": "^3.0.4" "minimatch": "^3.0.4"
} }
}, },
"filename-reserved-regex": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/filename-reserved-regex/-/filename-reserved-regex-2.0.0.tgz",
"integrity": "sha1-q/c9+rc10EVECr/qLZHzieu/oik="
},
"filenamify": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/filenamify/-/filenamify-4.3.0.tgz",
"integrity": "sha512-hcFKyUG57yWGAzu1CMt/dPzYZuv+jAJUT85bL8mrXvNe6hWj6yEHEc4EdcgiA6Z3oi1/9wXJdZPXF2dZNgwgOg==",
"requires": {
"filename-reserved-regex": "^2.0.0",
"strip-outer": "^1.0.1",
"trim-repeated": "^1.0.0"
}
},
"filesize": { "filesize": {
"version": "8.0.7", "version": "8.0.7",
"resolved": "https://registry.npmjs.org/filesize/-/filesize-8.0.7.tgz", "resolved": "https://registry.npmjs.org/filesize/-/filesize-8.0.7.tgz",
@ -5629,6 +5654,70 @@
"get-intrinsic": "^1.1.1" "get-intrinsic": "^1.1.1"
} }
}, },
"gh-pages": {
"version": "3.2.3",
"resolved": "https://registry.npmjs.org/gh-pages/-/gh-pages-3.2.3.tgz",
"integrity": "sha512-jA1PbapQ1jqzacECfjUaO9gV8uBgU6XNMV0oXLtfCX3haGLe5Atq8BxlrADhbD6/UdG9j6tZLWAkAybndOXTJg==",
"requires": {
"async": "^2.6.1",
"commander": "^2.18.0",
"email-addresses": "^3.0.1",
"filenamify": "^4.3.0",
"find-cache-dir": "^3.3.1",
"fs-extra": "^8.1.0",
"globby": "^6.1.0"
},
"dependencies": {
"array-union": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/array-union/-/array-union-1.0.2.tgz",
"integrity": "sha1-mjRBDk9OPaI96jdb5b5w8kd47Dk=",
"requires": {
"array-uniq": "^1.0.1"
}
},
"commander": {
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ=="
},
"fs-extra": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"globby": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/globby/-/globby-6.1.0.tgz",
"integrity": "sha1-9abXDoOV4hyFj7BInWTfAkJNUGw=",
"requires": {
"array-union": "^1.0.1",
"glob": "^7.0.3",
"object-assign": "^4.0.1",
"pify": "^2.0.0",
"pinkie-promise": "^2.0.0"
}
},
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"requires": {
"graceful-fs": "^4.1.6"
}
},
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
}
}
},
"glob": { "glob": {
"version": "7.2.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz",
@ -8462,6 +8551,24 @@
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==" "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="
}, },
"pify": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz",
"integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw="
},
"pinkie": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz",
"integrity": "sha1-clVrgM+g1IqXToDnckjoDtT3+HA="
},
"pinkie-promise": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz",
"integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=",
"requires": {
"pinkie": "^2.0.0"
}
},
"pirates": { "pirates": {
"version": "4.0.5", "version": "4.0.5",
"resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz",
@ -10460,6 +10567,14 @@
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
"integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==" "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig=="
}, },
"strip-outer": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/strip-outer/-/strip-outer-1.0.1.tgz",
"integrity": "sha512-k55yxKHwaXnpYGsOzg4Vl8+tDrWylxDEpknGjhTiZB8dFRU5rTo9CAzeycivxV3s+zlTKwrs6WxMxR95n26kwg==",
"requires": {
"escape-string-regexp": "^1.0.2"
}
},
"style-loader": { "style-loader": {
"version": "3.3.1", "version": "3.3.1",
"resolved": "https://registry.npmjs.org/style-loader/-/style-loader-3.3.1.tgz", "resolved": "https://registry.npmjs.org/style-loader/-/style-loader-3.3.1.tgz",
@ -10769,6 +10884,14 @@
"punycode": "^2.1.1" "punycode": "^2.1.1"
} }
}, },
"trim-repeated": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/trim-repeated/-/trim-repeated-1.0.0.tgz",
"integrity": "sha1-42RqLqTokTEr9+rObPsFOAvAHCE=",
"requires": {
"escape-string-regexp": "^1.0.2"
}
},
"tryer": { "tryer": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz", "resolved": "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz",

View File

@ -1,7 +1,8 @@
{ {
"name": "table-method-generator", "name": "table-method-generator",
"version": "0.1.0", "version": "0.1.0",
"private": true, "homepage": "https://rokaspuzonas.github.io/table-method-generator",
"license": "MIT",
"dependencies": { "dependencies": {
"@testing-library/jest-dom": "^5.16.2", "@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.2", "@testing-library/react": "^12.1.2",
@ -10,6 +11,7 @@
"@types/node": "^16.11.22", "@types/node": "^16.11.22",
"@types/react": "^17.0.39", "@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11", "@types/react-dom": "^17.0.11",
"gh-pages": "^3.2.3",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-scripts": "5.0.0", "react-scripts": "5.0.0",
@ -21,7 +23,9 @@
"start": "react-scripts start", "start": "react-scripts start",
"build": "react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject" "eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}, },
"eslintConfig": { "eslintConfig": {
"extends": [ "extends": [

View File

@ -1,14 +1,3 @@
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App { .App {
padding: 3em; padding: 3em;
background-color: #1E1E1E; background-color: #1E1E1E;
@ -17,19 +6,22 @@
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
font-size: calc(10px + 1vmin); font-size: 16px;
color: white; color: white;
} }
.App-link { input, select, button {
color: #61dafb; background-color: #131313;
color: white;
border: 1px solid black;
margin: 0.25em 0.5em;
padding: 0.5em;
} }
@keyframes App-logo-spin { button:hover {
from { background-color: #1F1F1F;
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
} }
button:active {
background-color: black;
} }

View File

@ -2,33 +2,64 @@ import { useState } from 'react';
import './App.css'; import './App.css';
import TableDefinitionForm from './TableDefinitionForm'; import TableDefinitionForm from './TableDefinitionForm';
import TableMethodCodeBlock from './TableMethodCodeBlock'; import TableMethodCodeBlock from './TableMethodCodeBlock';
import { TableDefinition } from './TableMethodGenerator'; import { TableColumns } from './TableMethodGenerator';
// TODO: Add section for testing how rendered table looks
// TODO: Render resizeable table which automatically update the necessary fields
function App() { function App() {
let [definition, setDefinition] = useState<TableDefinition>({ let initialColumns: TableColumns = []
names: ["Miestas", "Atsakingas", "Vardas", "Adresas", "Metai"],
fields: ["City", "Manager", "Name", "Address", "Year"],
widths: [10, 20, 18, 15, 5],
alignments: ["left", "left", "left", "left", "right"]
})
let [data, setData] = useState({ if (process.env.NODE_ENV === "development") {
definition initialColumns = [
}); {
name: "Miestas",
field: "City",
width: 10,
alignment: "left"
},
{
name: "Atsakingas",
field: "Manager",
width: 20,
alignment: "left"
},
{
name: "Vardas",
field: "Name",
width: 18,
alignment: "left"
},
{
name: "Address",
field: "Adress",
width: 15,
alignment: "left"
},
{
name: "Metai",
field: "Year",
width: 5,
alignment: "right"
}
]
}
const onChange = (e: TableDefinition) => { let [columns, setColumns] = useState<TableColumns>(initialColumns)
definition = e
data.definition = e // let [generatorOptions, setGeneratorOptions] = useState<TableMethodGeneratorOptions>({});
setDefinition(e)
setData({...data}) const onChange = (e: TableColumns) => {
setColumns([...e])
} }
return ( return (
<div className="App"> <div className="App">
<main> <main>
<TableDefinitionForm value={definition} onChange={onChange} /> <TableDefinitionForm value={columns} onChange={onChange} />
<hr /> <hr />
{TableMethodCodeBlock(data)} <TableMethodCodeBlock columns={columns}/>
</main> </main>
</div> </div>
); );

View File

@ -1,55 +1,46 @@
import { ChangeEvent, useState } from "react" import { ChangeEvent, useState } from "react"
import { Alignment, TableDefinition } from "./TableMethodGenerator" import { Alignment, generate, TableColumn, TableColumns } from "./TableMethodGenerator"
interface TableColumnProps {
interface TableDefinitionRowProps { value: TableColumn
value: Row onChange: { (e: TableColumn): void }
onChange?: { (e: Row): void }
} }
function TableDefinitionRow(props: TableDefinitionRowProps) { function TableDefinitionRow(props: TableColumnProps) {
let [, setName] = useState(props.value.name) let [, setName] = useState(props.value.name)
const onChangeName = (e: ChangeEvent<HTMLInputElement>) => { const onChangeName = (e: ChangeEvent<HTMLInputElement>) => {
props.value.name = e.target.value props.value.name = e.target.value
setName(props.value.name) setName(props.value.name)
if (props.onChange) {
props.onChange(props.value) props.onChange(props.value)
} }
}
let [, setField] = useState(props.value.field) let [, setField] = useState(props.value.field)
const onChangeField = (e: ChangeEvent<HTMLInputElement>) => { const onChangeField = (e: ChangeEvent<HTMLInputElement>) => {
props.value.field = e.target.value props.value.field = e.target.value
setField(props.value.field) setField(props.value.field)
if (props.onChange) {
props.onChange(props.value) props.onChange(props.value)
} }
}
let [, setWidth] = useState(props.value.width) let [, setWidth] = useState(props.value.width)
const onChangeWidth = (e: ChangeEvent<HTMLInputElement>) => { const onChangeWidth = (e: ChangeEvent<HTMLInputElement>) => {
props.value.width = parseFloat(e.target.value) props.value.width = parseFloat(e.target.value)
setWidth(props.value.width) setWidth(props.value.width)
if (props.onChange) {
props.onChange(props.value) props.onChange(props.value)
} }
}
let [, setAlignment] = useState(props.value.alighment) let [, setAlignment] = useState(props.value.alignment)
const onChangeAlignment = (e: ChangeEvent<HTMLSelectElement>) => { const onChangeAlignment = (e: ChangeEvent<HTMLSelectElement>) => {
props.value.alighment = e.target.value as Alignment props.value.alignment = e.target.value as Alignment
setAlignment(props.value.alighment) setAlignment(props.value.alignment)
if (props.onChange) {
props.onChange(props.value) props.onChange(props.value)
} }
}
return ( return (
<li> <li>
<input type="text" value={props.value.name} onChange={onChangeName} /> <input type="text" value={props.value.name} onChange={onChangeName} />
<input type="text" value={props.value.field} onChange={onChangeField} /> <input type="text" value={props.value.field} onChange={onChangeField} />
<input type="number" value={props.value.width || props.value.name.length} onChange={onChangeWidth}/> <input type="number" value={props.value.width || props.value.name.length} onChange={onChangeWidth}/>
<select value={props.value.alighment} onChange={onChangeAlignment}> <select value={props.value.alignment} onChange={onChangeAlignment}>
<option value="right">Right</option> <option value="right">Right</option>
<option value="left">Left</option> <option value="left">Left</option>
</select> </select>
@ -58,91 +49,57 @@ function TableDefinitionRow(props: TableDefinitionRowProps) {
} }
interface TableDefinitionProps { interface TableDefinitionProps {
value: TableDefinition value: TableColumns
onChange?: { (e: TableDefinition): void } onChange?: { (e: TableColumns): void }
}
interface Row {
name: string
width?: number
field?: string
alighment?: Alignment
}
function intoRows(definition: TableDefinition): Row[] {
const rows: Row[] = []
if (definition.names !== undefined) {
for (let i = 0; i < definition.names.length; i++) {
rows.push({
name: definition.names[i],
width: (definition.widths || [])[i],
field: (definition.fields || [])[i],
alighment: (definition.alignments || [])[i],
})
}
}
return rows
}
function fromRows(rows: Row[]): TableDefinition {
const definition: TableDefinition = {
names: [],
widths: [],
fields: [],
alignments: []
}
for (const row of rows) {
(definition.names as string[]).push(row.name);
(definition.widths as (number|undefined)[]).push(row.width);
(definition.fields as (string|undefined)[]).push(row.field);
(definition.alignments as (Alignment|undefined)[]).push(row.alighment);
}
return definition
} }
function TableDefinitionForm(props: TableDefinitionProps) { function TableDefinitionForm(props: TableDefinitionProps) {
let [currentName, setCurrentName] = useState("") let [currentName, setCurrentName] = useState("")
let [rows, setRows] = useState<Row[]>(intoRows(props.value)) let [columns, setColumns] = useState<TableColumns>(props.value)
const addRow = () => { const addRow = () => {
rows.push({ name: currentName }) if (currentName.length > 0) {
columns.push({ name: currentName })
setCurrentName("") setCurrentName("")
if (props.onChange !== undefined) { if (props.onChange !== undefined) {
props.onChange(fromRows(rows)) props.onChange(columns)
}
} }
} }
const updateRow = (i: number, row: Row) => { const updateRow = (i: number, column: TableColumn) => {
if (row.name === "") { if (column.name === "") {
rows.splice(i, 1) columns.splice(i, 1)
rows = [...rows] setColumns([...columns])
setRows(rows)
} else { } else {
rows = [...rows] columns[i] = column
rows[i] = row setColumns([...columns])
setRows(rows)
} }
if (props.onChange !== undefined) { if (props.onChange !== undefined) {
props.onChange(fromRows(rows)) props.onChange(columns)
} }
} }
const onClickCopy = () => {
navigator.clipboard.writeText(generate(columns))
}
return ( return (
<form onSubmit={(e) => e.preventDefault()}> <form onSubmit={(e) => e.preventDefault()}>
<label>Column:</label> <label>Column name:</label>
<input <input
type="text" type="text"
value={currentName} value={currentName}
onChange={(e) => setCurrentName(e.target.value)} onChange={(e) => setCurrentName(e.target.value)}
onKeyPress={(e) => e.key === "Enter" && addRow()} onKeyPress={(e) => e.key === "Enter" && addRow()}
/> />
<button onClick={addRow}>Insert column</button>
<button onClick={onClickCopy}>Copy code 📋!</button>
<ol> <ol>
{ {columns.map((item, i) =>
rows.map((item, i) =>
<TableDefinitionRow key={i} value={item} onChange={(e) => updateRow(i, e)}/> <TableDefinitionRow key={i} value={item} onChange={(e) => updateRow(i, e)}/>
) )}
}
</ol> </ol>
</form> </form>
) )

View File

@ -1,16 +1,24 @@
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'; import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
import csharp from 'react-syntax-highlighter/dist/esm/languages/hljs/csharp'; import csharp from 'react-syntax-highlighter/dist/esm/languages/hljs/csharp';
import { vs2015 } from 'react-syntax-highlighter/dist/esm/styles/hljs'; import { vs2015 } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import { generate, TableMethodGeneratorOptions } from './TableMethodGenerator'; import { generate, TableColumns, TableMethodGeneratorOptions } from './TableMethodGenerator';
SyntaxHighlighter.registerLanguage('csharp', csharp); SyntaxHighlighter.registerLanguage('csharp', csharp);
function TableMethodCodeBlock(props: TableMethodGeneratorOptions) { interface Props {
columns: TableColumns
options?: TableMethodGeneratorOptions
}
function TableMethodCodeBlock(props: Props) {
return ( return (
<div>
<SyntaxHighlighter language="csharp" style={vs2015}> <SyntaxHighlighter language="csharp" style={vs2015}>
{generate(props)} {generate(props.columns, props.options)}
</SyntaxHighlighter> </SyntaxHighlighter>
</div>
) )
} }
export default TableMethodCodeBlock export default TableMethodCodeBlock

View File

@ -1,15 +1,15 @@
export type Alignment = "left"|"right" export type Alignment = "left"|"right"
export interface TableDefinition { export interface TableColumn {
names?: string[] name: string
fields?: (string|undefined)[] field?: string
widths?: (number|undefined)[] width?: number
alignments?: (Alignment|undefined)[] alignment?: Alignment
} }
export interface TableMethodGeneratorOptions { export type TableColumns = TableColumn[]
definition: TableDefinition
export interface TableMethodGeneratorOptions {
method_name?: string method_name?: string
empty_message?: string empty_message?: string
@ -19,47 +19,38 @@ export interface TableMethodGeneratorOptions {
entry_name?: string entry_name?: string
} }
function get_total_width(definition: TableDefinition): number { function get_total_width(columns: TableColumns): number {
let names = definition.names || [] let total_width = 0;
if (names.length === 0) { return 0; }
let total_width = 0 for (const column of columns) {
let widths = definition.widths || [] total_width += column.width || column.name.length
for (let i = 0; i < names.length; i++) {
const name = names[i]
total_width += widths[i] || name.length
} }
total_width += 2 + 2; total_width += 2 + 2;
total_width += 3*(names.length-1); total_width += 3*(columns.length-1);
return total_width; return total_width;
} }
export function generate(options: TableMethodGeneratorOptions): string { export function generate(columns: TableColumns, options?: TableMethodGeneratorOptions): string {
const section_names = options.definition.names || [] const method_name = options?.method_name || "PrintTable"
const method_name = options.method_name || "PrintTable" if (columns.length === 0) {
if (section_names.length === 0) {
return `static void ${method_name}()\n{\n}`; return `static void ${method_name}()\n{\n}`;
} }
const empty_message = options.empty_message || "Empty" const empty_message = options?.empty_message || "Empty"
const container_name = options.container_name || "container" const container_name = options?.container_name || "container"
const container_type = options.container_type || "Container" const container_type = options?.container_type || "Container"
const entry_name = options.entry_name || "e" const entry_name = options?.entry_name || "e"
const entry_type = options.entry_type || "Entry" const entry_type = options?.entry_type || "Entry"
const section_widths = options.definition.widths || []
const section_alignments = options.definition.alignments || []
const section_fields = options.definition.fields || []
// Total width // Total width
const total_width = get_total_width(options.definition) const total_width = get_total_width(columns)
// Table row // Table row
let row_components: string[] = [] let row_components: string[] = []
for (let i = 0; i < section_names.length; i++) { for (let i = 0; i < columns.length; i++) {
const section = section_names[i] const width = columns[i].width || columns[i].name.length
const width = section_widths[i] || section.length const alighment = columns[i].alignment || "right"
const alighment = section_alignments[i] || "right"
if (alighment === "left") { if (alighment === "left") {
row_components.push(`{${i},-${width}}`) row_components.push(`{${i},-${width}}`)
} else { } else {
@ -67,24 +58,19 @@ export function generate(options: TableMethodGeneratorOptions): string {
} }
} }
let table_row = ""; const table_row = "| " + row_components.join(" | ") + " |"
if (row_components.length > 0) {
table_row = "| " + row_components.join(" | ") + " |"
}
// Section names // Section names
const joined_names = section_names.map((n) => `"${n}"`).join(", ") const joined_names = columns.map((c) => `"${c.name}"`).join(", ")
// Section fields // Section fields
const formatted_fields = [] const joined_fields = columns.map((column) => {
for (let i = 0; i < section_names.length; i++) { if (column.field) {
if (section_fields[i]) { return `${entry_name}.${column.field}`
formatted_fields.push(`${entry_name}.${section_fields[i]}`)
} else { } else {
formatted_fields.push(`"-"`) return `"-"`
} }
} }).join(", ")
const joined_fields = formatted_fields.join(", ")
// Final code string // Final code string
return `static void ${method_name}(${container_type} ${container_name})\n{ return `static void ${method_name}(${container_type} ${container_name})\n{
@ -105,40 +91,39 @@ export function generate(options: TableMethodGeneratorOptions): string {
Console.WriteLine(new string('-', ${total_width}));\n}`; Console.WriteLine(new string('-', ${total_width}));\n}`;
} }
function render_table_row(definition: TableDefinition, entry: string[]): string { function render_table_row(columns: TableColumns, entry: (string|undefined)[]): string {
let names = definition.names || [] if (columns.length === 0) { return "" }
if (names.length === 0) { return ""; }
let formated_cells = []
for (let i = 0; i < columns.length; i++) {
const alighment = columns[i].alignment || "right"
const width = columns[i].width || columns[i].name.length
const columns: string[] = []
const alighments = definition.alignments || []
const widths = definition.widths || []
for (let i = 0; i < names.length; i++) {
const value = entry[i] || ""; const value = entry[i] || "";
const alighment = alighments[i] || "right"
const width = widths[i] || names[i].length
const filler = " ".repeat(Math.max(width - value.length, 0)); const filler = " ".repeat(Math.max(width - value.length, 0));
if (alighment === "left") { if (alighment === "left") {
columns.push(value + filler); formated_cells.push(value + filler);
} else { } else {
columns.push(filler + value); formated_cells.push(filler + value);
} }
} }
return "| " + columns.join(" | ") + " |"
}
export function render_table(definition: TableDefinition, entries: string[][]): string { return "| " + formated_cells.join(" | ") + " |"
if (!definition.names) {return ""; } }
const total_width = get_total_width(definition) export function render_table(columns: TableColumns, entries: string[][]): string {
if (columns.length === 0) { return "" }
const total_width = get_total_width(columns)
const seperator = "-".repeat(total_width) const seperator = "-".repeat(total_width)
const column_names = columns.map((column) => column.name)
const lines = [] const lines = []
lines.push(seperator) lines.push(seperator)
lines.push(render_table_row(definition, definition.names)) lines.push(render_table_row(columns, column_names))
lines.push(seperator) lines.push(seperator)
for (let i = 0; i < entries.length; i++) { for (let i = 0; i < entries.length; i++) {
lines.push(render_table_row(definition, entries[i])) lines.push(render_table_row(columns, entries[i]))
} }
lines.push(seperator) lines.push(seperator)

View File

@ -1,14 +1,14 @@
import { render_table, TableDefinition } from "./TableMethodGenerator" import { render_table, TableColumns } from "./TableMethodGenerator"
export interface TableRendererProps { export interface TableRendererProps {
definition: TableDefinition, columns: TableColumns,
entries: string[][] entries: string[][]
} }
function TableRenderer(props: TableRendererProps) { function TableRenderer(props: TableRendererProps) {
return ( return (
<pre> <pre>
{render_table(props.definition, props.entries)} {render_table(props.columns, props.entries)}
</pre> </pre>
) )
} }