Skip to content

Commit

Permalink
Improve TokenizedEntry formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Jun 4, 2024
1 parent 2202b0f commit 2ea3794
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions tivars/types/tokenized.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ def __format__(self, format_spec: str) -> str:

spec, lang = format_spec.split(".")

match spec:
case "":
return self.decode(self.data, lang=lang)
try:
match spec:
case "":
return self.decode(self.data, lang=lang)

case "t":
return self.decode(self.data, lang=lang, mode="accessible")
case "t":
return self.decode(self.data, lang=lang, mode="accessible")

case _:
return super().__format__(format_spec)
except KeyError:
pass

return super().__format__(format_spec)

@staticmethod
def decode(data: bytes, *, lang: str = "en", mode: str = "display") -> str | bytes:
Expand Down Expand Up @@ -364,18 +367,21 @@ class TIAsmProgram(TIProgram):
is_tokenized = False

def __format__(self, format_spec: str) -> str:
try:
match [*format_spec]:
case sep, *width if width:
return self.data.hex(sep, int(''.join(width)))
if match := re.fullmatch(r"(?P<sep>\D)?(?P<width>\d+)?x", format_spec):
match match["sep"], match["width"]:
case None, None:
return self.data.hex()

case sep, *_:
case sep, None:
return self.data.hex(sep)

case _:
return self.data.hex()
case None, width:
return self.data.hex(" ", int(width))

case sep, width:
return self.data.hex(sep, int(width))

except TypeError:
else:
return super().__format__(format_spec)

def get_min_os(self, data: bytes = None) -> OsVersion:
Expand Down

0 comments on commit 2ea3794

Please sign in to comment.