diff --git a/tests/tivars.py b/tests/tivars.py index 4ca4690..bf6ef33 100644 --- a/tests/tivars.py +++ b/tests/tivars.py @@ -138,6 +138,11 @@ def test_truthiness(self): test_program.clear() self.assertEqual(bool(test_program), True) + def test_hex(self): + test_program = TIEntry.open("tests/data/var/Program.8xp") + self.assertEqual(f"{test_program}", "setDate(1") + self.assertEqual(f"{test_program:-2X:}", "0300:EF00:31") + class TokenizationTests(unittest.TestCase): def test_load_from_file(self): diff --git a/tivars/var.py b/tivars/var.py index 7f3c8c9..87bb788 100644 --- a/tivars/var.py +++ b/tivars/var.py @@ -431,19 +431,24 @@ def __format__(self, format_spec: str) -> str: :return: A string representation of this entry """ - if match := re.fullmatch(r"(?P\D)?(?P\d+)?x", format_spec): + if match := re.fullmatch(r"(?P[+-]?\d+)?(?P[xX])(?P\D)?", format_spec): match match["sep"], match["width"]: case None, None: - return self.calc_data.hex() + string = self.calc_data.hex() case sep, None: - return self.calc_data.hex(sep) + string = self.calc_data.hex(sep) case None, width: - return self.calc_data.hex(" ", int(width)) + string = self.calc_data.hex(" ", int(width)) case sep, width: - return self.calc_data.hex(sep, int(width)) + string = self.calc_data.hex(sep, int(width)) + + return string if match["case"] == "x" else string.upper() + + elif not format_spec: + return super().__str__() else: raise TypeError(f"unsupported format string passed to {type(self)}.__format__")