From 0999f4a039f7c978b4b2c872a762a983d069323d Mon Sep 17 00:00:00 2001 From: warmachine028 Date: Thu, 3 Aug 2023 22:05:38 +0530 Subject: [PATCH] add: added common tree-str functions in Utils/Commons.py --- datastax/Utils/Commons.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/datastax/Utils/Commons.py b/datastax/Utils/Commons.py index fde67c1..8a4a32e 100644 --- a/datastax/Utils/Commons.py +++ b/datastax/Utils/Commons.py @@ -1,4 +1,7 @@ -from typing import Any +from typing import Any, Optional +import math +from datastax.Utils.ColorCodes import ColorCodes +from datastax.Utils.Colors import Colors class Commons: @@ -7,3 +10,28 @@ def repr(item: Any) -> str: if "\n" in str(item): return f"{str(type(item))[8:-2].split('.')[-1]}@{id(item)}" return str(item) + + @staticmethod + def node_builder(data: Optional[str], piece_width: int) -> str: + value = data or '' + gap1 = int(math.ceil(piece_width / 2 - len(value) / 2)) + gap2 = int(math.floor(piece_width / 2 - len(value) / 2)) + return f"{' ' * gap1}{value}{' ' * gap2}" + + @staticmethod + def format(color, data): + fore, back = Colors.FORE, Colors.BACK + red, black, grey = Colors.RED, Colors.BLACK, Colors.GREY + if color is ColorCodes.BLACK: + return f"{fore}{red}{back}{black} {data} {back}{grey}" + return f"{fore}{black}{back}{red} {data} {back}{grey}" + + @staticmethod + def redblack_node_builder(data: Optional[str], + piece_width: int, n: int = 0) -> str: + value: str = data or '' + n = n or len(value) - 33 if value else 0 + + gap1 = int(math.ceil(piece_width / 2 - n / 2)) + gap2 = int(math.floor(piece_width / 2 - n / 2)) + return f"{' ' * gap1}{value}{' ' * gap2}"