-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from logchange/print-tabel
Added `HofundConnectionsTable` to allow printing HofundConnection to …
- Loading branch information
Showing
8 changed files
with
272 additions
and
1 deletion.
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
10 changes: 10 additions & 0 deletions
10
changelog/unreleased/000002-print_connections_to_terminal.yml
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,10 @@ | ||
title: Added `HofundConnectionsTable` to allow printing HofundConnection to terminal during booting up. | ||
authors: | ||
- name: Peter Zmilczak | ||
nick: marwin1991 | ||
url: https://github.com/marwin1991 | ||
type: added #[added/changed/deprecated/removed/fixed/security/other] | ||
issues: | ||
- 26 | ||
merge_requests: | ||
- 43 |
90 changes: 90 additions & 0 deletions
90
hofund-core/src/main/java/dev/logchange/hofund/AsciiTable.java
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,90 @@ | ||
package dev.logchange.hofund; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class AsciiTable { | ||
|
||
private final List<String> headers; | ||
private final List<List<String>> rows; | ||
|
||
public AsciiTable(List<String> headers) { | ||
this.headers = headers; | ||
this.rows = new ArrayList<>(); | ||
} | ||
|
||
|
||
public void addRow(String... columns) { | ||
if (columns.length != headers.size()) { | ||
throw new IllegalArgumentException("Number of columns must match number of headers"); | ||
} | ||
rows.add(Arrays.asList(columns)); | ||
} | ||
|
||
public String printTable() { | ||
StringBuilder table = new StringBuilder(); | ||
int[] columnWidths = new int[headers.size()]; | ||
|
||
// Calculate the width of each column | ||
for (int i = 0; i < headers.size(); i++) { | ||
columnWidths[i] = headers.get(i).length(); | ||
} | ||
|
||
for (List<String> row : rows) { | ||
for (int i = 0; i < row.size(); i++) { | ||
columnWidths[i] = Math.max(columnWidths[i], row.get(i).length()); | ||
} | ||
} | ||
|
||
// Append the headers | ||
table.append(printSeparator(columnWidths)); | ||
table.append(printRow(headers, columnWidths)); | ||
table.append(printSeparator(columnWidths)); | ||
|
||
// Append the rows | ||
for (List<String> row : rows) { | ||
table.append(printRow(row, columnWidths)); | ||
} | ||
table.append(printSeparator(columnWidths)); | ||
|
||
return table.toString(); | ||
} | ||
|
||
private String printRow(List<String> row, int[] columnWidths) { | ||
StringBuilder rowString = new StringBuilder(); | ||
rowString.append("| "); | ||
for (int i = 0; i < row.size(); i++) { | ||
if (i == row.size() - 1) { | ||
rowString.append(padRight(row.get(i), columnWidths[i])).append(" |"); | ||
} else { | ||
rowString.append(padRight(row.get(i), columnWidths[i])).append(" | "); | ||
} | ||
} | ||
rowString.append("\n"); | ||
return rowString.toString(); | ||
} | ||
|
||
private String printSeparator(int[] columnWidths) { | ||
StringBuilder separator = new StringBuilder(); | ||
separator.append("+"); | ||
for (int width : columnWidths) { | ||
separator.append(repeat("-", width + 2)).append("+"); | ||
} | ||
separator.append("\n"); | ||
return separator.toString(); | ||
} | ||
|
||
private String padRight(String text, int length) { | ||
return String.format("%-" + length + "s", text); | ||
} | ||
|
||
private String repeat(String str, int times) { | ||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0; i < times; i++) { | ||
result.append(str); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java
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,43 @@ | ||
package dev.logchange.hofund.connection; | ||
|
||
import dev.logchange.hofund.AsciiTable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class HofundConnectionsTable { | ||
|
||
private static final List<String> HEADERS = Arrays.asList("TYPE", "NAME", "STATUS", "URL"); | ||
|
||
private final List<HofundConnection> connections; | ||
|
||
public HofundConnectionsTable(List<HofundConnectionsProvider> connectionsProviders) { | ||
List<HofundConnection> connections = new ArrayList<>(); | ||
for (HofundConnectionsProvider connectionsProvider : connectionsProviders) { | ||
connections.addAll(connectionsProvider.getConnections()); | ||
} | ||
|
||
this.connections = connections; | ||
} | ||
|
||
public String print() { | ||
AsciiTable table = new AsciiTable(HEADERS); | ||
|
||
for (HofundConnection connection : connections) { | ||
table.addRow( | ||
connection.getType().name(), | ||
connection.getTarget(), | ||
connection.getFun().get().getStatus().getName(), | ||
connection.getUrl() | ||
); | ||
} | ||
|
||
return table.printTable(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return print(); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
hofund-core/src/test/java/dev/logchange/hofund/AsciiTableTest.java
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,61 @@ | ||
package dev.logchange.hofund; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class AsciiTableTest { | ||
|
||
@Test | ||
public void testEmptyTable() { | ||
AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); | ||
String expected = | ||
"+------+------+--------+-----+\n" + | ||
"| TYPE | NAME | STATUS | URL |\n" + | ||
"+------+------+--------+-----+\n" + | ||
"+------+------+--------+-----+\n"; | ||
assertEquals(expected, tablePrinter.printTable()); | ||
} | ||
|
||
@Test | ||
public void testSingleRow() { | ||
AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); | ||
tablePrinter.addRow("Type1", "Name1", "Status1", "http://url1.com"); | ||
String expected = | ||
"+-------+-------+---------+-----------------+\n" + | ||
"| TYPE | NAME | STATUS | URL |\n" + | ||
"+-------+-------+---------+-----------------+\n" + | ||
"| Type1 | Name1 | Status1 | http://url1.com |\n" + | ||
"+-------+-------+---------+-----------------+\n"; | ||
assertEquals(expected, tablePrinter.printTable()); | ||
} | ||
|
||
@Test | ||
public void testMultipleRows() { | ||
AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); | ||
tablePrinter.addRow("Type1", "Name1", "Status1", "http://url1.com"); | ||
tablePrinter.addRow("Type2", "Name2", "Status2", "http://url2.com"); | ||
tablePrinter.addRow("Type3", "Name3", "Status3", "http://url3.com"); | ||
String expected = | ||
"+-------+-------+---------+-----------------+\n" + | ||
"| TYPE | NAME | STATUS | URL |\n" + | ||
"+-------+-------+---------+-----------------+\n" + | ||
"| Type1 | Name1 | Status1 | http://url1.com |\n" + | ||
"| Type2 | Name2 | Status2 | http://url2.com |\n" + | ||
"| Type3 | Name3 | Status3 | http://url3.com |\n" + | ||
"+-------+-------+---------+-----------------+\n"; | ||
assertEquals(expected, tablePrinter.printTable()); | ||
} | ||
|
||
@Test | ||
public void testAddRowWithInvalidColumnCount() { | ||
AsciiTable tablePrinter = new AsciiTable(Arrays.asList("TYPE", "NAME", "STATUS", "URL")); | ||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { | ||
tablePrinter.addRow("Type1", "Name1", "Status1"); // Missing one column | ||
}); | ||
assertEquals("Number of columns must match number of headers", thrown.getMessage()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ev/logchange/hofund/connection/springboot/autoconfigure/ConnectionTabelAutoConfigure.java
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 @@ | ||
package dev.logchange.hofund.connection.springboot.autoconfigure; | ||
|
||
import dev.logchange.hofund.connection.HofundConnectionsTable; | ||
import dev.logchange.hofund.connection.HofundConnectionsProvider; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Configuration(proxyBeanMethods = false) | ||
public class ConnectionTabelAutoConfigure { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
@ConditionalOnProperty(name = "hofund.connections-tabel", havingValue = "true", matchIfMissing = true) | ||
public HofundConnectionsTable connectionsTable(List<HofundConnectionsProvider> hofundConnectionsProviders) { | ||
return new HofundConnectionsTable(hofundConnectionsProviders); | ||
} | ||
} |
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