Tab align to right possibility added

This commit is contained in:
Gino D
2023-01-06 15:39:28 +01:00
parent f6b4707191
commit 9f4b0903be

View File

@@ -43,6 +43,7 @@ BUFFER_SIZE: int = 256
class Tab(): class Tab():
bordered: bool = False bordered: bool = False
colsize: List = [] colsize: List = []
text_to_right: List = []
def __init__(self, *colsizes: int, nb_columns: int = 0, bordered: bool = False): def __init__(self, *colsizes: int, nb_columns: int = 0, bordered: bool = False):
self.colsize = list(colsizes) self.colsize = list(colsizes)
@@ -64,44 +65,49 @@ class Tab():
def border(self, text: str = '-'): def border(self, text: str = '-'):
sys.stdout.write(text * sum(self.colsize) + '\n') sys.stdout.write(text * sum(self.colsize) + '\n')
def align_to_right(self, *column_num: int):
self.text_to_right = [i - 1 for i in column_num]
def line(self, *texts: str, bordered: bool = None): def line(self, *texts: str, bordered: bool = None):
lines = {} lines = {}
max_lines = 0 max_lines = 0
padding_length = 0
for colno, text in enumerate(texts[:len(self.colsize)]): for column_num, text in enumerate(texts[:len(self.colsize)]):
max_length = self.colsize[colno] max_length = self.colsize[column_num]
lineno = -1 lineno = -1
lines[colno] = ((padding_length, max_length), []) lines[column_num] = (max_length, [])
for paragraph in str(text).split('\n'): for paragraph in str(text).split('\n'):
lineno += 1 lineno += 1
lines[colno][1].append([]) lines[column_num][1].append([])
for word in paragraph.split(' '): for word in paragraph.split(' '):
word = word.strip() word = word.strip()
next_sentence = ' '.join(lines[colno][1][lineno] + [word]) next_sentence = ' '.join(lines[column_num][1][lineno] + [word])
if len(next_sentence) >= max_length: if len(next_sentence) >= max_length:
lineno += 1 lineno += 1
lines[colno][1].append([]) lines[column_num][1].append([])
lines[colno][1][lineno].append(word) lines[column_num][1][lineno].append(word)
max_lines = max(max_lines, lineno) max_lines = max(max_lines, lineno)
padding_length += self.colsize[colno]
for i in range(0, max_lines + 1): for i in range(0, max_lines + 1):
output = '' output = ''
for _, line in lines.items(): for column_num, line in lines.items():
width, bag_of_words = line width, bag_of_words = line
if len(bag_of_words) > i: if len(bag_of_words) > i:
sentence = ' '.join(bag_of_words[i]) sentence = ' '.join(bag_of_words[i])
output += sentence + ' ' * (width[1] - len(sentence))
if column_num in self.text_to_right:
output += ' ' * (width - len(sentence) - 1) + sentence + ' '
else: else:
output += ' ' * width[1] output += sentence + ' ' * (width - len(sentence))
else:
output += ' ' * width
sys.stdout.write(output + '\n') sys.stdout.write(output + '\n')