diff --git a/mm.py b/mm.py index 5ce0f34..b23391e 100644 --- a/mm.py +++ b/mm.py @@ -24,7 +24,7 @@ class bcolors: ENDC = '\033[0m' # path where the entries are stored -DIR_PATH = "pages/" +DIR_PATH = "/home/vashqlf/Code/Python/mm/pages/" # order of the rows in the table output KEY_ORDER = ["name", "ipv4", "ipv6", "os", "desc"] # commands that need a name value @@ -85,6 +85,27 @@ def parse_args() -> dict: def print_error(msg: str): print(f"{bcolors.RED}{msg}{bcolors.ENDC}") +def get_groups() -> List[str]: + with os.scandir(DIR_PATH) as entries: + groups = [e.name for e in entries if e.is_dir()] + return groups + +def get_standalone_pages() -> List[str]: + with os.scandir(DIR_PATH) as entries: + pages = [e.name for e in entries if e.is_file()] + return pages + +def with_last(iterable): + it = iter(iterable) + try: + prev = next(it) + except StopIteration: + return + for item in it: + yield prev, False + prev = item + yield prev, True + def get_mm_info() -> dict: dirname = os.path.dirname(__file__) absolute_path = os.path.join(dirname, DIR_PATH) @@ -209,14 +230,18 @@ def print_page(name: str): def list_pages(): print_header("Pages") try: - items = os.listdir(DIR_PATH) - for item in items: - if os.path.isfile(DIR_PATH + item): - print(f"{bcolors.BOLD}{bcolors.BLUE}+ {bcolors.ENDC}{item[:-5]}") - else: - print(f"{bcolors.BOLD}{bcolors.BLUE}{bcolors.ENDC}{bcolors.BOLD}{item}{bcolors.ENDC}") - for dir_item in os.listdir(DIR_PATH + item): - print(f"{bcolors.BOLD}{bcolors.BLUE} + {bcolors.ENDC}{dir_item[:-5]}") + # List standalone Pages + pages = get_standalone_pages() + for page in pages: + print(f"{bcolors.BOLD}{bcolors.BLUE}+ {bcolors.ENDC}{page[:-5]}") + + # List groups + groups = get_groups() + for group in groups: + print(f"{bcolors.BOLD}{group}{bcolors.ENDC}") + for dir_item, is_last in with_last(os.scandir(DIR_PATH + group)): + prefix = "`--" if is_last else "|--" + print(f"{bcolors.BOLD}{bcolors.BLUE}{prefix}+ {bcolors.ENDC}{dir_item.name[:-5]}") except FileNotFoundError: print_error("Page-Path corrupted, restart mm.") @@ -230,6 +255,7 @@ match args["action"]: case "rm": delete_item(args["name"]) case "view": print_page(args["name"]) case "ls": list_pages() + case "test": print(get_groups()) case _: print_welcome()