You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2024/09/05 12:53:43 UTC

(plc4x) 01/03: fix(plc4go/spi): asciibox tab distortion

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 2d93142fc6ef6b0eaebad72e285be9932de4607c
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Sep 5 14:44:21 2024 +0200

    fix(plc4go/spi): asciibox tab distortion
---
 plc4go/spi/utils/asciiBox.go      | 12 +++++++-----
 plc4go/spi/utils/asciiBox_test.go | 20 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/plc4go/spi/utils/asciiBox.go b/plc4go/spi/utils/asciiBox.go
index 66a43a75a1..b56a09fa87 100644
--- a/plc4go/spi/utils/asciiBox.go
+++ b/plc4go/spi/utils/asciiBox.go
@@ -20,13 +20,14 @@
 package utils
 
 import (
+	"github.com/rs/zerolog"
 	"math"
 	"regexp"
 	"strings"
-
-	"github.com/rs/zerolog/log"
 )
 
+var AsciiBoxLogger zerolog.Logger
+
 // AsciiBox is a string surrounded by an ascii border (and an optional name)
 type AsciiBox struct {
 	data             string
@@ -159,11 +160,12 @@ type asciiBoxWriter struct {
 }
 
 func (a *asciiBoxWriter) boxString(name string, data string, charWidth int) AsciiBox {
+	data = strings.ReplaceAll(data, "\t", "  ") // Tabs just don't work well as they distort the boxes so we convert them to a double space
 	rawBox := AsciiBox{data, a, a.compressBoxSet()}
 	longestLine := rawBox.Width()
 	if charWidth < longestLine {
 		if DebugAsciiBox {
-			log.Debug().Int("nChars", longestLine-charWidth).Msg("Overflow by nChars chars")
+			AsciiBoxLogger.Debug().Int("nChars", longestLine-charWidth).Msg("Overflow by nChars chars")
 		}
 		charWidth = longestLine + a.borderWidth + a.borderWidth
 	}
@@ -356,13 +358,13 @@ func (a *asciiBoxWriter) AlignBoxes(boxes []AsciiBox, desiredWidth int) AsciiBox
 		boxWidth := box.Width()
 		if boxWidth > actualWidth {
 			if DebugAsciiBox {
-				log.Debug().Int("nChars", boxWidth-desiredWidth).Msg("Overflow by nChars chars")
+				AsciiBoxLogger.Debug().Int("nChars", boxWidth-desiredWidth).Msg("Overflow by nChars chars")
 			}
 			actualWidth = boxWidth
 		}
 	}
 	if DebugAsciiBox {
-		log.Debug().Int("actualWidth", actualWidth).Msg("Working with actualWidth chars")
+		AsciiBoxLogger.Debug().Int("actualWidth", actualWidth).Msg("Working with actualWidth chars")
 	}
 	bigBox := AsciiBox{"", a, a.compressBoxSet()}
 	currentBoxRow := make([]AsciiBox, 0)
diff --git a/plc4go/spi/utils/asciiBox_test.go b/plc4go/spi/utils/asciiBox_test.go
index 5261b56261..17d4a990e2 100644
--- a/plc4go/spi/utils/asciiBox_test.go
+++ b/plc4go/spi/utils/asciiBox_test.go
@@ -297,6 +297,13 @@ func TestAsciiBox_Width(t *testing.T) {
 			},
 			want: 29,
 		},
+		{
+			name: "width tabs",
+			fields: fields{
+				data: "  \n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t      \t  \n\t",
+			},
+			want: 12,
+		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -565,6 +572,19 @@ func TestBoxString(t *testing.T) {
 ║          123123123123123123123123           ║
 ╚═════════════════════════════════════════════╝`[1:]),
 		},
+		{
+			name: "something with tabs and other stuff",
+			args: args{
+				data:      "a\n\tb\n\t\t\t\t\tc",
+				charWidth: 1,
+			},
+			want: asciiBoxForTest(`
+╔═══════════╗
+║     a     ║
+║      b    ║
+║          c║
+╚═══════════╝`[1:]),
+		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {