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