Tab align to right possibility added
This commit is contained in:
@@ -43,6 +43,7 @@ BUFFER_SIZE: int = 256
|
||||
class Tab():
|
||||
bordered: bool = False
|
||||
colsize: List = []
|
||||
text_to_right: List = []
|
||||
|
||||
def __init__(self, *colsizes: int, nb_columns: int = 0, bordered: bool = False):
|
||||
self.colsize = list(colsizes)
|
||||
@@ -64,44 +65,49 @@ class Tab():
|
||||
def border(self, text: str = '-'):
|
||||
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):
|
||||
lines = {}
|
||||
max_lines = 0
|
||||
padding_length = 0
|
||||
|
||||
for colno, text in enumerate(texts[:len(self.colsize)]):
|
||||
max_length = self.colsize[colno]
|
||||
for column_num, text in enumerate(texts[:len(self.colsize)]):
|
||||
max_length = self.colsize[column_num]
|
||||
lineno = -1
|
||||
lines[colno] = ((padding_length, max_length), [])
|
||||
lines[column_num] = (max_length, [])
|
||||
|
||||
for paragraph in str(text).split('\n'):
|
||||
lineno += 1
|
||||
lines[colno][1].append([])
|
||||
lines[column_num][1].append([])
|
||||
|
||||
for word in paragraph.split(' '):
|
||||
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:
|
||||
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)
|
||||
padding_length += self.colsize[colno]
|
||||
|
||||
for i in range(0, max_lines + 1):
|
||||
output = ''
|
||||
|
||||
for _, line in lines.items():
|
||||
for column_num, 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))
|
||||
|
||||
if column_num in self.text_to_right:
|
||||
output += ' ' * (width - len(sentence) - 1) + sentence + ' '
|
||||
else:
|
||||
output += sentence + ' ' * (width - len(sentence))
|
||||
else:
|
||||
output += ' ' * width[1]
|
||||
output += ' ' * width
|
||||
|
||||
sys.stdout.write(output + '\n')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user