From ff96be23dc225aca60d51eba67da2ed8c3d2d054 Mon Sep 17 00:00:00 2001 From: Gino D Date: Fri, 6 Jan 2023 02:37:46 +0100 Subject: [PATCH] Console visual improved --- src/picowatch.py | 124 ++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/src/picowatch.py b/src/picowatch.py index 8d92578..1a3ed0c 100644 --- a/src/picowatch.py +++ b/src/picowatch.py @@ -40,7 +40,6 @@ from typing import List, Optional, Tuple, Union BUFFER_SIZE: int = 256 - class Tab(): colsize: Tuple = () @@ -48,54 +47,57 @@ class Tab(): self.colsize = length 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 coltext(text: str, padding_length: int = 0, max_length: int = 20, truncate: bool = False) -> str: - text = str(text) - - 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 = '' + def line(self, *texts: str, show_border: bool = False): + lines = {} + max_lines = 0 padding_length = 0 - for i, text in enumerate(texts[:len(self.colsize)]): - line += coltext(text, padding_length, self.colsize[i], truncate) - padding_length += self.colsize[i] + for colno, text in enumerate(texts[:len(self.colsize)]): + max_length = self.colsize[colno] + 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 = '-'): self.blank(blank_text) - self.print(*texts) - 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() + self.line(*texts, show_border=True) class Telnet: @@ -314,9 +316,9 @@ class Pyboard(object): for call in traceback[1][:-2].split('\\r\\n'): reason += f'{call}\n' - raise Exception(f'{"-"* 45}\n{reason.strip()}') + raise Exception('' + reason.strip()) else: - raise Exception(f'{"-" * 45}\n{exception}') + raise Exception(exception) return data.strip() @@ -634,11 +636,11 @@ class Picowatch(object): if status: for filename, size in output: if size == -1: - tab.print('[*]', filename, '-') + tab.line('[*]', filename, '-') else: - tab.print('[*]', filename, f'{round(size / 1024, 2)} kb') + tab.line('[*]', filename, f'{round(size / 1024, 2)} kb') else: - tab.print('[?]', filepath, '', str(exception)) + tab.line('[?]', filepath, '', str(exception)) def contents(self, filename: str): filename = filename.strip('./').strip('/') @@ -656,9 +658,9 @@ class Picowatch(object): destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/') 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: - tab.print('[?]', destination, '', '', str(e)) + tab.line('[?]', destination, '', '', str(e)) def download(self, filepath: str): tab = Tab(4, 30, 15, 100) @@ -675,11 +677,11 @@ class Picowatch(object): if not size == -1: try: - tab.print('[↓]', remote, self.filesystem.download(remote, destination)) + tab.line('[↓]', remote, self.filesystem.download(remote, destination)) except Exception as e: - tab.print('[?]', remote, '', str(e)) + tab.line('[?]', remote, '', str(e)) else: - tab.print('[?]', filepath, '', exception) + tab.line('[?]', filepath, '', exception) def delete(self, filepath: str): tab = Tab(4, 30, 100) @@ -689,11 +691,11 @@ class Picowatch(object): if status: for filename, checked, exception in output: if checked: - tab.print('[-]', filename) + tab.line('[-]', filename) else: - tab.print('[?]', filename, exception) + tab.line('[?]', filename, exception) else: - tab.print('[?]', filepath, exception) + tab.line('[?]', filepath, exception) def compare(self, filepath: str): content, _ = self.filesystem.get(filepath.strip('./').strip('/')) @@ -737,7 +739,7 @@ class Picowatch(object): tab.labels('[ ]', 'Filename') for status, filename in changes: - tab.print('[+]' if status == 1 else '[-]', filename) + tab.line('[+]' if status == 1 else '[-]', filename) def push(self): tab = Tab(4, 30, 15, 100) @@ -751,11 +753,11 @@ class Picowatch(object): if status: for filename, checked, exception in output: if checked: - tab.print('[-]', filename, '', 'DELETED') + tab.line('[-]', filename, '', 'DELETED') else: - tab.print('[?]', filename, '', '', exception) + tab.line('[?]', filename, '', '', exception) else: - tab.print('[?]', filepath, '', exception) + tab.line('[?]', filepath, '', exception) queue = [] @@ -766,9 +768,9 @@ class Picowatch(object): destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/') 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: - tab.print('[?]', destination, '', '', str(e)) + tab.line('[?]', destination, '', '', str(e)) print('Pico board up to date.')