diff --git a/.local/bin/filelist b/.local/bin/filelist index 247f7f4..f5ff593 100755 --- a/.local/bin/filelist +++ b/.local/bin/filelist @@ -4,32 +4,65 @@ import os from sys import argv +class Tcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + DARKERGREEN = '\033[32m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + + def main(): - path = argv[1] if len(argv) > 1 else os.getcwd() - print_files_size(path) + if len(argv) > 2: + print(f"{Tcolors.FAIL}[Error] Too many arguments.{Tcolors.ENDC}") + exit(1) + path = argv[1] if len(argv) > 1 and argv[1] != '-r' else os.getcwd() + reverse = True if '-r' in argv else False + print_files_size(path, reverse) -def print_files_size(path): +def print_files_size(path, reverse): + files = [] try: directory = os.listdir(path) except FileNotFoundError: - print(f"[Error] Unable to find that directory: '{os.path.abspath(path)}'") + print(f"{Tcolors.FAIL}[Error] Unable to find that directory: '{os.path.abspath(path)}'{Tcolors.ENDC}") exit(1) except NotADirectoryError: - print(f"[Error] Path is not a directory: '{os.path.abspath(path)}'") + print(f"{Tcolors.FAIL}[Error] Path is not a directory: '{os.path.abspath(path)}'{Tcolors.ENDC}") + exit(1) + except PermissionError: + print(f"{Tcolors.FAIL}[Error] Permission denied for: '{os.path.abspath(path)}'{Tcolors.ENDC}") exit(1) else: - print(f"|{'-'*237}|\n| ID | {'File':<200} {'Size':>24} | ID |\n|{'-'*237}|") + print(f"{Tcolors.HEADER}Scanning....{Tcolors.ENDC}") total_size = 0 - for index, file in enumerate(directory, 1): - size = get_dir_size(f'{path}/{file}') if os.path.isdir(f'{path}/{file}') else os.stat(f'{path}/{file}').st_size + for file in directory: + try: + size = get_dir_size(f'{path}/{file}') if os.path.isdir(f'{path}/{file}') else os.stat(f'{path}/{file}').st_size + except PermissionError: + size = 0 + except FileNotFoundError: + size = 0 + except OSError: + size = 0 total_size += size - size = compact_size(size) + compacted_size, color = compact_size(size) if os.path.isdir(f'{path}/{file}'): - size = f"{'