Console visual improved

This commit is contained in:
Gino D
2023-01-06 02:37:46 +01:00
parent 4833c58cc4
commit ff96be23dc

View File

@@ -40,7 +40,6 @@ from typing import List, Optional, Tuple, Union
BUFFER_SIZE: int = 256 BUFFER_SIZE: int = 256
class Tab(): class Tab():
colsize: Tuple = () colsize: Tuple = ()
@@ -48,54 +47,57 @@ class Tab():
self.colsize = length self.colsize = length
def blank(self, text: str = '-'): def blank(self, text: str = '-'):
print(text * sum(self.colsize)) sys.stdout.write(text * sum(self.colsize) + '\n')
def print(self, *texts: str, truncate: bool = False): def line(self, *texts: str, show_border: bool = False):
def coltext(text: str, padding_length: int = 0, max_length: int = 20, truncate: bool = False) -> str: lines = {}
text = str(text) max_lines = 0
if len(text) >= max_length:
if truncate:
text = text[:(max_length - 4) if max_length > 5 else max_length] + '...'
else:
words = []
lines = []
for word in text.split(' '):
if sum([len(w) + 1 for w in words]) >= max_length:
if len(lines) == 0:
lines.append(' '.join(words) + '\n')
else:
lines.append((' ' * (padding_length - 1)) + ' '.join(words) + '\n')
words = []
words.append(word)
lines.append((' ' * (padding_length - 1)) + ' '.join(words))
text = ' '.join(lines)
return text + ' ' * (max_length - len(text))
line = ''
padding_length = 0 padding_length = 0
for i, text in enumerate(texts[:len(self.colsize)]): for colno, text in enumerate(texts[:len(self.colsize)]):
line += coltext(text, padding_length, self.colsize[i], truncate) max_length = self.colsize[colno]
padding_length += self.colsize[i] lineno = -1
lines[colno] = ((padding_length, max_length), [])
for paragraph in str(text).split('\n'):
lineno += 1
lines[colno][1].append([])
for word in paragraph.split(' '):
word = word.strip()
next_sentence = ' '.join(lines[colno][1][lineno] + [word])
if len(next_sentence) >= max_length:
lineno += 1
lines[colno][1].append([])
lines[colno][1][lineno].append(word)
max_lines = max(max_lines, lineno)
padding_length += self.colsize[colno]
for i in range(0, max_lines + 1):
output = ''
for _, line in lines.items():
width, bag_of_words = line
if len(bag_of_words) > i:
sentence = ' '.join(bag_of_words[i])
output += sentence + ' ' * (width[1] - len(sentence))
else:
output += ' ' * width[1]
sys.stdout.write(output + '\n')
if show_border:
sys.stdout.write('-' * sum(self.colsize) + '\n')
print(line)
def labels(self, *texts: str, blank_text: str = '-'): def labels(self, *texts: str, blank_text: str = '-'):
self.blank(blank_text) self.blank(blank_text)
self.print(*texts) self.line(*texts, show_border=True)
self.blank(blank_text)
# tab = Tab(4, 11, 60)
# tab.print('[*]', 'Notice how even though the input was a set and a tuple', 'Notice how even though the input was a set and a tuple, the output is a list because sorted() returns a new list by definition')
# tab.print('[*]', 'abcdefghij', 'The returned object can be cast to a new type if it needs to match the input type. Be careful if attempting to cast the resulting list back to a set, as a set by definition is unordered')
# exit()
class Telnet: class Telnet:
@@ -314,9 +316,9 @@ class Pyboard(object):
for call in traceback[1][:-2].split('\\r\\n'): for call in traceback[1][:-2].split('\\r\\n'):
reason += f'{call}\n' reason += f'{call}\n'
raise Exception(f'{"-"* 45}\n{reason.strip()}') raise Exception('' + reason.strip())
else: else:
raise Exception(f'{"-" * 45}\n{exception}') raise Exception(exception)
return data.strip() return data.strip()
@@ -634,11 +636,11 @@ class Picowatch(object):
if status: if status:
for filename, size in output: for filename, size in output:
if size == -1: if size == -1:
tab.print('[*]', filename, '-') tab.line('[*]', filename, '-')
else: else:
tab.print('[*]', filename, f'{round(size / 1024, 2)} kb') tab.line('[*]', filename, f'{round(size / 1024, 2)} kb')
else: else:
tab.print('[?]', filepath, '', str(exception)) tab.line('[?]', filepath, '', str(exception))
def contents(self, filename: str): def contents(self, filename: str):
filename = filename.strip('./').strip('/') filename = filename.strip('./').strip('/')
@@ -656,9 +658,9 @@ class Picowatch(object):
destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/') destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/')
try: try:
tab.print('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination)) tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination))
except Exception as e: except Exception as e:
tab.print('[?]', destination, '', '', str(e)) tab.line('[?]', destination, '', '', str(e))
def download(self, filepath: str): def download(self, filepath: str):
tab = Tab(4, 30, 15, 100) tab = Tab(4, 30, 15, 100)
@@ -675,11 +677,11 @@ class Picowatch(object):
if not size == -1: if not size == -1:
try: try:
tab.print('[↓]', remote, self.filesystem.download(remote, destination)) tab.line('[↓]', remote, self.filesystem.download(remote, destination))
except Exception as e: except Exception as e:
tab.print('[?]', remote, '', str(e)) tab.line('[?]', remote, '', str(e))
else: else:
tab.print('[?]', filepath, '', exception) tab.line('[?]', filepath, '', exception)
def delete(self, filepath: str): def delete(self, filepath: str):
tab = Tab(4, 30, 100) tab = Tab(4, 30, 100)
@@ -689,11 +691,11 @@ class Picowatch(object):
if status: if status:
for filename, checked, exception in output: for filename, checked, exception in output:
if checked: if checked:
tab.print('[-]', filename) tab.line('[-]', filename)
else: else:
tab.print('[?]', filename, exception) tab.line('[?]', filename, exception)
else: else:
tab.print('[?]', filepath, exception) tab.line('[?]', filepath, exception)
def compare(self, filepath: str): def compare(self, filepath: str):
content, _ = self.filesystem.get(filepath.strip('./').strip('/')) content, _ = self.filesystem.get(filepath.strip('./').strip('/'))
@@ -737,7 +739,7 @@ class Picowatch(object):
tab.labels('[ ]', 'Filename') tab.labels('[ ]', 'Filename')
for status, filename in changes: for status, filename in changes:
tab.print('[+]' if status == 1 else '[-]', filename) tab.line('[+]' if status == 1 else '[-]', filename)
def push(self): def push(self):
tab = Tab(4, 30, 15, 100) tab = Tab(4, 30, 15, 100)
@@ -751,11 +753,11 @@ class Picowatch(object):
if status: if status:
for filename, checked, exception in output: for filename, checked, exception in output:
if checked: if checked:
tab.print('[-]', filename, '', 'DELETED') tab.line('[-]', filename, '', 'DELETED')
else: else:
tab.print('[?]', filename, '', '', exception) tab.line('[?]', filename, '', '', exception)
else: else:
tab.print('[?]', filepath, '', exception) tab.line('[?]', filepath, '', exception)
queue = [] queue = []
@@ -766,9 +768,9 @@ class Picowatch(object):
destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/') destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/')
try: try:
tab.print('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination)) tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination))
except Exception as e: except Exception as e:
tab.print('[?]', destination, '', '', str(e)) tab.line('[?]', destination, '', '', str(e))
print('Pico board up to date.') print('Pico board up to date.')