2022-07-14 16:33:38 +08:00
|
|
|
#!/usr/bin/env python3
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
# Teal Dulcet, CS546
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
import locale
|
2022-02-01 23:20:23 +08:00
|
|
|
import re
|
2023-05-11 22:03:53 +08:00
|
|
|
import shutil
|
|
|
|
import sys
|
2022-02-01 23:20:23 +08:00
|
|
|
import textwrap
|
2023-03-10 21:15:01 +08:00
|
|
|
from enum import IntEnum, auto
|
2023-05-11 22:03:53 +08:00
|
|
|
from typing import Any, Callable, List, Optional, Sequence
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
from wcwidth import wcswidth
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
locale.setlocale(locale.LC_ALL, "")
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
|
|
|
class style_types(IntEnum):
|
|
|
|
ASCII = 0
|
|
|
|
basic = auto()
|
|
|
|
light = auto()
|
|
|
|
heavy = auto()
|
|
|
|
double = auto()
|
2023-08-30 20:57:03 +08:00
|
|
|
arc = auto()
|
2023-03-10 21:15:01 +08:00
|
|
|
light_dashed = auto()
|
|
|
|
heavy_dashed = auto()
|
|
|
|
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
styles = [
|
|
|
|
["-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # ASCII
|
|
|
|
["—", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"], # Basic
|
|
|
|
["─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light
|
|
|
|
["━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"], # Heavy
|
|
|
|
["═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"], # Double
|
2023-08-30 20:57:03 +08:00
|
|
|
["─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"], # Light Arc
|
2023-03-13 22:29:43 +08:00
|
|
|
["╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"], # Light Dashed
|
|
|
|
["╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"] # Heavy Dashed
|
|
|
|
# [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] #No border
|
2022-02-01 23:20:23 +08:00
|
|
|
]
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
ansi = re.compile(r"\x1B\[(?:[0-9]+(?:;[0-9]+)*)?m")
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
def strcol(astr: str) -> int:
|
2022-02-01 23:20:23 +08:00
|
|
|
"""Returns the number of columns that the given string would take up if printed."""
|
2023-05-11 22:03:53 +08:00
|
|
|
astr = ansi.sub("", astr)
|
2023-03-10 21:15:01 +08:00
|
|
|
width = wcswidth(astr)
|
2022-02-01 23:20:23 +08:00
|
|
|
if width == -1:
|
2023-03-10 21:15:01 +08:00
|
|
|
print(
|
|
|
|
"\nError! wcswidth failed. Nonprintable wide character.",
|
|
|
|
file=sys.stderr)
|
2022-02-01 23:20:23 +08:00
|
|
|
sys.exit(1)
|
|
|
|
return width
|
2023-03-10 21:15:01 +08:00
|
|
|
# return len(astr)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False,
|
|
|
|
padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light, check: bool = True) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
"""Output char array as table."""
|
2022-02-01 23:20:23 +08:00
|
|
|
if not array:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
rows = len(array)
|
|
|
|
columns = len(array[0])
|
|
|
|
|
|
|
|
columnwidth = [max(strcol(i) for i in j) for j in zip(*array)]
|
|
|
|
|
|
|
|
width = sum(columnwidth)
|
|
|
|
|
|
|
|
w = shutil.get_terminal_size()
|
|
|
|
|
|
|
|
if tableborder or cellborder or headerrow or headercolumn:
|
2023-03-13 22:29:43 +08:00
|
|
|
width += (2 * padding + 1) * columns + (1 if tableborder else -1)
|
2022-02-01 23:20:23 +08:00
|
|
|
else:
|
2023-03-13 22:29:43 +08:00
|
|
|
width += 2 * padding * columns
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
if check:
|
|
|
|
if width > w.columns:
|
2023-05-11 22:03:53 +08:00
|
|
|
print(
|
|
|
|
f"The width of the table ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
|
2023-03-10 21:15:01 +08:00
|
|
|
return 1
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
if title:
|
2023-03-10 21:15:01 +08:00
|
|
|
print(textwrap.fill(title, width=width))
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
strm = ""
|
|
|
|
|
|
|
|
if tableborder:
|
|
|
|
strm += styles[style][2]
|
|
|
|
|
|
|
|
for j in range(columns):
|
2023-03-13 22:29:43 +08:00
|
|
|
strm += styles[style][0] * (2 * padding + columnwidth[j])
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
if j < columns - 1:
|
2023-03-10 21:15:01 +08:00
|
|
|
if cellborder or headerrow or (j == 0 and headercolumn):
|
|
|
|
strm += styles[style][3]
|
|
|
|
else:
|
|
|
|
strm += styles[style][0]
|
|
|
|
|
|
|
|
strm += styles[style][4] + "\n"
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
for i in range(rows):
|
2023-03-10 21:15:01 +08:00
|
|
|
if tableborder:
|
|
|
|
strm += styles[style][1]
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
for j in range(columns):
|
2023-03-10 21:15:01 +08:00
|
|
|
if (j > 0 and cellborder) or (i == 0 and j > 0 and headerrow) or (j == 1 and headercolumn):
|
2022-02-01 23:20:23 +08:00
|
|
|
strm += styles[style][1]
|
2023-03-10 21:15:01 +08:00
|
|
|
elif j > 0 and (tableborder or (i > 0 and headerrow) or headercolumn):
|
2022-02-01 23:20:23 +08:00
|
|
|
strm += " "
|
|
|
|
|
|
|
|
awidth = columnwidth[j] - (strcol(array[i][j]) - len(array[i][j]))
|
|
|
|
|
|
|
|
if (i == 0 and headerrow) or (j == 0 and headercolumn):
|
|
|
|
strm += " " * padding
|
|
|
|
|
|
|
|
strm += "\033[1m" + array[i][j].center(awidth) + "\033[22m"
|
|
|
|
|
|
|
|
strm += " " * padding
|
|
|
|
else:
|
|
|
|
strm += " " * padding
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
if alignment is None:
|
2023-05-11 22:03:53 +08:00
|
|
|
strm += f"{array[i][j]:{awidth}}"
|
2023-03-10 21:15:01 +08:00
|
|
|
elif alignment:
|
2022-02-01 23:20:23 +08:00
|
|
|
strm += array[i][j].rjust(awidth)
|
|
|
|
else:
|
|
|
|
strm += array[i][j].ljust(awidth)
|
|
|
|
|
|
|
|
strm += " " * padding
|
|
|
|
|
|
|
|
if tableborder:
|
|
|
|
strm += styles[style][1]
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
if i < rows - 1 or tableborder:
|
|
|
|
strm += "\n"
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
if (i < rows - 1 and cellborder) or (i == 0 and headerrow) or (i < rows - 1 and headercolumn):
|
|
|
|
if tableborder and (cellborder or (i == 0 and headerrow) or headercolumn):
|
2022-02-01 23:20:23 +08:00
|
|
|
strm += styles[style][5]
|
|
|
|
|
|
|
|
for j in range(columns):
|
2023-05-11 22:03:53 +08:00
|
|
|
if cellborder or (i == 0 and headerrow) or (j == 0 and headercolumn):
|
2023-03-13 22:29:43 +08:00
|
|
|
strm += styles[style][0] * (2 * padding + columnwidth[j])
|
2023-05-11 22:03:53 +08:00
|
|
|
elif headercolumn:
|
2023-03-13 22:29:43 +08:00
|
|
|
strm += " " * (2 * padding + columnwidth[j])
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
if j < columns - 1:
|
2023-05-11 22:03:53 +08:00
|
|
|
if cellborder or ((i == 0 and headerrow) and (j == 0 and headercolumn)):
|
2022-02-01 23:20:23 +08:00
|
|
|
strm += styles[style][6]
|
|
|
|
elif i == 0 and headerrow:
|
|
|
|
strm += styles[style][9]
|
2023-05-11 22:03:53 +08:00
|
|
|
elif headercolumn:
|
2022-02-01 23:20:23 +08:00
|
|
|
if j == 0:
|
|
|
|
strm += styles[style][7]
|
|
|
|
else:
|
|
|
|
strm += " "
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
if tableborder:
|
2023-05-11 22:03:53 +08:00
|
|
|
if cellborder or (i == 0 and headerrow):
|
2023-03-10 21:15:01 +08:00
|
|
|
strm += styles[style][7]
|
|
|
|
elif headercolumn:
|
|
|
|
strm += styles[style][1]
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
strm += "\n"
|
|
|
|
|
|
|
|
if tableborder:
|
|
|
|
strm += styles[style][8]
|
|
|
|
|
|
|
|
for j in range(columns):
|
|
|
|
strm += styles[style][0] * (2 * padding + columnwidth[j])
|
|
|
|
|
|
|
|
if j < columns - 1:
|
|
|
|
if cellborder or (j == 0 and headercolumn):
|
|
|
|
strm += styles[style][9]
|
|
|
|
else:
|
|
|
|
strm += styles[style][0]
|
|
|
|
|
|
|
|
strm += styles[style][10]
|
2023-03-10 21:15:01 +08:00
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
print(strm)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
def array(aarray: Sequence[Sequence[Any]], aheaderrow: Optional[Sequence[Any]] = None, aheadercolumn: Optional[Sequence[Any]] = None, headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
"""Convert array to char array and output as table."""
|
2022-02-01 23:20:23 +08:00
|
|
|
if not aarray:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
rows = len(aarray)
|
|
|
|
columns = len(aarray[0])
|
|
|
|
|
|
|
|
if not all(len(x) == columns for x in aarray):
|
2023-03-10 21:15:01 +08:00
|
|
|
print(
|
|
|
|
"Error: The rows of the array must have the same number of columns.",
|
|
|
|
file=sys.stderr)
|
2022-02-01 23:20:23 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
if aheaderrow:
|
|
|
|
rows += 1
|
|
|
|
|
|
|
|
if aheadercolumn:
|
|
|
|
columns += 1
|
|
|
|
|
|
|
|
if aheaderrow and len(aheaderrow) != columns:
|
2023-03-10 21:15:01 +08:00
|
|
|
print(
|
|
|
|
"Error: The header row must have the same number of columns as the array.",
|
|
|
|
file=sys.stderr)
|
2022-02-01 23:20:23 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
if aheadercolumn and len(aheadercolumn) != (rows - 1 if aheaderrow else rows):
|
2023-03-10 21:15:01 +08:00
|
|
|
print(
|
|
|
|
"Error: The header column must have the same number of rows as the array.",
|
|
|
|
file=sys.stderr)
|
2022-02-01 23:20:23 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
aaarray = [["" for j in range(columns)] for i in range(rows)]
|
|
|
|
|
|
|
|
if aheaderrow:
|
2023-03-10 21:15:01 +08:00
|
|
|
aaarray[0] = aheaderrow[:columns]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
for i in range(1 if aheaderrow else 0, rows):
|
|
|
|
ii = i - 1 if aheaderrow else i
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
if aheadercolumn:
|
2023-03-10 21:15:01 +08:00
|
|
|
aaarray[i][0] = aheadercolumn[ii]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
j = 1 if aheadercolumn else 0
|
|
|
|
aaarray[i][j:] = map(str, aarray[ii][:columns - j])
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
return table(aaarray, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
def functions(xmin: float, xmax: float, xstep: float, afunctions: Sequence[Callable[[float], float]], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
"""Convert one or more functions to array and output as table."""
|
2022-07-14 16:33:38 +08:00
|
|
|
if not afunctions:
|
2022-02-01 23:20:23 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
if xmin >= xmax:
|
|
|
|
print("xmin must be less than xmax.", file=sys.stderr)
|
|
|
|
return 1
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
if xstep <= 0:
|
|
|
|
print("xstep must be greater than zero.", file=sys.stderr)
|
2022-02-01 23:20:23 +08:00
|
|
|
return 1
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
rows = int((xmax - xmin) / xstep) + 1
|
2022-02-01 23:20:23 +08:00
|
|
|
columns = len(afunctions) + 1
|
|
|
|
|
|
|
|
aaheaderrow = ["x", "y"]
|
|
|
|
length = len(aaheaderrow)
|
|
|
|
|
|
|
|
aheaderrow = [""] * columns
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
if len(afunctions) == 1:
|
|
|
|
aheaderrow = aaheaderrow
|
|
|
|
else:
|
|
|
|
aheaderrow = aaheaderrow[:-1] + [aaheaderrow[-1] +
|
|
|
|
str(j - length + 2) for j in range(1, columns)]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
aarray = [[0 for j in range(columns)] for i in range(rows)]
|
|
|
|
|
|
|
|
for i in range(rows):
|
2023-08-30 20:57:03 +08:00
|
|
|
temp = aarray[i][0] = i * xstep + xmin
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
aarray[i][1:] = [function(temp) for function in afunctions]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
return array(aarray, aheaderrow, None, headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
def function(xmin: float, xmax: float, xstep: float, afunction: Callable[[float], float], headerrow: bool = False, headercolumn: bool = False, tableborder: bool = True, cellborder: bool = False, padding: int = 1, alignment: Optional[bool] = None, title: Optional[str] = None, style: style_types = style_types.light) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
"""Convert single function to array and output as table."""
|
2023-08-30 20:57:03 +08:00
|
|
|
return functions(xmin, xmax, xstep, [afunction], headerrow, headercolumn, tableborder, cellborder, padding, alignment, title, style)
|