This is an automated email from the ASF dual-hosted git repository. sruehl pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 15bc36516060df3f9656b694817999b004af3edf Author: Sebastian Rühl <[email protected]> AuthorDate: Sat Apr 17 22:31:57 2021 +0200 plc4go: fixed ascii box alignment issues --- plc4go/internal/plc4go/spi/utils/asciiBox.go | 24 +++++-- plc4go/internal/plc4go/spi/utils/asciiBox_test.go | 78 +++++++++++++++++++++++ 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox.go b/plc4go/internal/plc4go/spi/utils/asciiBox.go index 0e2cda4..1c163eb 100644 --- a/plc4go/internal/plc4go/spi/utils/asciiBox.go +++ b/plc4go/internal/plc4go/spi/utils/asciiBox.go @@ -133,23 +133,39 @@ func BoxString(name string, data string, charWidth int) AsciiBox { return AsciiBox(boxedString) } -func AlignBoxes(boxes []AsciiBox, desiredWith int) AsciiBox { +func AlignBoxes(boxes []AsciiBox, desiredWidth int) AsciiBox { + if len(boxes) == 0 { + return boxes[0] + } + actualWidth := desiredWidth + for _, box := range boxes { + boxWidth := box.Width() + if boxWidth > desiredWidth { + if DebugAsciiBox { + log.Debug().Msgf("Overflow by %d chars", boxWidth-desiredWidth) + } + actualWidth = boxWidth + } + } + if DebugAsciiBox { + log.Debug().Msgf("Working with %d chars", actualWidth) + } bigBox := AsciiBox("") currentBoxRow := make([]AsciiBox, 0) currentRowLength := 0 for _, box := range boxes { currentRowLength += box.Width() - currentBoxRow = append(currentBoxRow, box) - if currentRowLength >= desiredWith { + if currentRowLength > actualWidth { mergedBoxes := mergeHorizontal(currentBoxRow) if bigBox == "" { bigBox = mergedBoxes } else { bigBox = BoxBelowBox(bigBox, mergedBoxes) } - currentRowLength = 0 + currentRowLength = box.Width() currentBoxRow = make([]AsciiBox, 0) } + currentBoxRow = append(currentBoxRow, box) } if len(currentBoxRow) > 0 { // Special case where all boxes fit into one row diff --git a/plc4go/internal/plc4go/spi/utils/asciiBox_test.go b/plc4go/internal/plc4go/spi/utils/asciiBox_test.go index 5b8788f..68a5857 100644 --- a/plc4go/internal/plc4go/spi/utils/asciiBox_test.go +++ b/plc4go/internal/plc4go/spi/utils/asciiBox_test.go @@ -24,6 +24,10 @@ import ( "testing" ) +func init() { + DebugAsciiBox = true +} + func TestBoxAnything(t *testing.T) { type args struct { name string @@ -470,6 +474,80 @@ func TestAlignBoxes(t *testing.T) { desiredWith: 65, }, want: ` +╔═sampleField════════════╗╔═sampleField════════════╗ +║ 123123123123 ║║ 123123123123 ║ +║123123ABABABABABAB123123║║123123123123123123123123║ +╚════════════════════════╝╚════════════════════════╝ +╔═sampleField════════════╗╔═sampleField════════════╗ +║ 123123123123 ║║ 123123123123 ║ +║123123ABABABABABAB123123║║123123123123123123123123║ +╚════════════════════════╝╚════════════════════════╝ +╔═sampleField════════════╗╔═sampleField════════════╗ +║ 123123123123 ║║ 123123123123 ║ +║123123ABABABABABAB123123║║123123123123123123123123║ +╚════════════════════════╝╚════════════════════════╝ +╔═sampleField════════════╗╔═sampleField════════════╗ +║ 123123123123 ║║ 123123123123 ║ +║123123ABABABABABAB123123║║123123123123123123123123║ +╚════════════════════════╝╚════════════════════════╝ +`, + }, + { + name: "not enough space should result in multiple rows (3 columns)", + args: args{ + boxes: []AsciiBox{ + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123ABABABABABAB123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123123123123123123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123ABABABABABAB123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123123123123123123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123ABABABABABAB123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123123123123123123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123ABABABABABAB123123║ +╚════════════════════════╝ +`, + ` +╔═sampleField════════════╗ +║ 123123123123 ║ +║123123123123123123123123║ +╚════════════════════════╝ +`, + }, + desiredWith: 78, + }, + want: ` ╔═sampleField════════════╗╔═sampleField════════════╗╔═sampleField════════════╗ ║ 123123123123 ║║ 123123123123 ║║ 123123123123 ║ ║123123ABABABABABAB123123║║123123123123123123123123║║123123ABABABABABAB123123║
