Skip to content

Instantly share code, notes, and snippets.

@JoshuaVandaele
Last active October 28, 2022 20:43
Show Gist options
  • Select an option

  • Save JoshuaVandaele/d2fff269fd1c2643eb88e1e4f348002f to your computer and use it in GitHub Desktop.

Select an option

Save JoshuaVandaele/d2fff269fd1c2643eb88e1e4f348002f to your computer and use it in GitHub Desktop.
Simple tool allowing me to print pretty boxes to the CLI
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)
@JoshuaVandaele

Copy link
Copy Markdown
Author

Output:

+------------- Hello, World! -------------+
| This is a test thing                    |
| Really, I just wanna see how this looks |
+--------------- Also this is a child! ---+-----------+
| I think this is pretty cool (If I do say so myself) |
| And I am always objectively right.                  |
+------------ Your box is: Damn box -------------+----+
| Get it? It's the Uncanny joke (I don't get it) |
+------------------------------------------------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment