Updated to require C++17 and use exceptions.

This commit is contained in:
Teal Dulcet
2024-11-13 09:31:42 -08:00
parent 370dde9025
commit 8fa05e23c8
10 changed files with 182 additions and 273 deletions

View File

@@ -173,10 +173,8 @@ def strcol(astr: str) -> int:
"""Returns the number of columns that the given string would take up if printed."""
width = wcswidth(astr)
if width == -1:
print(
"\nError! wcswidth failed. Nonprintable wide character.",
file=sys.stderr)
sys.exit(1)
msg = "wcswidth failed. Nonprintable wide character."
raise ValueError(msg)
return width
# return len(astr)
@@ -305,10 +303,10 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
if not array:
return 1
if height == 0:
if not height:
return 1
if width == 0:
if not width:
return 1
w = shutil.get_terminal_size()
@@ -395,7 +393,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
strm += astyle[6]
output = True
elif axaxis:
if i == 0:
if not i:
strm += astyle[4]
output = True
elif i >= height - ai:
@@ -414,7 +412,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
strm += astyle[1]
output = True
elif ayaxis:
if j == 0:
if not j:
strm += astyle[2]
output = True
elif j >= width - aj:
@@ -435,7 +433,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
elif yaxislabel and xaxislabel and axistick and axisunitslabel and ymin <= 0 <= ymax and xmin <= 0 <= xmax:
strm += "0"
output = True
elif (j >= width - aj if xaxis <= width - aj else j == 0) and yaxislabel and axislabel:
elif (j >= width - aj if xaxis <= width - aj else not j) and yaxislabel and axislabel:
strm += "x"
output = True
elif yaxislabel and axistick and axisunitslabel:
@@ -470,7 +468,7 @@ def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
output = True
else:
j += aj
elif (i == 0 if yaxis >= ai else i >= height - ai) and xaxislabel and axislabel:
elif (not i if yaxis >= ai else i >= height - ai) and xaxislabel and axislabel:
strm += "y"
output = True
elif ylabellength and (xaxislabel if xaxis < aj else j < xaxis - ylabellength and j + aj >= xaxis - ylabellength) and (yaxis >= ai or i < height - ai) and axistick and axisunitslabel:
@@ -535,10 +533,10 @@ def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
w = shutil.get_terminal_size()
if height == 0:
if not height:
height = w.lines * 4
if width == 0:
if not width:
width = w.columns * 2
if check:
@@ -558,7 +556,7 @@ def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
height *= 2
width //= 2
if xmin == xmax == 0:
if not xmin and not xmax:
xmin = min(aarray)
xmax = max(aarray)
@@ -575,7 +573,7 @@ def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
index = int((x - xmin) / xstep)
histogram[index] += 1
if ymin == ymax == 0:
if not ymin and not ymax:
ymin = min(histogram)
ymax = max(histogram)
@@ -610,10 +608,10 @@ def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
w = shutil.get_terminal_size()
if height == 0:
if not height:
height = w.lines * 4
if width == 0:
if not width:
width = w.columns * 2
if check:
@@ -633,11 +631,11 @@ def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax:
if atype == type_types.block:
height //= 2
if xmin == xmax == 0:
if not xmin and not xmax:
xmin = min(x for aarray in aarrays for x, y in aarray)
xmax = max(x for aarray in aarrays for x, y in aarray)
if ymin == ymax == 0:
if not ymin and not ymax:
ymin = min(y for aarray in aarrays for x, y in aarray)
ymax = max(y for aarray in aarrays for x, y in aarray)
@@ -690,10 +688,10 @@ def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ym
w = shutil.get_terminal_size()
if height == 0:
if not height:
height = w.lines * 4
if width == 0:
if not width:
width = w.columns * 2
if check:

View File

@@ -59,10 +59,8 @@ def strcol(astr: str) -> int:
astr = ansi.sub("", astr)
width = wcswidth(astr)
if width == -1:
print(
"\nError! wcswidth failed. Nonprintable wide character.",
file=sys.stderr)
sys.exit(1)
msg = "wcswidth failed. Nonprintable wide character."
raise ValueError(msg)
return width
# return len(astr)
@@ -106,7 +104,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
strm += astyle[0] * (2 * padding + columnwidth[j])
if j < columns - 1:
if cellborder or headerrow or (j == 0 and headercolumn):
if cellborder or headerrow or (not j and headercolumn):
strm += astyle[3]
else:
strm += astyle[0]
@@ -118,14 +116,14 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
strm += astyle[1]
for j in range(columns):
if (j > 0 and cellborder) or (i == 0 and j > 0 and headerrow) or (j == 1 and headercolumn):
if (j > 0 and cellborder) or (not i and j > 0 and headerrow) or (j == 1 and headercolumn):
strm += astyle[1]
elif j > 0 and (tableborder or (i > 0 and headerrow) or headercolumn):
strm += " "
awidth = columnwidth[j] - (strcol(array[i][j]) - len(array[i][j]))
if (i == 0 and headerrow) or (j == 0 and headercolumn):
if (not i and headerrow) or (not j and headercolumn):
strm += (" " * padding) + "\033[1m" + array[i][j].center(awidth) + "\033[22m" + (" " * padding)
else:
strm += (" " * padding) + (f"{array[i][j]:{awidth}}" if alignment is None else array[i][j].rjust(awidth) if alignment else array[i][j].ljust(awidth)) + (" " * padding)
@@ -136,29 +134,29 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
if i < rows - 1 or tableborder:
strm += "\n"
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):
if (i < rows - 1 and cellborder) or (not i and headerrow) or (i < rows - 1 and headercolumn):
if tableborder and (cellborder or (not i and headerrow) or headercolumn):
strm += astyle[5]
for j in range(columns):
if cellborder or (i == 0 and headerrow) or (j == 0 and headercolumn):
if cellborder or (not i and headerrow) or (not j and headercolumn):
strm += astyle[0] * (2 * padding + columnwidth[j])
elif headercolumn:
strm += " " * (2 * padding + columnwidth[j])
if j < columns - 1:
if cellborder or ((i == 0 and headerrow) and (j == 0 and headercolumn)):
if cellborder or ((not i and headerrow) and (not j and headercolumn)):
strm += astyle[6]
elif i == 0 and headerrow:
elif not i and headerrow:
strm += astyle[9]
elif headercolumn:
if j == 0:
if not j:
strm += astyle[7]
else:
strm += " "
if tableborder:
if cellborder or (i == 0 and headerrow):
if cellborder or (not i and headerrow):
strm += astyle[7]
elif headercolumn:
strm += astyle[1]
@@ -172,7 +170,7 @@ def table(array: List[List[str]], headerrow: bool = False, headercolumn: bool =
strm += astyle[0] * (2 * padding + columnwidth[j])
if j < columns - 1:
if cellborder or (j == 0 and headercolumn):
if cellborder or (not j and headercolumn):
strm += astyle[9]
else:
strm += astyle[0]

View File

@@ -5,6 +5,7 @@
# Run: python3 -OO test.py
import math
import operator
import random
import sys
@@ -87,7 +88,7 @@ print("\nOutput sorted array as table\n")
array = ([random.randint(0, sys.maxsize)
for j in range(columns)] for i in range(rows))
sortdimension = 0
array = sorted(array, key=lambda x: x[sortdimension])
array = sorted(array, key=operator.itemgetter(sortdimension))
for style in tables.style_types:
tables.array(array, None, None, style=style)