#!/bin/python3 from abc import ABC class ShellFormatter(ABC): def __init__(self, columnInfo): pass def header(self, header): pass def row(self, row): pass def end(self): pass class ShellFormatterBasic(ShellFormatter): def __init__(self, columnInfo): print("----------------------") def header(self, header): print("|", "|".join(header), "|") print("----------------------") def row(self, row): print("|", "|".join(row), "|") def end(self): print("----------------------") class ShellFormatterClassic(ShellFormatter): def __init__(self, columnInfo): self.fmtrow = "" self.colinfo = columnInfo for col in columnInfo: self.fmtrow += ' {:' + str(columnInfo[col]["length"]) + 's} |' self.line() def line(self): print("+", end='') for col in self.colinfo: print("-" * (self.colinfo[col]["length"]+2), end='+') print() def header(self, header): print("|", end="") print(self.fmtrow.format(*header)) self.line() def row(self, row): print("|", end="") print(self.fmtrow.format(*row)) def end(self): self.line() class ShellFormatterUnicode(ShellFormatter): def __init__(self, columnInfo): self.fmtrow = "" self.fmthdr = "" self.colinfo = columnInfo for col in columnInfo: self.fmtrow += ' {:' + str(columnInfo[col]["length"]) + 's} │' self.fmthdr += ' \033[1m{:' + str(columnInfo[col]["length"]) + 's}\033[0m │' self.line('╭','┬','╮') def line(self, s, m, e): buf = s for col in self.colinfo: buf += "─" * (self.colinfo[col]["length"]+2) buf += m buf = buf[:len(buf)-1] buf += e print(buf) def header(self, header): print("│", end="") print(self.fmthdr.format(*header)) self.line('├','┼','┤') def row(self, row): print("│", end="") print(self.fmtrow.format(*row)) def end(self): self.line('╰','┴','╯') colinfo = { "foo": { "length": 4, "type": str, }, "bar": { "length": 5, "type": str, }, "baz": { "length": 4, "type": str, }, } header = {"foo", "bar", "baz"} rows = [ {"foo1", "bar1", "baz1"}, {"foo2", "bar2", "baz2"}, ] fmt = ShellFormatterBasic(colinfo) fmt.header(header) for row in rows: fmt.row(row) fmt.end() fmt = ShellFormatterClassic(colinfo) fmt.header(header) for row in rows: fmt.row(row) fmt.end() fmt = ShellFormatterUnicode(colinfo) fmt.header(header) for row in rows: fmt.row(row) fmt.end()