Skip to content

Commit

Permalink
mud info command updated
Browse files Browse the repository at this point in the history
  • Loading branch information
jasursadikov committed Sep 16, 2024
1 parent e300ade commit 4b4dcf2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def run(self) -> None:
return

self.config.find()
self._parse_arguments()
self._filter_with_arguments()

self.cmd_runner = Runner(self.config)
# Handling commands
Expand Down Expand Up @@ -168,7 +168,7 @@ def remove(self, args) -> None:
self.config.save(utils.CONFIG_FILE_NAME)

# Filter out repositories if user provided filters
def _parse_arguments(self) -> None:
def _filter_with_arguments(self) -> None:
self.repos = self.config.data
self.table = utils.settings.config['mud'].getboolean('run_table')
self.run_async = utils.settings.config['mud'].getboolean('run_async')
Expand Down
110 changes: 86 additions & 24 deletions runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,81 @@ def __init__(self, repos):

# `mud info` command implementation
def info(self, repos: Dict[str, List[str]]) -> None:
def get_directory_size(directory):
total_size = 0
for dirpath, dirnames, filenames in os.walk(directory):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.isfile(fp):
total_size += os.path.getsize(fp)
return total_size

def format_size(size_in_bytes):
if size_in_bytes >= 1024 ** 3:
return f"{RED}{glyphs("weight")}{glyphs("space")}{RESET}{BOLD}{size_in_bytes / (1024 ** 3):.2f}{RESET} GB"
elif size_in_bytes >= 1024 ** 2:
return f"{YELLOW}{glyphs("weight")}{glyphs("space")}{RESET}{BOLD}{size_in_bytes / (1024 ** 2):.2f}{RESET} MB"
elif size_in_bytes >= 1024:
return f"{GREEN}{glyphs("weight")}{glyphs("space")}{RESET}{BOLD}{size_in_bytes / 1024:.2f}{RESET} KB"
else:
return f"{BLUE}{glyphs("weight")}{glyphs("space")}{RESET}{BOLD}{size_in_bytes}{RESET} Bytes"

def get_git_origin_host():
try:
result = subprocess.check_output('git remote get-url origin', shell=True, text=True, cwd=path)
remote_url = result.strip()

if '://' in remote_url:
host = remote_url.split('/')[2]
else:
host = remote_url.split(':')[0].split('@')[1]

icon = YELLOW + glyphs('git')

if 'azure' in remote_url or 'visualstudio' in remote_url:
icon = BLUE + glyphs('azure')
elif 'github' in remote_url:
icon = GRAY + glyphs('github')
elif 'gitlab' in remote_url:
icon = YELLOW + glyphs('gitlab')
elif 'bitbucket' in remote_url:
icon = CYAN + glyphs('bitbucket')

icon += RESET + glyphs('space')

return f'{icon}{host}'
except Exception as e:
return 'Unknown origin'

table = utils.get_table()
for path, labels in repos.items():
output = subprocess.check_output('git status --porcelain', shell=True, text=True, cwd=path)
output = self._get_status_porcelain(path)
files = output.splitlines()

formatted_path = self._get_formatted_path(path)
size = f'{format_size(get_directory_size(path))}'
origin = get_git_origin_host()
branch = self._get_branch_status(path)
status = self._get_status_string(files)
colored_labels = self._get_formatted_labels(labels)
origin_sync = self._get_origin_sync(path)

# Sync with origin status
try:
ahead_behind_cmd = subprocess.run('git rev-list --left-right --count HEAD...@{upstream}', shell=True, text=True, cwd=path, capture_output=True)
stdout = ahead_behind_cmd.stdout.strip().split()
except subprocess.CalledProcessError:
stdout = ['0', '0']

origin_sync = ''
if len(stdout) >= 2:
ahead, behind = stdout[0], stdout[1]
if ahead and ahead != '0':
origin_sync += f'{BRIGHT_GREEN}{glyphs("ahead")} {ahead}{RESET}'
if behind and behind != '0':
if origin_sync:
origin_sync += ' '
origin_sync += f'{BRIGHT_BLUE}{glyphs("behind")} {behind}{RESET}'

if not origin_sync.strip():
origin_sync = f'{BLUE}{glyphs("synced")}{RESET}'

table.add_row([formatted_path, branch, origin_sync, status, colored_labels])
table.add_row([formatted_path, origin, size, branch, origin_sync, status, colored_labels])

utils.print_table(table)

# `mud status` command implementation
def status(self, repos: Dict[str, List[str]]) -> None:

table = utils.get_table()
for path, labels in repos.items():
output = subprocess.check_output('git status --porcelain', shell=True, text=True, cwd=path)
output = self._get_status_porcelain(path)
files = output.splitlines()

formatted_path = self._get_formatted_path(path)
branch = self._get_branch_status(path)
status = self._get_status_string(files)
origin_sync = self._get_origin_sync(path)

colored_output = []

Expand All @@ -88,7 +119,7 @@ def status(self, repos: Dict[str, List[str]]) -> None:
if len(files) > 5:
colored_output.append('...')

table.add_row([formatted_path, branch, status, ', '.join(colored_output)])
table.add_row([formatted_path, branch, origin_sync, status, ', '.join(colored_output)])

utils.print_table(table)

Expand Down Expand Up @@ -246,6 +277,37 @@ def _clear_printed_lines(self) -> None:
print('\033[A\033[K', end='')
self._last_printed_lines = 0

# Sync with origin status
def _get_origin_sync(self, path: str) -> str:
try:
ahead_behind_cmd = subprocess.run('git rev-list --left-right --count HEAD...@{upstream}', shell=True, text=True, cwd=path, capture_output=True)
stdout = ahead_behind_cmd.stdout.strip().split()
except subprocess.CalledProcessError:
stdout = ['0', '0']

origin_sync = ''
if len(stdout) >= 2:
ahead, behind = stdout[0], stdout[1]
if ahead and ahead != '0':
origin_sync += f'{BRIGHT_GREEN}{glyphs("ahead")} {ahead}{RESET}'
if behind and behind != '0':
if origin_sync:
origin_sync += ' '
origin_sync += f'{BRIGHT_BLUE}{glyphs("behind")} {behind}{RESET}'

if not origin_sync.strip():
origin_sync = f'{BLUE}{glyphs("synced")}{RESET}'

return origin_sync

@staticmethod
def _get_status_porcelain(path: str) -> str:
try:
output = subprocess.check_output('git status --porcelain', shell=True, text=True, cwd=path)
return output
except Exception as e:
return e

@staticmethod
def _get_status_string(files: List[str]) -> str:
modified, added, removed, moved = 0, 0, 0, 0
Expand Down
50 changes: 28 additions & 22 deletions styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,34 @@
ALL = BKG + TEXT + STYLES + END + [RESET]

GLYPHS = {
'ahead': ['\uf062', 'Ahead'],
'behind': ['\uf063', 'Behind'],
'modified': ['\uf040', '*'],
'added': ['\uf067', '+'],
'removed': ['\uf1f8', '-'],
'moved': ['\uf064', 'M'],
'clear': ['\uf00c', 'Clear'],
'synced': ['\uf00c', 'Up to date'],
'master': ['\uf015', ''],
'bugfix': ['\uf188', ''],
'release': ['\uf135', ''],
'feature': ['\uf0ad', ''],
'branch': ['\ue725', ''],
'failed': ['\uf00d', 'Failed'],
'finished': ['\uf00c', 'Finished'],
'running': ['\uf46a', 'Running'],
'label': ['\uf435', ''],
'tag': ['\uf02b', ''],
'terminal': ['\ue795', ''],
'(': ['\uE0B2', ''],
')': ['\uE0B0', ' '],
'space': [' ', '']
'ahead': ['\uf062', 'Ahead'],
'behind': ['\uf063', 'Behind'],
'modified': ['\uf040', '*'],
'added': ['\uf067', '+'],
'removed': ['\uf1f8', '-'],
'moved': ['\uf064', 'M'],
'clear': ['\uf00c', 'Clear'],
'synced': ['\uf00c', 'Up to date'],
'master': ['\uf015', ''],
'bugfix': ['\uf188', ''],
'release': ['\uf135', ''],
'feature': ['\uf0ad', ''],
'branch': ['\ue725', ''],
'failed': ['\uf00d', 'Failed'],
'finished': ['\uf00c', 'Finished'],
'running': ['\uf46a', 'Running'],
'label': ['\uf435', ''],
'tag': ['\uf02b', ''],
'terminal': ['\ue795', ''],
'(': ['\uE0B2', ''],
')': ['\uE0B0', ' '],
'weight': ['\uee94', ''],
'space': [' ', ''],
'git': ['\ue702', ''],
'github': ['\uf09b', ''],
'gitlab': ['\uf296', ''],
'azure': ['\uebe8', ''],
'bitbucket': ['\ue703', '']
}


Expand Down

0 comments on commit 4b4dcf2

Please sign in to comment.