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 math
|
|
|
|
|
import shutil
|
2023-05-11 22:03:53 +08:00
|
|
|
|
import sys
|
2022-02-01 23:20:23 +08:00
|
|
|
|
import textwrap
|
2023-05-11 22:03:53 +08:00
|
|
|
|
from datetime import datetime, timezone
|
2023-03-10 21:15:01 +08:00
|
|
|
|
from enum import Enum, IntEnum, auto
|
2023-05-11 22:03:53 +08:00
|
|
|
|
from fractions import Fraction
|
2024-11-23 02:49:10 +08:00
|
|
|
|
from typing import Callable, List, Optional, Sequence, TextIO, Tuple
|
2023-05-11 22:03:53 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
if sys.platform != "win32":
|
|
|
|
|
import ctypes
|
|
|
|
|
from ctypes.util import find_library
|
|
|
|
|
|
|
|
|
|
libc = ctypes.CDLL(find_library("c"))
|
|
|
|
|
libc.wcwidth.argtypes = (ctypes.c_wchar,)
|
|
|
|
|
libc.wcwidth.restype = ctypes.c_int
|
|
|
|
|
libc.wcswidth.argtypes = (ctypes.c_wchar_p, ctypes.c_int)
|
|
|
|
|
libc.wcswidth.restype = ctypes.c_int
|
|
|
|
|
|
|
|
|
|
def wcswidth(astr: str) -> int:
|
|
|
|
|
return libc.wcswidth(astr, len(astr))
|
|
|
|
|
else:
|
|
|
|
|
from wcwidth import wcswidth
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
styles = (
|
|
|
|
|
("-", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"), # ASCII
|
|
|
|
|
("—", "|", "+", "+", "+", "+", "+", "+", "+", "+", "+"), # Basic
|
|
|
|
|
("─", "│", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"), # Light
|
|
|
|
|
("━", "┃", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛"), # Heavy
|
|
|
|
|
("═", "║", "╔", "╦", "╗", "╠", "╬", "╣", "╚", "╩", "╝"), # Double
|
|
|
|
|
("─", "│", "╭", "┬", "╮", "├", "┼", "┤", "╰", "┴", "╯"), # Light Arc
|
|
|
|
|
("╌", "┊", "┌", "┬", "┐", "├", "┼", "┤", "└", "┴", "┘"), # Light Dashed
|
|
|
|
|
("╍", "┋", "┏", "┳", "┓", "┣", "╋", "┫", "┗", "┻", "┛") # Heavy Dashed
|
|
|
|
|
# (" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ")) #No border
|
|
|
|
|
)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
class color_types(IntEnum):
|
|
|
|
|
default = 0
|
|
|
|
|
black = auto()
|
|
|
|
|
red = auto()
|
|
|
|
|
green = auto()
|
|
|
|
|
yellow = auto()
|
|
|
|
|
blue = auto()
|
2023-03-13 22:29:43 +08:00
|
|
|
|
magenta = auto()
|
2023-03-10 21:15:01 +08:00
|
|
|
|
cyan = auto()
|
|
|
|
|
white = auto()
|
2024-11-12 21:03:10 +08:00
|
|
|
|
gray = auto()
|
|
|
|
|
bright_red = auto()
|
|
|
|
|
bright_green = auto()
|
|
|
|
|
bright_yellow = auto()
|
|
|
|
|
bright_blue = auto()
|
|
|
|
|
bright_magenta = auto()
|
|
|
|
|
bright_cyan = auto()
|
|
|
|
|
bright_white = auto()
|
2023-03-10 21:15:01 +08:00
|
|
|
|
|
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
colors = (39, 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
dots = (
|
2023-03-10 21:15:01 +08:00
|
|
|
|
"⠀", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎",
|
|
|
|
|
"⠏", "⠐", "⠑", "⠒", "⠓", "⠔", "⠕", "⠖", "⠗", "⠘", "⠙", "⠚", "⠛", "⠜", "⠝",
|
|
|
|
|
"⠞", "⠟", "⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⠨", "⠩", "⠪", "⠫", "⠬",
|
|
|
|
|
"⠭", "⠮", "⠯", "⠰", "⠱", "⠲", "⠳", "⠴", "⠵", "⠶", "⠷", "⠸", "⠹", "⠺", "⠻",
|
|
|
|
|
"⠼", "⠽", "⠾", "⠿", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", "⡈", "⡉", "⡊",
|
|
|
|
|
"⡋", "⡌", "⡍", "⡎", "⡏", "⡐", "⡑", "⡒", "⡓", "⡔", "⡕", "⡖", "⡗", "⡘", "⡙",
|
|
|
|
|
"⡚", "⡛", "⡜", "⡝", "⡞", "⡟", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", "⡨",
|
|
|
|
|
"⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", "⡰", "⡱", "⡲", "⡳", "⡴", "⡵", "⡶", "⡷",
|
|
|
|
|
"⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿", "⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆",
|
|
|
|
|
"⢇", "⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⢐", "⢑", "⢒", "⢓", "⢔", "⢕",
|
|
|
|
|
"⢖", "⢗", "⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⢠", "⢡", "⢢", "⢣", "⢤",
|
|
|
|
|
"⢥", "⢦", "⢧", "⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⢰", "⢱", "⢲", "⢳",
|
|
|
|
|
"⢴", "⢵", "⢶", "⢷", "⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣀", "⣁", "⣂",
|
|
|
|
|
"⣃", "⣄", "⣅", "⣆", "⣇", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", "⣐", "⣑",
|
|
|
|
|
"⣒", "⣓", "⣔", "⣕", "⣖", "⣗", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", "⣠",
|
|
|
|
|
"⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯",
|
|
|
|
|
"⣰", "⣱", "⣲", "⣳", "⣴", "⣵", "⣶", "⣷", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾",
|
2024-06-01 23:59:28 +08:00
|
|
|
|
"⣿")
|
|
|
|
|
dotvalues = ((0x1, 0x2, 0x4, 0x40), (0x8, 0x10, 0x20, 0x80))
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
blocks = ("\xA0", "█")
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
blocks_quadrant = ("\xA0", "▘", "▝", "▀", "▖", "▌", "▞", "▛", "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█")
|
|
|
|
|
|
|
|
|
|
separated_blocks_quadrant = ("\xA0", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "")
|
|
|
|
|
|
|
|
|
|
blocks_sextant = ("\xA0", "🬀", "🬁", "🬂", "🬃", "🬄", "🬅", "🬆", "🬇", "🬈", "🬉", "🬊", "🬋", "🬌", "🬍", "🬎", "🬏", "🬐", "🬑", "🬒", "🬓", "▌", "🬔", "🬕", "🬖", "🬗", "🬘", "🬙", "🬚", "🬛", "🬜", "🬝", "🬞", "🬟", "🬠", "🬡", "🬢", "🬣", "🬤", "🬥", "🬦", "🬧", "▐", "🬨", "🬩", "🬪", "🬫", "🬬", "🬭", "🬮", "🬯", "🬰", "🬱", "🬲", "🬳", "🬴", "🬵", "🬶", "🬷", "🬸", "🬹", "🬺", "🬻", "█")
|
|
|
|
|
|
|
|
|
|
separated_blocks_sextant = ("\xA0", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "")
|
|
|
|
|
|
|
|
|
|
blocks_octant = ("\xA0", "", "", "🮂", "", "▘", "", "", "", "", "▝", "", "", "", "", "▀", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "🮅", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "▖", "", "", "", "", "▌", "", "", "", "", "▞", "", "", "", "", "▛", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "▗", "", "", "", "", "▚", "", "", "", "", "▐", "", "", "", "", "▜", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "▂", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "▄", "", "", "", "", "▙", "", "", "", "", "▟", "", "▆", "", "", "█")
|
|
|
|
|
|
|
|
|
|
bars = ("\xA0", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█")
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class type_types(IntEnum):
|
|
|
|
|
braille = 0
|
|
|
|
|
block = auto()
|
2024-12-18 00:55:15 +08:00
|
|
|
|
block_quadrant = auto()
|
|
|
|
|
separated_block_quadrant = auto()
|
|
|
|
|
block_sextant = auto()
|
|
|
|
|
separated_block_sextant = auto()
|
|
|
|
|
block_octant = auto()
|
2023-08-30 20:57:03 +08:00
|
|
|
|
histogram = auto() # Set automatically by the histogram() function
|
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
atype_types = (type_types.braille, type_types.block, type_types.block_quadrant, type_types.separated_block_quadrant, type_types.block_sextant, type_types.separated_block_sextant, type_types.block_octant)
|
|
|
|
|
|
|
|
|
|
densities = ((4, 2), (1, 1), (2, 2), (2, 2), (3, 2), (3, 2), (4, 2), (8, 1))
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
marks = (((0, 0),), ((0, 1), (-1, 0), (0, 0), (1, 0), (0, -1)),
|
|
|
|
|
((-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)))
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class mark_types(IntEnum):
|
|
|
|
|
dot = 0
|
|
|
|
|
plus = auto()
|
|
|
|
|
square = auto()
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fractions = {
|
2023-03-10 21:15:01 +08:00
|
|
|
|
"¼": Fraction(1, 4),
|
|
|
|
|
"½": Fraction(1, 2),
|
|
|
|
|
"¾": Fraction(3, 4),
|
|
|
|
|
"⅐": Fraction(1, 7),
|
|
|
|
|
"⅑": Fraction(1, 9),
|
|
|
|
|
"⅒": Fraction(1, 10),
|
|
|
|
|
"⅓": Fraction(1, 3),
|
|
|
|
|
"⅔": Fraction(2, 3),
|
|
|
|
|
"⅕": Fraction(1, 5),
|
|
|
|
|
"⅖": Fraction(2, 5),
|
|
|
|
|
"⅗": Fraction(3, 5),
|
|
|
|
|
"⅘": Fraction(4, 5),
|
|
|
|
|
"⅙": Fraction(1, 6),
|
|
|
|
|
"⅚": Fraction(5, 6),
|
|
|
|
|
"⅛": Fraction(1, 8),
|
|
|
|
|
"⅜": Fraction(3, 8),
|
|
|
|
|
"⅝": Fraction(5, 8),
|
|
|
|
|
"⅞": Fraction(7, 8)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constants = {
|
|
|
|
|
"π": math.pi,
|
|
|
|
|
"e": math.e
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
|
|
|
|
|
class units_types(Enum):
|
|
|
|
|
number = auto()
|
|
|
|
|
scale_none = auto()
|
|
|
|
|
scale_SI = auto()
|
|
|
|
|
scale_IEC = auto()
|
|
|
|
|
scale_IEC_I = auto()
|
|
|
|
|
fracts = auto()
|
|
|
|
|
percent = auto()
|
|
|
|
|
date = auto()
|
|
|
|
|
time = auto()
|
|
|
|
|
monetary = auto()
|
|
|
|
|
|
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
suffix_power_char = ("", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q")
|
2023-05-11 22:03:53 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
MAX = sys.float_info.radix ** sys.float_info.mant_dig - 1
|
|
|
|
|
|
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-03-10 21:15:01 +08:00
|
|
|
|
width = wcswidth(astr)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if width == -1:
|
2024-11-14 01:31:42 +08:00
|
|
|
|
msg = "wcswidth failed. Nonprintable wide character."
|
|
|
|
|
raise ValueError(msg)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
return width
|
2023-03-10 21:15:01 +08:00
|
|
|
|
# return len(astr)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
def outputunit(number: float, scale: units_types) -> str:
|
|
|
|
|
x = 0
|
|
|
|
|
val = number
|
|
|
|
|
if -sys.float_info.max <= val <= sys.float_info.max:
|
|
|
|
|
while abs(val) >= 10:
|
|
|
|
|
x += 1
|
|
|
|
|
val /= 10
|
|
|
|
|
|
|
|
|
|
if scale == units_types.scale_none:
|
|
|
|
|
if x > sys.float_info.dig:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
return f"{number:.{sys.float_info.dig}n}"
|
|
|
|
|
|
|
|
|
|
if x > 33 - 1:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
if scale in {units_types.scale_IEC, units_types.scale_IEC_I}:
|
|
|
|
|
scale_base = 1024
|
|
|
|
|
elif scale == units_types.scale_SI:
|
|
|
|
|
scale_base = 1000
|
|
|
|
|
|
|
|
|
|
power = 0
|
|
|
|
|
if -sys.float_info.max <= number <= sys.float_info.max:
|
|
|
|
|
while abs(number) >= scale_base:
|
|
|
|
|
power += 1
|
|
|
|
|
number /= scale_base
|
|
|
|
|
|
|
|
|
|
anumber = abs(number)
|
|
|
|
|
anumber += 0.0005 if anumber < 10 else 0.005 if anumber < 100 else 0.05 if anumber < 1000 else 0.5
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if number and anumber < 1000 and power > 0:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
strm = f"{number:.{sys.float_info.dig}n}"
|
|
|
|
|
|
|
|
|
|
length = 5 + (number < 0)
|
|
|
|
|
if len(strm) > length:
|
|
|
|
|
prec = 3 if anumber < 10 else 2 if anumber < 100 else 1
|
|
|
|
|
strm = locale.format_string(f"%.{prec}f", number, grouping=True)
|
|
|
|
|
else:
|
|
|
|
|
strm = locale.format_string("%.0f", number, grouping=True)
|
|
|
|
|
|
2024-06-01 23:59:28 +08:00
|
|
|
|
# "k" if power == 1 and scale == scale_SI else
|
2023-05-11 22:03:53 +08:00
|
|
|
|
strm += suffix_power_char[power] if power < len(
|
|
|
|
|
suffix_power_char) else "(error)"
|
|
|
|
|
|
|
|
|
|
if scale == units_types.scale_IEC_I and power > 0:
|
|
|
|
|
strm += "i"
|
|
|
|
|
|
|
|
|
|
return strm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def outputfraction(number: float) -> str:
|
|
|
|
|
"""Convert fractions and constants to Unicode characters."""
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = False
|
|
|
|
|
|
|
|
|
|
strm = ""
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
n = abs(number)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if n <= MAX:
|
|
|
|
|
# fractionpart, intpart = math.modf(number)
|
|
|
|
|
# fractionpart = abs(fractionpart)
|
|
|
|
|
intpart, fractionpart = divmod(Fraction(number).limit_denominator(), 1)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for fraction, value in fractions.items():
|
|
|
|
|
if abs(fractionpart - value) <= sys.float_info.epsilon * n:
|
|
|
|
|
if intpart == 0 and number < 0:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm += "-"
|
2023-03-10 21:15:01 +08:00
|
|
|
|
elif intpart != 0:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
strm += f"{intpart:n}"
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
strm += fraction
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
output = True
|
|
|
|
|
break
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if n > sys.float_info.epsilon and not output:
|
|
|
|
|
for constant, value in constants.items():
|
|
|
|
|
if not output and number % value <= sys.float_info.epsilon * n:
|
|
|
|
|
intpart = number / value
|
|
|
|
|
|
|
|
|
|
if intpart == -1:
|
|
|
|
|
strm += "-"
|
|
|
|
|
elif intpart != 1:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
strm += f"{intpart:.{sys.float_info.dig}n}"
|
2023-03-10 21:15:01 +08:00
|
|
|
|
|
|
|
|
|
strm += constant
|
|
|
|
|
|
|
|
|
|
output = True
|
|
|
|
|
break
|
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if not output:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
strm += f"{number:n}"
|
|
|
|
|
|
|
|
|
|
return strm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def outputlabel(label: float, units: units_types) -> Tuple[int, str]:
|
|
|
|
|
"""Outputs a label in a nice, human readable format."""
|
|
|
|
|
if units == units_types.number:
|
|
|
|
|
strm = f"{label:n}"
|
|
|
|
|
elif units in {units_types.scale_none, units_types.scale_SI, units_types.scale_IEC, units_types.scale_IEC_I}:
|
|
|
|
|
strm = outputunit(label, units)
|
|
|
|
|
elif units == units_types.fracts:
|
|
|
|
|
strm = outputfraction(label)
|
|
|
|
|
elif units == units_types.percent:
|
|
|
|
|
strm = f"{label:%}"
|
|
|
|
|
elif units == units_types.date:
|
|
|
|
|
strm = datetime.fromtimestamp(label, timezone.utc).strftime("%x")
|
|
|
|
|
elif units == units_types.time:
|
|
|
|
|
strm = datetime.fromtimestamp(label, timezone.utc).strftime("%X")
|
|
|
|
|
elif units == units_types.monetary:
|
|
|
|
|
strm = locale.currency(label, grouping=True)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
length = strcol(strm)
|
|
|
|
|
|
|
|
|
|
return length, strm
|
|
|
|
|
|
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
def outputcolor(color: color_types) -> str:
|
|
|
|
|
return f"\033[{colors[color]}m"
|
|
|
|
|
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
def graph(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, array: List[List[int]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, style: style_types = style_types.light, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
"""Output graph."""
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if not array:
|
|
|
|
|
return 1
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not height:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not width:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
w = shutil.get_terminal_size()
|
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
ai, aj = densities[atype]
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
|
|
|
|
aheight = height // ai
|
|
|
|
|
awidth = width // aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if check:
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if aheight > w.lines:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
print(
|
|
|
|
|
f"The height of the graph ({aheight}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
return 1
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if awidth > w.columns:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
print(
|
|
|
|
|
f"The width of the graph ({awidth}) 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 xmin >= xmax:
|
|
|
|
|
print("xmin must be less than xmax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if ymin >= ymax:
|
|
|
|
|
print("ymin must be less than ymax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
xstep = (xmax - xmin) / width
|
|
|
|
|
ystep = (ymax - ymin) / height
|
2023-03-13 22:29:43 +08:00
|
|
|
|
xaxis = 0 if xmin > 0 else width if xmax < 0 else width - xmax / xstep
|
2023-03-10 21:15:01 +08:00
|
|
|
|
yaxis = height if ymin > 0 else 0 if ymax < 0 else ymax / ystep
|
2023-08-30 20:57:03 +08:00
|
|
|
|
xdivisor = 4 * aj * ((((2 * width) // aj) // 160) + 2)
|
|
|
|
|
ydivisor = 2 * ai * ((((4 * height) // ai) // 160) + 2)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if title:
|
2024-11-23 02:49:10 +08:00
|
|
|
|
print(textwrap.fill(title, width=awidth), file=file)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-12-31 23:57:30 +08:00
|
|
|
|
astyle = styles[style]
|
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm = ""
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if border:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[2] + (astyle[0] * awidth) + astyle[4] + "\n"
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
|
i = 0
|
|
|
|
|
while i < height:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
ayaxis = i <= yaxis < i + ai if yaxis <= height - ai else i < yaxis <= i + ai
|
|
|
|
|
yaxislabel = i <= yaxis + ai < i + ai if yaxis <= height - ai else i < yaxis - ai <= i + ai
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
ylabelstrm = ""
|
|
|
|
|
ylabellength = 0
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
if axis and axistick and axisunitslabel and 0 <= yaxis <= height:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = False
|
|
|
|
|
label = 0.0
|
2023-03-10 21:15:01 +08:00
|
|
|
|
adivisor = -ydivisor if i < yaxis else ydivisor
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
k = yaxis + adivisor
|
2023-08-30 20:57:03 +08:00
|
|
|
|
while (k >= i if i < yaxis else k < i + ai) and i >= ai and not output:
|
|
|
|
|
if i <= k < i + ai:
|
|
|
|
|
label = ymax - min(k, height) * ystep
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
output = True
|
|
|
|
|
k += adivisor
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if output:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
ylabellength, ylabelstrm = outputlabel(label, yunits)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
ylabellength *= aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if border:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[1]
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
|
j = 0
|
|
|
|
|
while j < width:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
axaxis = j < xaxis <= j + aj if xaxis >= aj else j <= xaxis < j + aj
|
|
|
|
|
xaxislabel = j < xaxis - aj <= j + aj if xaxis >= aj else j <= xaxis + aj < j + aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
output = False
|
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if axis:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if axaxis and ayaxis:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[6]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = True
|
|
|
|
|
elif axaxis:
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not i:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[4]
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = True
|
2023-08-30 20:57:03 +08:00
|
|
|
|
elif i >= height - ai:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[10]
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = True
|
2023-05-11 22:03:53 +08:00
|
|
|
|
elif axistick:
|
2023-03-10 21:15:01 +08:00
|
|
|
|
adivisor = -ydivisor if i < yaxis else ydivisor
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
k = yaxis + adivisor
|
2023-08-30 20:57:03 +08:00
|
|
|
|
while (k >= i if i < yaxis else k < i + ai) and i >= ai and not output:
|
|
|
|
|
if i <= k < i + ai:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[7 if xaxis >= aj else 5]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = True
|
|
|
|
|
k += adivisor
|
|
|
|
|
if not output:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[1]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = True
|
|
|
|
|
elif ayaxis:
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not j:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[2]
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = True
|
2023-08-30 20:57:03 +08:00
|
|
|
|
elif j >= width - aj:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[4]
|
2023-03-10 21:15:01 +08:00
|
|
|
|
output = True
|
2023-05-11 22:03:53 +08:00
|
|
|
|
elif axistick:
|
2023-03-10 21:15:01 +08:00
|
|
|
|
adivisor = -xdivisor if j < xaxis else xdivisor
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
k = xaxis + adivisor
|
2023-08-30 20:57:03 +08:00
|
|
|
|
while (k >= j if j < xaxis else k < j + aj) and j < width - ai and not output:
|
|
|
|
|
if j <= k < j + aj:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[3 if yaxis <= height - ai else 9]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = True
|
|
|
|
|
k += adivisor
|
|
|
|
|
if not output:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[0]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
output = True
|
2023-05-11 22:03:53 +08:00
|
|
|
|
elif yaxislabel and xaxislabel and axistick and axisunitslabel and ymin <= 0 <= ymax and xmin <= 0 <= xmax:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm += "0"
|
|
|
|
|
output = True
|
2024-11-14 01:31:42 +08:00
|
|
|
|
elif (j >= width - aj if xaxis <= width - aj else not j) and yaxislabel and axislabel:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm += "x"
|
|
|
|
|
output = True
|
2023-05-11 22:03:53 +08:00
|
|
|
|
elif yaxislabel and axistick and axisunitslabel:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
label = 0.0
|
2023-03-10 21:15:01 +08:00
|
|
|
|
adivisor = -xdivisor if j < xaxis else xdivisor
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if j < xaxis:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j += aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
k = xaxis + adivisor
|
2023-08-30 20:57:03 +08:00
|
|
|
|
while (k >= j if j < xaxis else k < j + aj) and j < width - aj and not output:
|
|
|
|
|
if j <= k < j + aj:
|
|
|
|
|
label = min(k, width) * xstep + xmin
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
output = True
|
|
|
|
|
k += adivisor
|
|
|
|
|
|
|
|
|
|
if adivisor < 0:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j -= aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if output:
|
|
|
|
|
output = False
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
length, astrm = outputlabel(label, xunits)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
length *= aj
|
|
|
|
|
if (j >= xaxis or j + length < xaxis - ai) and j + length < width - aj:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm += astrm
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if length > aj:
|
|
|
|
|
j += length - aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if adivisor < 0:
|
|
|
|
|
output = True
|
|
|
|
|
else:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j += aj
|
2024-11-14 01:31:42 +08:00
|
|
|
|
elif (not i if yaxis >= ai else i >= height - ai) and xaxislabel and axislabel:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm += "y"
|
|
|
|
|
output = True
|
2023-08-30 20:57:03 +08:00
|
|
|
|
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:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
strm += ylabelstrm
|
|
|
|
|
output = True
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if ylabellength > aj:
|
|
|
|
|
j += ylabellength - aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if not output:
|
|
|
|
|
dot = 0
|
|
|
|
|
color = 0
|
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for k in range(min(aj, width - j)):
|
|
|
|
|
for l in range(min(ai, height - i)):
|
|
|
|
|
value = array[j + k][i + l]
|
|
|
|
|
if value:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if atype == type_types.braille:
|
|
|
|
|
dot += dotvalues[k][l]
|
|
|
|
|
elif atype in {type_types.block, type_types.block_quadrant, type_types.separated_block_quadrant, type_types.block_sextant, type_types.separated_block_sextant, type_types.block_octant}:
|
|
|
|
|
dot += 1 << (l * aj + k)
|
|
|
|
|
elif atype == type_types.histogram:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if not dot:
|
|
|
|
|
dot = (len(bars) - l) - 1
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if color:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if value and color != value:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
color = 1
|
|
|
|
|
else:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
color = value
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if color:
|
|
|
|
|
color -= 1
|
|
|
|
|
|
|
|
|
|
if color:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
strm += outputcolor(color)
|
|
|
|
|
|
|
|
|
|
if atype == type_types.braille:
|
|
|
|
|
strm += dots[dot]
|
|
|
|
|
elif atype == type_types.block:
|
|
|
|
|
strm += blocks[dot]
|
|
|
|
|
elif atype == type_types.block_quadrant:
|
|
|
|
|
strm += blocks_quadrant[dot]
|
|
|
|
|
elif atype == type_types.separated_block_quadrant:
|
|
|
|
|
strm += separated_blocks_quadrant[dot]
|
|
|
|
|
elif atype == type_types.block_sextant:
|
|
|
|
|
strm += blocks_sextant[dot]
|
|
|
|
|
elif atype == type_types.separated_block_sextant:
|
|
|
|
|
strm += separated_blocks_sextant[dot]
|
|
|
|
|
elif atype == type_types.block_octant:
|
|
|
|
|
strm += blocks_octant[dot]
|
|
|
|
|
elif atype == type_types.histogram:
|
|
|
|
|
strm += bars[dot]
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if color:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
strm += outputcolor(color_types.default)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
j += aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if border:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[1]
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if i < height - ai or border:
|
2023-03-10 21:15:01 +08:00
|
|
|
|
strm += "\n"
|
2023-08-30 20:57:03 +08:00
|
|
|
|
i += ai
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-13 22:29:43 +08:00
|
|
|
|
if border:
|
2023-12-31 23:57:30 +08:00
|
|
|
|
strm += astyle[8] + (astyle[0] * awidth) + astyle[10]
|
2023-03-13 22:29:43 +08:00
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
print(strm, file=file)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
def histogram(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[float], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
"""Convert one or more arrays to graph and output."""
|
|
|
|
|
if not aarray:
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
w = shutil.get_terminal_size()
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not height:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
height = w.lines
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not width:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
width = w.columns
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
|
|
|
|
if check:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if height > w.lines:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
print(
|
2024-12-18 00:55:15 +08:00
|
|
|
|
f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if width > w.columns:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
print(
|
2024-12-18 00:55:15 +08:00
|
|
|
|
f"The width of the graph ({width}) is greater then the width of the terminal ({w.columns}).", file=sys.stderr)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
ai, aj = densities[type_types.histogram]
|
|
|
|
|
|
|
|
|
|
height *= ai
|
|
|
|
|
width *= aj
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not xmin and not xmax:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
xmin = min(aarray)
|
|
|
|
|
xmax = max(aarray)
|
|
|
|
|
|
|
|
|
|
if xmin >= xmax:
|
|
|
|
|
print("xmin must be less than xmax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
histogram = [0 for i in range(width)]
|
|
|
|
|
|
|
|
|
|
xstep = (xmax - xmin) / width
|
|
|
|
|
|
|
|
|
|
for x in aarray:
|
|
|
|
|
if xmin <= x < xmax:
|
|
|
|
|
index = int((x - xmin) / xstep)
|
|
|
|
|
histogram[index] += 1
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not ymin and not ymax:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
ymin = min(histogram)
|
|
|
|
|
ymax = max(histogram)
|
|
|
|
|
|
|
|
|
|
if ymin >= ymax:
|
|
|
|
|
print("ymin must be less than ymax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
ystep = (ymax - ymin) / height
|
|
|
|
|
yaxis = ymax / ystep
|
|
|
|
|
|
|
|
|
|
aaarray = [[0 for j in range(height)] for i in range(width)]
|
|
|
|
|
|
|
|
|
|
acolor = color + 1
|
|
|
|
|
|
|
|
|
|
for x, ay in enumerate(histogram):
|
|
|
|
|
y = 0 if ay >= ymax else int(yaxis - (ay / ystep))
|
|
|
|
|
while y < yaxis and y < height:
|
|
|
|
|
aaarray[x][y] = acolor
|
|
|
|
|
y += 1
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, type_types.histogram, style, title, file)
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
def plots(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarrays: Sequence[Sequence[Sequence[float]]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, mark: mark_types = mark_types.dot, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
"""Convert one or more arrays to graph and output."""
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if not aarrays:
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if not all(len(x) == 2 for aarray in aarrays for x in aarray):
|
|
|
|
|
print("Error: The arrays must have two columns.")
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
w = shutil.get_terminal_size()
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not height:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
height = w.lines
|
2022-07-14 16:33:38 +08:00
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not width:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
width = w.columns
|
2022-07-14 16:33:38 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if check:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if height > w.lines:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
print(
|
2024-12-18 00:55:15 +08:00
|
|
|
|
f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
return 1
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if width > w.columns:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
print(
|
2024-12-18 00:55:15 +08:00
|
|
|
|
f"The width of the graph ({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
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
ai, aj = densities[atype]
|
|
|
|
|
|
|
|
|
|
height *= ai
|
|
|
|
|
width *= aj
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not xmin and not xmax:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
xmin = min(x for aarray in aarrays for x, y in aarray)
|
|
|
|
|
xmax = max(x for aarray in aarrays for x, y in aarray)
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not ymin and not ymax:
|
2023-08-30 20:57:03 +08:00
|
|
|
|
ymin = min(y for aarray in aarrays for x, y in aarray)
|
|
|
|
|
ymax = max(y for aarray in aarrays for x, y in aarray)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if xmin >= xmax:
|
|
|
|
|
print("xmin must be less than xmax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if ymin >= ymax:
|
|
|
|
|
print("ymin must be less than ymax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
xstep = (xmax - xmin) / width
|
|
|
|
|
ystep = (ymax - ymin) / height
|
2023-03-13 22:29:43 +08:00
|
|
|
|
xaxis = width - xmax / xstep
|
2023-03-10 21:15:01 +08:00
|
|
|
|
yaxis = ymax / ystep
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
aaarray = [[0 for j in range(height)] for i in range(width)]
|
|
|
|
|
|
|
|
|
|
for j, aarray in enumerate(aarrays):
|
2023-03-13 22:29:43 +08:00
|
|
|
|
acolor = color + 1 if len(aarrays) == 1 else j % (len(colors) - 2) + 3
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
for x, y in aarray:
|
|
|
|
|
if xmin <= x < xmax and ymin <= y < ymax:
|
|
|
|
|
ax = int(x / xstep + xaxis)
|
|
|
|
|
ay = int(yaxis - y / ystep - 1)
|
|
|
|
|
|
|
|
|
|
for ix, iy in marks[mark]:
|
|
|
|
|
x = ax + ix
|
|
|
|
|
y = ay + iy
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-08-30 20:57:03 +08:00
|
|
|
|
if x < width and y < height:
|
|
|
|
|
if aaarray[x][y]:
|
|
|
|
|
if aaarray[x][y] != acolor:
|
|
|
|
|
aaarray[x][y] = 1
|
|
|
|
|
else:
|
|
|
|
|
aaarray[x][y] = acolor
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
return graph(height, width, xmin, xmax, ymin, ymax, aaarray, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, title, file)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
def plot(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, aarray: Sequence[Sequence[float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, mark: mark_types = mark_types.dot, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
"""Convert single array to graph and output."""
|
2024-11-23 02:49:10 +08:00
|
|
|
|
return plots(height, width, xmin, xmax, ymin, ymax, (aarray,), border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, mark, style, color, title, file, check)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
def functions(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunctions: Sequence[Callable[[float], float]], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
"""Convert one or more functions to graph and output."""
|
2022-07-14 16:33:38 +08:00
|
|
|
|
if not afunctions:
|
2022-02-01 23:20:23 +08:00
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
w = shutil.get_terminal_size()
|
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not height:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
height = w.lines
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2024-11-14 01:31:42 +08:00
|
|
|
|
if not width:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
width = w.columns
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
if check:
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if height > w.lines:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
print(
|
2024-12-18 00:55:15 +08:00
|
|
|
|
f"The height of the graph ({height}) is greater then the height of the terminal ({w.lines}).", file=sys.stderr)
|
2023-03-10 21:15:01 +08:00
|
|
|
|
return 1
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
if height > w.columns:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
print(
|
2024-12-18 00:55:15 +08:00
|
|
|
|
f"The width of the graph ({height}) 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
|
|
|
|
|
2024-12-18 00:55:15 +08:00
|
|
|
|
ai, aj = densities[atype]
|
|
|
|
|
|
|
|
|
|
height *= ai
|
|
|
|
|
width *= aj
|
2023-08-30 20:57:03 +08:00
|
|
|
|
|
2022-02-01 23:20:23 +08:00
|
|
|
|
if xmin >= xmax:
|
|
|
|
|
print("xmin must be less than xmax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
if ymin >= ymax:
|
|
|
|
|
print("ymin must be less than ymax.", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
rows = width
|
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
xstep = (xmax - xmin) / width
|
|
|
|
|
ystep = (ymax - ymin) / height
|
2023-03-13 22:29:43 +08:00
|
|
|
|
xaxis = width - xmax / xstep
|
2023-03-10 21:15:01 +08:00
|
|
|
|
yaxis = ymax / ystep
|
|
|
|
|
xres = 2
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
array = [[0 for j in range(height)] for i in range(width)]
|
|
|
|
|
|
|
|
|
|
for j, function in enumerate(afunctions):
|
2023-03-13 22:29:43 +08:00
|
|
|
|
acolor = color + \
|
|
|
|
|
1 if len(afunctions) == 1 else j % (len(colors) - 2) + 3
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
2023-03-10 21:15:01 +08:00
|
|
|
|
for i in (x / xres for x in range(rows * xres)):
|
2023-03-13 22:29:43 +08:00
|
|
|
|
x = i * xstep + xmin
|
2022-02-01 23:20:23 +08:00
|
|
|
|
y = function(x)
|
|
|
|
|
|
2023-05-11 22:03:53 +08:00
|
|
|
|
if xmin <= x < xmax and ymin <= y < ymax:
|
2023-03-13 22:29:43 +08:00
|
|
|
|
ax = int(x / xstep + xaxis)
|
|
|
|
|
ay = int(yaxis - y / ystep - 1)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
if array[ax][ay]:
|
|
|
|
|
if array[ax][ay] != acolor:
|
|
|
|
|
array[ax][ay] = 1
|
|
|
|
|
else:
|
|
|
|
|
array[ax][ay] = acolor
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
return graph(height, width, xmin, xmax, ymin, ymax, array, border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, title, file)
|
2022-02-01 23:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
2024-11-23 02:49:10 +08:00
|
|
|
|
def function(height: int, width: int, xmin: float, xmax: float, ymin: float, ymax: float, afunction: Callable[[float], float], border: bool = False, axis: bool = True, axislabel: bool = True, axistick: bool = True, axisunitslabel: bool = True, xunits: units_types = units_types.fracts, yunits: units_types = units_types.fracts, atype: type_types = type_types.braille, style: style_types = style_types.light, color: color_types = color_types.red, title: Optional[str] = None, file: TextIO = sys.stdout, check: bool = True) -> int:
|
2023-05-11 22:03:53 +08:00
|
|
|
|
"""Convert single function to function array and output."""
|
2024-11-23 02:49:10 +08:00
|
|
|
|
return functions(height, width, xmin, xmax, ymin, ymax, (afunction,), border, axis, axislabel, axistick, axisunitslabel, xunits, yunits, atype, style, color, title, file, check)
|