import argparse import sys import pyperclip DOLLCODE_CHARS = ("▌", "▖", "▘") BASE_NUM = 3 # encodes input string into integer by using ord() & base256 def encode_base256_int(input: str) -> int: result: int = 0 for char in input: result = (result * 256) + ord(char) return result # decodes input number back in char def decode_base256_int(num: int) -> str: output: str = "" while num > 0: output = (chr(num % 256)) + output num = num // 256 return output # converts encoded base256 int -> bijective base-3 numeration # then inserts corresponding number (3, 1, 2) into output list def encode_dollcode(num: int) -> list[str]: output: list[str] = [] window: int = num mod: int while window > 0: mod = window % BASE_NUM if mod == 0: window = (window - BASE_NUM) // BASE_NUM else: window = (window - mod) // BASE_NUM output.insert(0, DOLLCODE_CHARS[mod]) return output # decodes input dollcode -> integer def decode_dollcode(input: str) -> int: result: int = 0 for char in input: if char not in DOLLCODE_CHARS: raise ValueError(f"Invalid character '{char}' in input") elif char == DOLLCODE_CHARS[0]: result = (result + 1) * BASE_NUM else: result = result * BASE_NUM + DOLLCODE_CHARS.index(char) return result def main(command: str, input: str, copy: bool): result: str = "" try: if command == "decode": result = decode_base256_int(decode_dollcode(input)) else: encoded_dollcode = encode_dollcode(encode_base256_int(input)) result = "".join(encoded_dollcode) if copy: pyperclip.copy(result) print(f"{result} -> 📋 copied!") else: print(result) except ValueError as e: print(f"Error: {e}") sys.exit(1) if __name__ == "__main__": parser = argparse.ArgumentParser(description="converts input into dollcode") subparsers = parser.add_subparsers(dest="command", required=True) # encode subcommand encode_parser = subparsers.add_parser("encode", help="encode text to dollcode") encode_parser.add_argument("input", type=str, help="text to encode") encode_parser.add_argument( "-c", action="store_true", help="copy output to clipboard" ) # decode subcommand decode_parser = subparsers.add_parser("decode", help="decode dollcode to text") decode_parser.add_argument("input", type=str, help="dollcode to decode") decode_parser.add_argument( "-c", action="store_true", help="copy output to clipboard" ) args = parser.parse_args() main(args.command, args.input, args.c)