printy printing of tables
parent
3b03bee19a
commit
e8e1376947
|
@ -0,0 +1,124 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -1,12 +1,30 @@
|
||||||
package ca.cosc3p91.a2;
|
package ca.cosc3p91.a2;
|
||||||
|
|
||||||
import ca.cosc3p91.a2.game.GameEngine;
|
import ca.cosc3p91.a2.game.GameEngine;
|
||||||
|
import ca.cosc3p91.a2.util.Print;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
GameEngine engine = new GameEngine();
|
GameEngine engine = new GameEngine();
|
||||||
|
|
||||||
|
Print test = new Print("Hello There", 2, 500);
|
||||||
|
|
||||||
|
test.addColumn(new Print.Column("I am value 1"));
|
||||||
|
test.addColumn(new Print.Column("Super Value"));
|
||||||
|
test.addColumn(new Print.Column("SOLD!"));
|
||||||
|
|
||||||
|
Print.Row row = new Print.Row();
|
||||||
|
row.add("HelloThere");
|
||||||
|
row.add("Goodbye");
|
||||||
|
row.add("3");
|
||||||
|
|
||||||
|
test.addRow(row);
|
||||||
|
test.addRow(row);
|
||||||
|
test.addRow(row);
|
||||||
|
|
||||||
|
Print.print(test.createTable());
|
||||||
|
|
||||||
engine.run();
|
engine.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,209 @@
|
||||||
package ca.cosc3p91.a2.util;
|
package ca.cosc3p91.a2.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ported from blt::TableFormatter (C++)
|
||||||
|
* https://github.com/Tri11Paragon/BLT/
|
||||||
|
*/
|
||||||
public class Print {
|
public class Print {
|
||||||
|
|
||||||
|
public static class Row {
|
||||||
|
private final ArrayList<String> values;
|
||||||
|
|
||||||
|
public Row(ArrayList<String> row) {
|
||||||
|
this.values = row;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Row(){
|
||||||
|
this.values = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(String value){
|
||||||
|
values.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Column {
|
||||||
|
private final String columnName;
|
||||||
|
private long maxColumnLength = 0;
|
||||||
|
|
||||||
|
public Column(String columnName){
|
||||||
|
this.columnName = columnName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final ArrayList<Row> rows = new ArrayList<>();
|
||||||
|
private final ArrayList<Column> columns = new ArrayList<>();
|
||||||
|
|
||||||
|
private String tableName;
|
||||||
|
private int columnPadding;
|
||||||
|
private int maxColumnWidth;
|
||||||
|
|
||||||
|
public Print(String tableName, int columnPadding, int maxColumnWidth){
|
||||||
|
this.tableName = tableName;
|
||||||
|
this.columnPadding = columnPadding;
|
||||||
|
this.maxColumnWidth = maxColumnWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Print(){
|
||||||
|
this("", 2, 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createPadding(int amount) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (int i = 0; i < amount; i++){
|
||||||
|
builder.append(' ');
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateTopSelector(long size) {
|
||||||
|
long sizeOfName = tableName.isEmpty() ? 0 : tableName.length() + 4;
|
||||||
|
long sizeNameRemoved = size - sizeOfName;
|
||||||
|
|
||||||
|
StringBuilder halfWidthLeftSeparator = new StringBuilder();
|
||||||
|
StringBuilder halfWidthRightSeparator = new StringBuilder();
|
||||||
|
|
||||||
|
long sizeNameFloor = (long) Math.floor((double)sizeNameRemoved / 2.0);
|
||||||
|
long sizeNameCeil = (long) Math.ceil((double)sizeNameRemoved / 2.0);
|
||||||
|
|
||||||
|
halfWidthLeftSeparator.append('+');
|
||||||
|
|
||||||
|
for (int i = 0; i < sizeNameFloor - 1; i++)
|
||||||
|
halfWidthLeftSeparator.append('-');
|
||||||
|
for (int i = 0; i < sizeNameCeil - 1; i++)
|
||||||
|
halfWidthRightSeparator.append('-');
|
||||||
|
|
||||||
|
halfWidthRightSeparator.append('+');
|
||||||
|
|
||||||
|
StringBuilder separator = new StringBuilder();
|
||||||
|
separator.append(halfWidthLeftSeparator.toString());
|
||||||
|
if (sizeOfName != 0){
|
||||||
|
separator.append("{ ");
|
||||||
|
separator.append(tableName);
|
||||||
|
separator.append(" }");
|
||||||
|
}
|
||||||
|
separator.append(halfWidthRightSeparator);
|
||||||
|
return separator.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateColumnHeader(){
|
||||||
|
updateColumnLengths();
|
||||||
|
StringBuilder header = new StringBuilder();
|
||||||
|
header.append('|');
|
||||||
|
|
||||||
|
for (int i = 0; i < columns.size(); i++){
|
||||||
|
Column column = columns.get(i);
|
||||||
|
double columnPaddingLength = ((int)(column.maxColumnLength) - (int)(column.columnName.length()))/2.0;
|
||||||
|
header.append(createPadding((int)(columnPadding + (int)Math.floor(columnPaddingLength))));
|
||||||
|
|
||||||
|
header.append(column.columnName);
|
||||||
|
|
||||||
|
header.append(createPadding((int)(columnPadding + (int)Math.ceil(columnPaddingLength))));
|
||||||
|
if (i < columns.size()-1)
|
||||||
|
header.append('|');
|
||||||
|
}
|
||||||
|
header.append('|');
|
||||||
|
|
||||||
|
return header.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateSeparator(long size) {
|
||||||
|
int nextIndex = 0;
|
||||||
|
int currentColumnIndex = 0;
|
||||||
|
StringBuilder wholeWidthSeparator = new StringBuilder();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (i == nextIndex) {
|
||||||
|
System.out.println(currentColumnIndex + " " + nextIndex + " " + size);
|
||||||
|
int currentColumnSize = (int) (columns.get(currentColumnIndex++).maxColumnLength + columnPadding*2);
|
||||||
|
nextIndex += currentColumnSize + 1;
|
||||||
|
wholeWidthSeparator.append('+');
|
||||||
|
} else
|
||||||
|
wholeWidthSeparator.append('-');
|
||||||
|
}
|
||||||
|
wholeWidthSeparator.append('+');
|
||||||
|
return wholeWidthSeparator.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateColumnLengths() {
|
||||||
|
for (int i = 0; i < columns.size(); i++) {
|
||||||
|
Column column = columns.get(i);
|
||||||
|
column.maxColumnLength = column.columnName.length();
|
||||||
|
for (Row row : rows) {
|
||||||
|
column.maxColumnLength = Math.max(column.maxColumnLength, row.values.get(i).length() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public long columnSize(Column column) {
|
||||||
|
return column.columnName.length() + columnPadding * 2L;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Print addColumn(Column column){
|
||||||
|
columns.add(column);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Print addRow(Row row) {
|
||||||
|
if (row.values.size() > columns.size())
|
||||||
|
throw new RuntimeException("Unable to create row with more values than columns!");
|
||||||
|
if (row.values.size() < columns.size())
|
||||||
|
for (int i = row.values.size(); i < columns.size(); i++)
|
||||||
|
row.values.add("");
|
||||||
|
rows.add(row);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Print addRow(ArrayList<String> row){
|
||||||
|
return addRow(new Row(row));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> createTable(boolean top, boolean bottom, boolean separators){
|
||||||
|
ArrayList<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
|
String header = generateColumnHeader();
|
||||||
|
String topSeparator = generateTopSelector(header.length());
|
||||||
|
String lineSeparator = generateSeparator(header.length() - 1);
|
||||||
|
|
||||||
|
if (top)
|
||||||
|
lines.add(topSeparator);
|
||||||
|
|
||||||
|
lines.add(header);
|
||||||
|
lines.add(lineSeparator);
|
||||||
|
|
||||||
|
for (Row row : rows) {
|
||||||
|
StringBuilder rowLine = new StringBuilder();
|
||||||
|
rowLine.append('|');
|
||||||
|
for (int i = 0; i < row.values.size(); i++){
|
||||||
|
String value = row.values.get(i);
|
||||||
|
Column column = columns.get(i);
|
||||||
|
int spaceLeft = (int)(column.maxColumnLength - value.length());
|
||||||
|
rowLine.append(createPadding((int)Math.floor(spaceLeft / 2.0) + columnPadding));
|
||||||
|
rowLine.append(value);
|
||||||
|
rowLine.append(createPadding((int)Math.ceil(spaceLeft / 2.0) + columnPadding));
|
||||||
|
rowLine.append('|');
|
||||||
|
}
|
||||||
|
lines.add(rowLine.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bottom)
|
||||||
|
lines.add(lineSeparator);
|
||||||
|
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> createBox(){
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> createTable() {
|
||||||
|
return createTable(true, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void print(ArrayList<String> lines){
|
||||||
|
for (String line : lines)
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,16 @@ public class Time {
|
||||||
|
|
||||||
private long timeSeconds;
|
private long timeSeconds;
|
||||||
|
|
||||||
|
public Time() {
|
||||||
|
this.timeSeconds = 0;
|
||||||
|
}
|
||||||
|
|
||||||
protected Time(long timeSeconds) {
|
protected Time(long timeSeconds) {
|
||||||
this.timeSeconds = timeSeconds;
|
this.timeSeconds = timeSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Time offsetSeconds(long seconds){
|
public Time offsetSeconds(long seconds) {
|
||||||
timeSeconds += seconds;
|
this.timeSeconds += seconds;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,27 +25,31 @@ public class Time {
|
||||||
return offsetMinutes(hours * 60);
|
return offsetMinutes(hours * 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Time offsetDays(long days){
|
public Time offsetDays(long days) {
|
||||||
return offsetHours(days * 24);
|
return offsetHours(days * 24);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long get(){
|
public Time offsetTime(Time time) {
|
||||||
|
return offsetSeconds(time.timeSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long get() {
|
||||||
return timeSeconds;
|
return timeSeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class FutureTime {
|
public static class FutureTime {
|
||||||
private final Time futureTime;
|
private final Time futureTime;
|
||||||
|
|
||||||
public FutureTime(Time futureTime){
|
public FutureTime(Time futureTime) {
|
||||||
this.futureTime = futureTime;
|
this.futureTime = futureTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean occurred(){
|
public boolean occurred() {
|
||||||
return getTime().timeSeconds >= futureTime.timeSeconds;
|
return getTime().timeSeconds >= futureTime.timeSeconds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Time getTime(){
|
public static Time getTime() {
|
||||||
return new Time(System.currentTimeMillis() / 1000);
|
return new Time(System.currentTimeMillis() / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue