Last active
October 28, 2022 20:43
-
-
Save JoshuaVandaele/d2fff269fd1c2643eb88e1e4f348002f to your computer and use it in GitHub Desktop.
Simple tool allowing me to print pretty boxes to the CLI
This file contains hidden or 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
| from __future__ import annotations | |
| class BoxCli(): | |
| def __init__(self: BoxCli, title: str = "", width: int = 0, lines: list[str] = [], _parent_width: int = 0) -> None: | |
| self.__width: int = (width if width > len(title) else len(title))+4 | |
| self.title: str = title | |
| self.lines: list[str] = lines | |
| self._parent_width: int = _parent_width | |
| self._child: BoxCli | None = None | |
| def get_heading(self: BoxCli) -> str: | |
| heading = f" {self.title} ".center(self.width, "-") | |
| heading = "+" + heading[1:-1] + "+" | |
| if self._parent_width < self.width: | |
| heading = heading[:self._parent_width] + "+" + heading[self._parent_width+1:] | |
| return heading | |
| def add_line(self: BoxCli, line: str) -> None: | |
| self.lines.append(line) | |
| def add_lines(self: BoxCli, lines: list[str]) -> None: | |
| self.lines += lines | |
| def get_line(self: BoxCli, i) -> str: | |
| line: str = "| " + self.lines[i] | |
| line = line.ljust(self.width-2) | |
| line += " |" | |
| return line | |
| def get_footing(self: BoxCli) -> str: | |
| footing: str = "-"*self.width | |
| footing = "+" + footing[1:-1] + "+" | |
| return footing | |
| def print_heading(self: BoxCli) -> None: | |
| print(self.get_heading()) | |
| def print_line(self: BoxCli, i) -> None: | |
| print(self.get_line(i)) | |
| def print_lines(self: BoxCli) -> None: | |
| for index in range(len(self.lines)): | |
| self.print_line(index) | |
| def print_footing(self: BoxCli) -> None: | |
| print(self.get_footing(), end="\r") | |
| def print(self: BoxCli, with_children: bool = True) -> None: | |
| self.print_heading() | |
| self.print_lines() | |
| self.print_footing() | |
| if with_children and self._child: | |
| return self._child.print() | |
| def set_child(self: BoxCli, title: str = "", width: int = 0, lines: list[str] = []) -> BoxCli: | |
| self._child = BoxCli(title=title, width=width, | |
| lines=lines, _parent_width=self.width-1) | |
| return self._child | |
| @property | |
| def width(self: BoxCli) -> int: | |
| return self.__width if self.__width > (new_width := len(max(self.lines, key=len, default=[0]))+4) else new_width | |
| box: BoxCli = BoxCli("Hello, World!", lines=[ | |
| "This is a test thing", | |
| "Really, I just wanna see how this looks" | |
| ]) | |
| child_box: BoxCli = box.set_child("Also this is a child!", lines=[ | |
| "I think this is pretty cool (If I do say so myself)", | |
| "And I am always objectively right." | |
| ]) | |
| aborted_child_box: BoxCli = child_box.set_child("Your box is: Damn box", lines=[ | |
| "Get it? It's the Uncanny joke (I don't get it)" | |
| ]) | |
| box.print(with_children=True) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: