Skip to content

Commit

Permalink
Table is now being refreshed on overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jasursadikov committed Sep 7, 2024
1 parent 2e1e948 commit 40b4b5d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
36 changes: 22 additions & 14 deletions commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import utils
import shutil
import asyncio
import subprocess

from styles import *
from typing import List, Dict
from collections import Counter
from prettytable import PrettyTable, PLAIN_COLUMNS
from styles import *


class Commands:
Expand Down Expand Up @@ -104,7 +105,7 @@ def log(self, repos: Dict[str, List[str]]) -> None:
formatted_path = self._get_formatted_path(path)
branch = self._get_branch_status(path)
author = self._get_authors_name(path)
commit = self._get_commit_message(path, 35)
commit = self._get_commit_message(path)

# Commit time
commit_time_cmd = subprocess.run('git log -1 --pretty=format:%cd --date=relative', shell=True, text=True, cwd=path, capture_output=True)
Expand Down Expand Up @@ -212,9 +213,9 @@ async def _run_process(self, repo_path: str, table: Dict[str, List[str]], comman

return_code = await process.wait()
if return_code == 0:
status = f'{GREEN}{utils.GLYPHS["finished"]}'
status = f'{GREEN}{utils.GLYPHS["finished"]}{RESET}'
else:
status = f'{RED}{utils.GLYPHS["failed"]} Code: {return_code}'
status = f'{RED}{utils.GLYPHS["failed"]} Code: {return_code}{RESET}'

table[repo_path] = [table[repo_path][0], status]
self._print_process(table)
Expand All @@ -224,23 +225,18 @@ def _print_process(self, info: Dict[str, List[str]]) -> None:

for path, (line, status) in info.items():
formatted_path = self._get_formatted_path(path)
table.add_row([formatted_path, line, status])
table.add_row([formatted_path, status, line])

table_str = self._table_to_str(table)
num_lines = table_str.count('\n') + 1

if hasattr(self, '_last_printed_lines') and self._last_printed_lines > 0:
for _ in range(self._last_printed_lines):
# Clear previous line
print('\033[A\033[K', end='')

print(f'{table_str}\n', end='')
self._print_table(table)
self._last_printed_lines = num_lines

def _print_table(self, table: PrettyTable):
table = self._table_to_str(table)
if len(table) != 0:
print(table)

@staticmethod
def _get_status_string(files: List[str]):
modified, added, removed, moved = 0, 0, 0, 0
Expand Down Expand Up @@ -268,6 +264,19 @@ def _get_status_string(files: List[str]):
status = f'{GREEN}{utils.GLYPHS["clear"]}{RESET}'
return status

@staticmethod
def _print_table(table: PrettyTable):
width, _ = shutil.get_terminal_size()
rows = Commands._table_to_str(table).split('\n')
for row in rows:
if len(row) != 0:
if len(sterilize(row)) > width:
styles_count = len(row) - len(sterilize(row))
count = width + styles_count - 1
print(row[:count] + RESET)
else:
print(row)

@staticmethod
def _table_to_str(table: PrettyTable) -> str:
table = table.get_string()
Expand Down Expand Up @@ -310,10 +319,9 @@ def _get_authors_name(path: str) -> str:
return author

@staticmethod
def _get_commit_message(path: str, max_chars: int) -> str:
def _get_commit_message(path: str) -> str:
cmd = subprocess.run('git log -1 --pretty=format:%s', shell=True, text=True, cwd=path, capture_output=True)
log = cmd.stdout.strip()
log = log[:max_chars] + '...' if len(log) > max_chars else log
return log

@staticmethod
Expand Down
14 changes: 13 additions & 1 deletion styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
UNDERLINE = '\033[4m'
BLINK = '\033[5m'

STYLES = [BOLD, DIM, ITALIC, UNDERLINE, BLINK]

END_BOLD = '\033[22m'
END_DIM = '\033[22m'
END_ITALIC = '\033[23m'
END_UNDERLINE = '\033[24m'
END_BLINK = '\033[25m'

RESET = '\033[0m'
END = [END_BOLD, END_DIM, END_ITALIC, END_UNDERLINE, END_BLINK]

# Text colors
WHITE = '\033[37m'
Expand Down Expand Up @@ -51,3 +53,13 @@
BKG_BRIGHT_CYAN = '\033[106m'

BKG = [BKG_WHITE, BKG_MEDIUM_GRAY, BKG_BLACK, BKG_RED, BKG_GREEN, BKG_YELLOW, BKG_BLUE, BKG_MAGENTA, BKG_CYAN, BKG_BRIGHT_WHITE, BKG_BRIGHT_RED, BKG_BRIGHT_GREEN, BKG_BRIGHT_YELLOW, BKG_BRIGHT_BLUE, BKG_BRIGHT_MAGENTA, BKG_BRIGHT_CYAN]

RESET = '\033[0m'

ALL = BKG + TEXT + STYLES + END + [RESET]


def sterilize(string: str) -> str:
for char in ALL:
string = string.replace(char, '')
return string
2 changes: 1 addition & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ def ask(text: str) -> bool:

def print_error(text: str, code: int = 255) -> None:
print(f'{RED}Error:{RESET} {text}')
sys.exit(code)
sys.exit(code)

0 comments on commit 40b4b5d

Please sign in to comment.