import binascii import itertools RED = "\033[31m" GREEN = "\033[32m" YELLOW = "\033[33m" BLUE = "\033[34m" RESET = "\033[0m" class Diff: def __init__(self, c1, c2): self.state = "" self.c1 = c1 self.c2 = c2 def consume(self, s: str): for p, c in itertools.zip_longest(self.state, s): if c is None: break if p == c: print(f"{self.c1}{c}{RESET}", end="") else: print(f"{self.c2}{c}{RESET}", end="") self.state = s print() d_c = Diff(GREEN, RED) d_s = Diff(BLUE, YELLOW) def handle(tag: str, data: bytes): msg = data.decode().strip() if msg.startswith("0c") or msg.startswith("0a"): # ping, ignore return if tag == "server": d_s.consume(msg) else: d_c.consume(msg)