Tab class refactorized

This commit is contained in:
Gino D
2023-01-06 12:05:19 +01:00
parent ff96be23dc
commit f6b4707191

View File

@@ -41,15 +41,30 @@ from typing import List, Optional, Tuple, Union
BUFFER_SIZE: int = 256
class Tab():
colsize: Tuple = ()
bordered: bool = False
colsize: List = []
def __init__(self, *length: int):
self.colsize = length
def __init__(self, *colsizes: int, nb_columns: int = 0, bordered: bool = False):
self.colsize = list(colsizes)
self.bordered = bordered
auto_size = nb_columns - len(self.colsize)
def blank(self, text: str = '-'):
if nb_columns > 0 and auto_size > 0:
terminal_size = os.get_terminal_size()
column_size = int((terminal_size.columns - sum(self.colsize)) / auto_size)
for i in range(len(self.colsize), nb_columns):
self.colsize.append(column_size)
def head(self, *texts: str):
self.border('=')
self.line(*texts, bordered=False)
self.border('=')
def border(self, text: str = '-'):
sys.stdout.write(text * sum(self.colsize) + '\n')
def line(self, *texts: str, show_border: bool = False):
def line(self, *texts: str, bordered: bool = None):
lines = {}
max_lines = 0
padding_length = 0
@@ -73,7 +88,6 @@ class Tab():
lines[colno][1][lineno].append(word)
max_lines = max(max_lines, lineno)
padding_length += self.colsize[colno]
@@ -91,13 +105,11 @@ class Tab():
sys.stdout.write(output + '\n')
if show_border:
sys.stdout.write('-' * sum(self.colsize) + '\n')
if bordered == False:
return
def labels(self, *texts: str, blank_text: str = '-'):
self.blank(blank_text)
self.line(*texts, show_border=True)
if bordered or self.bordered:
self.border('-')
class Telnet:
@@ -630,7 +642,7 @@ class Picowatch(object):
def listing(self, filepath: str = '/'):
filepath = filepath.strip('./')
tab = Tab(4, 30, 15, 100)
tab.labels('[ ]', 'Filename', 'Size (kb)', 'Exception')
tab.head('[ ]', 'Filename', 'Size (kb)', 'Exception')
status, output, exception = self.filesystem.ls(filepath)
if status:
@@ -652,7 +664,7 @@ class Picowatch(object):
def upload(self, filepath: str):
tab = Tab(4, 30, 15, 15, 100)
tab.labels('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
for source, size in self.internal_ls(filepath):
destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/')
@@ -664,7 +676,7 @@ class Picowatch(object):
def download(self, filepath: str):
tab = Tab(4, 30, 15, 100)
tab.labels('[ ]', 'Filename', 'Checksum', 'Exception')
tab.head('[ ]', 'Filename', 'Checksum', 'Exception')
status, output, exception = self.filesystem.ls(filepath.strip('./').strip('/'))
if status:
@@ -685,7 +697,7 @@ class Picowatch(object):
def delete(self, filepath: str):
tab = Tab(4, 30, 100)
tab.labels('[ ]', 'Filename', 'Exception')
tab.head('[ ]', 'Filename', 'Exception')
status, output, exception = self.filesystem.rm(filepath.strip('./'))
if status:
@@ -736,14 +748,14 @@ class Picowatch(object):
return changes
tab = Tab(4, 40)
tab.labels('[ ]', 'Filename')
tab.head('[ ]', 'Filename')
for status, filename in changes:
tab.line('[+]' if status == 1 else '[-]', filename)
def push(self):
tab = Tab(4, 30, 15, 100)
tab.labels('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
changes = self.status(return_output=True)
for filepath in [filename for status, filename in changes if status == -1]: