Skip to content

Commit 2192f25

Browse files
committed
Improve and slightly change hex formatting
1 parent 170e063 commit 2192f25

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

tests/tivars.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ def test_truthiness(self):
138138
test_program.clear()
139139
self.assertEqual(bool(test_program), True)
140140

141+
def test_hex(self):
142+
test_program = TIEntry.open("tests/data/var/Program.8xp")
143+
self.assertEqual(f"{test_program}", "setDate(1")
144+
self.assertEqual(f"{test_program:-2X:}", "0300:EF00:31")
145+
141146

142147
class TokenizationTests(unittest.TestCase):
143148
def test_load_from_file(self):

tivars/var.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,19 +431,24 @@ def __format__(self, format_spec: str) -> str:
431431
:return: A string representation of this entry
432432
"""
433433

434-
if match := re.fullmatch(r"(?P<sep>\D)?(?P<width>\d+)?x", format_spec):
434+
if match := re.fullmatch(r"(?P<width>[+-]?\d+)?(?P<case>[xX])(?P<sep>\D)?", format_spec):
435435
match match["sep"], match["width"]:
436436
case None, None:
437-
return self.calc_data.hex()
437+
string = self.calc_data.hex()
438438

439439
case sep, None:
440-
return self.calc_data.hex(sep)
440+
string = self.calc_data.hex(sep)
441441

442442
case None, width:
443-
return self.calc_data.hex(" ", int(width))
443+
string = self.calc_data.hex(" ", int(width))
444444

445445
case sep, width:
446-
return self.calc_data.hex(sep, int(width))
446+
string = self.calc_data.hex(sep, int(width))
447+
448+
return string if match["case"] == "x" else string.upper()
449+
450+
elif not format_spec:
451+
return super().__str__()
447452

448453
else:
449454
raise TypeError(f"unsupported format string passed to {type(self)}.__format__")

0 commit comments

Comments
 (0)