#!/usr/bin/python3

import json
import sys
import subprocess

from colorama import init as colorama_init, Fore, Style

def get_totp_codes():
    totp_json = subprocess.check_output(["pass", "show", "misc/totp-codes"])
    totp_codes = json.loads(totp_json)
    return { entry["name"]: entry for entry in totp_codes }

def get_totp(secret):
    raw_output = subprocess.check_output(["oathtool", "--base32", "--totp", secret])
    return raw_output.decode("utf-8").strip()

def find_most_similar(totp_data, search_name):
    case_insensitive = search_name.islower()

    for name, entry in totp_data.items():
        if case_insensitive:
            name = name.lower()

        if search_name in name:
            return entry

    return None

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} <app-name>...", file=sys.stderr)
        sys.exit(1)

    colorama_init()
    totp_data = get_totp_codes()

    for app_name in sys.argv[1:]:
        entry = find_most_similar(totp_data, app_name)
        if entry is None:
            print(f"No match for '{app_name}'", file=sys.stderr)
            continue

        name = entry["name"]
        totp = get_totp(entry["secret"])
        print(f"{Fore.MAGENTA}{name}{Fore.RESET}: {totp}")

@@ -107,15 +107,8 @@
 Fore
-, Style
 %0A%0Ade
@@ -765,174 +765,473 @@
     
-if len(sys.argv) %3C 2:%0A        print(f%22Usage: %7Bsys.argv%5B0%5D%7D %3Capp-name%3E...%22, file=sys.stderr)%0A        sys.exit(1)%0A%0A    colorama_init()%0A    totp_data = get_totp_codes()%0A
+exit_code = 0%0A    colorama_init()%0A    totp_data = get_totp_codes()%0A%0A    # No arguments, list all app names%0A    if len(sys.argv) %3C 2:%0A        print(%22List of all TOTP applications:%22)%0A        if not totp_data:%0A            print(%22* (no entries found)%22)%0A        for name in totp_data:%0A            totp = get_totp(totp_data%5Bname%5D%5B%22secret%22%5D)%0A            print(f%22* %7BFore.MAGENTA%7D%7Bname%7D%7BFore.RESET%7D (%7Btotp%7D)%22)%0A%0A        sys.exit(0)%0A%0A    # Print TOTP codes for each app listed
 %0A   
@@ -1407,16 +1407,42 @@
 stderr)%0A
+            exit_code = 1%0A
         
@@ -1584,8 +1584,33 @@
 totp%7D%22)%0A
+%0A    sys.exit(exit_code)%0A

Add listing of all TOTP codes.