Repository: TheUncleKai/bbutils

Issue:
Fix appname bug in logging
Fix appname bug in logging, it uses the same width as the tags. Thats stupid.

Comments:

Patch:
diff --git a/bbutil/__init__.py b/bbutil/__init__.py
index d289f07..f758209 100644
--- a/bbutil/__init__.py
+++ b/bbutil/__init__.py
@@ -59,7 +59,7 @@ __major__ = 4
 __minor__ = 0
 
 #: version patch
-__patch__ = 5
+__patch__ = 6
 
 #: package version
 __version__ = "{0:d}.{1:d}.{2:d}.{3:d}".format(__milestone__, __major__, __minor__, __patch__)
diff --git a/bbutil/logging/__init__.py b/bbutil/logging/__init__.py
index c5e9dbe..f231310 100755
--- a/bbutil/logging/__init__.py
+++ b/bbutil/logging/__init__.py
@@ -37,7 +37,7 @@ __all__ = [
 _index = {
     0: ["INFORM", "WARN", "ERROR", "EXCEPTION", "TIMER", "PROGRESS"],
     1: ["INFORM", "DEBUG1", "WARN", "ERROR", "EXCEPTION", "TIMER", "PROGRESS"],
-    2: ["INFORM", "DEBUG1", "DEBUG1", "WARN", "ERROR", "EXCEPTION", "TIMER", "PROGRESS"],
+    2: ["INFORM", "DEBUG1", "DEBUG2", "WARN", "ERROR", "EXCEPTION", "TIMER", "PROGRESS"],
     3: ["INFORM", "DEBUG1", "DEBUG2", "DEBUG3", "WARN", "ERROR", "EXCEPTION", "TIMER", "PROGRESS"]
 }
 
diff --git a/bbutil/logging/writer/console.py b/bbutil/logging/writer/console.py
index b769bf7..bdc2386 100755
--- a/bbutil/logging/writer/console.py
+++ b/bbutil/logging/writer/console.py
@@ -84,6 +84,7 @@ class ConsoleWriter(Writer):
 
         self.styles: Dict[str, _Style] = _schemes
         self.encoding: str = ""
+        self.app_space: int = 0
         self.text_space: int = 15
         self.seperator: str = "|"
         self.length: int = 0
@@ -103,6 +104,10 @@ class ConsoleWriter(Writer):
         if item is not None:
             self.text_space = item
 
+        item = kwargs.get("app_space", None)
+        if item is not None:
+            self.app_space = item
+
         item = kwargs.get("seperator", None)
         if item is not None:
             self.seperator = item
@@ -192,7 +197,10 @@ class ConsoleWriter(Writer):
         return
 
     def _create_color(self, item: Message, text: str) -> str:
-        appname = "{0:s} ".format(item.app).ljust(self.text_space)
+        _app_space = self.app_space
+        if self.app_space == 0:
+            _app_space = len(item.app) + 5
+        appname = "{0:s} ".format(item.app).ljust(_app_space)
         scheme = self.styles[item.level].scheme
 
         if item.tag == "":


Test patch:
diff --git a/tests.json b/tests.json
index efb2357..5e650f7 100644
--- a/tests.json
+++ b/tests.json
@@ -82,6 +82,10 @@
                 "test_write_04",
                 "test_write_05",
                 "test_write_06",
+                "test_write_07",
+                "test_write_08",
+                "test_write_09",
+                "test_write_10",
                 "test_clear_01",
                 "test_clear_02"
             ]
diff --git a/tests/logging/console.py b/tests/logging/console.py
index e7ce01a..505e772 100755
--- a/tests/logging/console.py
+++ b/tests/logging/console.py
@@ -21,10 +21,15 @@ import sys
 import unittest
 import unittest.mock as mock
 
-from bbutil.logging.writer.console import ConsoleWriter
+import colorama
+
+from bbutil.logging.writer.console import ConsoleWriter, _Style
 from bbutil.logging.types import Message, Progress, Writer
 
 
+RESET_ALL = colorama.Style.RESET_ALL
+
+
 class Callback(object):
 
     def __init__(self, writer: Writer):
@@ -241,6 +246,157 @@ class TestConsoleWriter(unittest.TestCase):
         self.assertFalse(write_called)
         return
 
+    def test_write_07(self):
+        message = Message(app="TEST", level="INFORM", tag="TEST", content="This is a test!")
+
+        _style = _Style("INFORM", "BRIGHT", "GREEN", "")
+
+        item = ConsoleWriter()
+        item.open()
+        item.stdout = SysWrite()
+
+        item.write(message)
+
+        write_called = item.stdout.write.called
+        call = item.stdout.write.call_args_list[0]
+        (args, kwargs) = call
+        data = args[0]
+
+        print(data)
+
+        _tag = "TEST".ljust(15)
+        _app_space = len("TEST") + 5
+        _app = "{0:s} ".format("TEST").ljust(_app_space)
+
+        content = "{0:s}{1:s}{2:s} {3:s}{4:s} {5:s}{6:s}\n".format(RESET_ALL,
+                                                                   _app,
+                                                                   _style.scheme,
+                                                                   _tag,
+                                                                   "|",
+                                                                   RESET_ALL,
+                                                                   "This is a test!")
+
+        self.assertTrue(write_called)
+        self.assertIn(message.app, data)
+        self.assertIn(message.tag, data)
+        self.assertIn(message.content, data)
+        self.assertEqual(content, data)
+        return
+
+    def test_write_08(self):
+        message = Message(app="TEST", level="INFORM", tag="TEST", content="This is a test!")
+
+        _style = _Style("INFORM", "BRIGHT", "GREEN", "")
+
+        item = ConsoleWriter()
+        item.setup(app_space=15)
+        item.open()
+        item.stdout = SysWrite()
+
+        item.write(message)
+
+        write_called = item.stdout.write.called
+        call = item.stdout.write.call_args_list[0]
+        (args, kwargs) = call
+        data = args[0]
+
+        print(data)
+
+        _tag = "TEST".ljust(15)
+        _app_space = len("TEST") + 5
+        _app = "{0:s} ".format("TEST").ljust(15)
+
+        content = "{0:s}{1:s}{2:s} {3:s}{4:s} {5:s}{6:s}\n".format(RESET_ALL,
+                                                                   _app,
+                                                                   _style.scheme,
+                                                                   _tag,
+                                                                   "|",
+                                                                   RESET_ALL,
+                                                                   "This is a test!")
+
+        self.assertTrue(write_called)
+        self.assertIn(message.app, data)
+        self.assertIn(message.tag, data)
+        self.assertIn(message.content, data)
+        self.assertEqual(content, data)
+        return
+
+    def test_write_09(self):
+        message = Message(app="TEST", level="INFORM", tag="TEST", content="This is a test!")
+
+        _style = _Style("INFORM", "BRIGHT", "GREEN", "")
+
+        item = ConsoleWriter()
+        item.setup(app_space=10)
+        item.open()
+        item.stdout = SysWrite()
+
+        item.write(message)
+
+        write_called = item.stdout.write.called
+        call = item.stdout.write.call_args_list[0]
+        (args, kwargs) = call
+        data = args[0]
+
+        print(data)
+
+        _tag = "TEST".ljust(15)
+        _app_space = len("TEST") + 5
+        _app = "{0:s} ".format("TEST").ljust(15)
+
+        content = "{0:s}{1:s}{2:s} {3:s}{4:s} {5:s}{6:s}\n".format(RESET_ALL,
+                                                                   _app,
+                                                                   _style.scheme,
+                                                                   _tag,
+                                                                   "|",
+                                                                   RESET_ALL,
+                                                                   "This is a test!")
+
+        self.assertTrue(write_called)
+        self.assertIn(message.app, data)
+        self.assertIn(message.tag, data)
+        self.assertIn(message.content, data)
+        self.assertNotEqual(content, data)
+        return
+
+    def test_write_10(self):
+        message = Message(app="TEST", level="INFORM", tag="TEST", content="This is a test!")
+
+        _style = _Style("INFORM", "BRIGHT", "GREEN", "")
+
+        item = ConsoleWriter()
+        item.setup(text_space=10)
+        item.open()
+        item.stdout = SysWrite()
+
+        item.write(message)
+
+        write_called = item.stdout.write.called
+        call = item.stdout.write.call_args_list[0]
+        (args, kwargs) = call
+        data = args[0]
+
+        print(data)
+
+        _tag = "TEST".ljust(10)
+        _app_space = len("TEST") + 5
+        _app = "{0:s} ".format("TEST").ljust(_app_space)
+
+        content = "{0:s}{1:s}{2:s} {3:s}{4:s} {5:s}{6:s}\n".format(RESET_ALL,
+                                                                   _app,
+                                                                   _style.scheme,
+                                                                   _tag,
+                                                                   "|",
+                                                                   RESET_ALL,
+                                                                   "This is a test!")
+
+        self.assertTrue(write_called)
+        self.assertIn(message.app, data)
+        self.assertIn(message.tag, data)
+        self.assertIn(message.content, data)
+        self.assertEqual(content, data)
+        return
+
     def test_clear_01(self):
 
         message = Message(app="TEST", content="This is a test!", raw=True)