From 108f24255d73b441c06935dcfba29c8b9652c509 Mon Sep 17 00:00:00 2001 From: icaema Date: Thu, 22 Feb 2024 15:07:09 -0600 Subject: [PATCH] add the git project ending part --- ls.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/ls.py b/ls.py index f3b1641..7e741a9 100755 --- a/ls.py +++ b/ls.py @@ -6,6 +6,7 @@ import grp import time import argparse from enum import Enum +import subprocess # ANSI escape codes for colors COLORS = { @@ -97,6 +98,27 @@ def sizeof_fmt(num): num /= 1024.0 return f"{num:.1f}Y" +def git_gud(path): + try: + output = subprocess.check_output(['git', 'status', '--porcelain'], cwd=path).strip().decode("utf-8") + if output.strip(): + lines=output.split('\n') + mods = len([x for x in lines if x[0] == 'M']) + unt = len([x for x in lines if x[0] == '?']) + ding = '' + ding += colorize('!'+str(mods), "YELLOW") if mods > 0 else '' + ding += ' ' if mods > 0 and unt > 0 else '' + ding += colorize('?'+str(unt), "CYAN") if unt > 0 else '' + return ding + +# output = subprocess.check_output(['git', 'rev-list', '--count', '--left-only', '@{u}...HEAD'], cwd=file_path, stderr=subprocess.DEVNULL) +# if int(output.strip()) > 0: +# print("bbbb") + except (subprocess.CalledProcessError, FileNotFoundError): + pass + return '' + + def list_files(directory='.', args={}): files = os.listdir(directory) @@ -138,14 +160,19 @@ def list_files(directory='.', args={}): mtime = time.strftime("%b %d %H:%M", time.localtime(stats.st_mtime)) colored_file = color_file(file_path, mode) + + + git='' + if args.extend and Filetype(mode & Filetype.S_IFMT.value) == Filetype.S_IFDIR and os.path.exists(os.path.join(file_path, '.git')): + git=git_gud(file_path) if os.path.islink(file_path): color = "RED" if os.path.exists(os.readlink(file_path)): color = "GREEN" - print(f"{permissions} {nlink:2d} {user:<8s} {group:<8s} {size:>6} {mtime} {colored_file} -> {colorize(os.readlink(file_path), color)}") + print(f"{permissions} {nlink:2d} {user:<8s} {group:<8s} {size:>6} {mtime} {colored_file} -> {colorize(os.readlink(file_path), color)} {git}") else: - print(f"{permissions} {nlink:2d} {user:<8s} {group:<8s} {size:>6} {mtime} {colored_file}") + print(f"{permissions} {nlink:2d} {user:<8s} {group:<8s} {size:>6} {mtime} {colored_file} {git}") else: for file in files: @@ -172,6 +199,8 @@ if __name__ == "__main__": parser.add_argument('-r', '--reverse', action='store_true', help="Reverse sort order") parser.add_argument('-h', '--human', action='store_true', help="Human Readable size") + + parser.add_argument('-e', '--extend', action='store_true', help="Human Readable size") args = parser.parse_args()