change layout of ls and add some util functions

This commit is contained in:
2026-10-02 19:31:58 +02:00
parent f2379cc6d4
commit b3217f480b
+35 -9
View File
@@ -24,7 +24,7 @@ class bcolors:
ENDC = '\033[0m' ENDC = '\033[0m'
# path where the entries are stored # 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 # order of the rows in the table output
KEY_ORDER = ["name", "ipv4", "ipv6", "os", "desc"] KEY_ORDER = ["name", "ipv4", "ipv6", "os", "desc"]
# commands that need a name value # commands that need a name value
@@ -85,6 +85,27 @@ def parse_args() -> dict:
def print_error(msg: str): def print_error(msg: str):
print(f"{bcolors.RED}{msg}{bcolors.ENDC}") 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: def get_mm_info() -> dict:
dirname = os.path.dirname(__file__) dirname = os.path.dirname(__file__)
absolute_path = os.path.join(dirname, DIR_PATH) absolute_path = os.path.join(dirname, DIR_PATH)
@@ -209,14 +230,18 @@ def print_page(name: str):
def list_pages(): def list_pages():
print_header("Pages") print_header("Pages")
try: try:
items = os.listdir(DIR_PATH) # List standalone Pages
for item in items: pages = get_standalone_pages()
if os.path.isfile(DIR_PATH + item): for page in pages:
print(f"{bcolors.BOLD}{bcolors.BLUE}+ {bcolors.ENDC}{item[:-5]}") print(f"{bcolors.BOLD}{bcolors.BLUE}+ {bcolors.ENDC}{page[:-5]}")
else:
print(f"{bcolors.BOLD}{bcolors.BLUE}{bcolors.ENDC}{bcolors.BOLD}{item}{bcolors.ENDC}") # List groups
for dir_item in os.listdir(DIR_PATH + item): groups = get_groups()
print(f"{bcolors.BOLD}{bcolors.BLUE} + {bcolors.ENDC}{dir_item[:-5]}") 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: except FileNotFoundError:
print_error("Page-Path corrupted, restart mm.") print_error("Page-Path corrupted, restart mm.")
@@ -230,6 +255,7 @@ match args["action"]:
case "rm": delete_item(args["name"]) case "rm": delete_item(args["name"])
case "view": print_page(args["name"]) case "view": print_page(args["name"])
case "ls": list_pages() case "ls": list_pages()
case "test": print(get_groups())
case _: print_welcome() case _: print_welcome()