big tabs vs spaces adjust

This commit is contained in:
fanyx 2023-01-13 00:27:41 +01:00
parent b83bfe5687
commit 1f2c44c82d
1 changed files with 150 additions and 146 deletions

296
build.py
View File

@ -18,190 +18,194 @@ DELIMITER = "| :|"
# split strings longer than length into a list # split strings longer than length into a list
# strings are split at last index of delim # strings are split at last index of delim
def split_to_list(str, length, delim=' '): def split_to_list(str, length, delim=' '):
l = [] l = []
while len(str) > length: while len(str) > length:
sep = str.rfind(delim, 0, length) sep = str.rfind(delim, 0, length)
l.append(str[0:sep]) l.append(str[0:sep])
str = str[sep+1:] str = str[sep+1:]
l.append(str)
return l l.append(str)
return l
# build_header using section title # build_header using section title
def build_header(l_title): def build_header(l_title, add_index=false, index_prefix=None):
header = [] header = []
len_title = len(l_title[0]) len_title = len(l_title[0])
pad, pad_left = divmod(MAX_WIDTH - (len_title + (TITLE_PADDING * 2)) - 2, 2) pad, pad_left = divmod(MAX_WIDTH - (len_title + (TITLE_PADDING * 2)) - 2, 2)
len_border = len_title + ((TITLE_PADDING * 2) - 2) len_border = len_title + ((TITLE_PADDING * 2) - 2)
# upper part
header.append(' ' * (pad + pad_left + 2) + '_' * len_border + ' ' * (pad + 2))
header.append('_' * (pad + pad_left) + '//' + ' ' * len_border + '\\\\' + '_' * pad)
# ' ' padded Title
count = 1
for title in l_title:
c_pad = '_' if count == len(l_title) else ' '
pad_right = len_title - len(title)
header.append(c_pad * (pad + pad_left) + '|' + ' ' * TITLE_PADDING + title + ' ' * (TITLE_PADDING + pad_right) + '|' + c_pad * pad)
count += 1
# lower part
header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * pad)
header.append(' ' * MAX_WIDTH)
return header # upper part
header.append(' ' * (pad + pad_left + 2) + '_' * len_border + ' ' * (pad + 2))
header.append('_' * (pad + pad_left) + '//' + ' ' * len_border + '\\\\' + '_' * pad)
# ' ' padded Title
count = 1
for title in l_title:
c_pad = '_' if count == len(l_title) else ' '
pad_right = len_title - len(title)
header.append(c_pad * (pad + pad_left) + '|' + ' ' * TITLE_PADDING + title + ' ' * (TITLE_PADDING + pad_right) + '|' + c_pad * pad)
count += 1
# lower part
header.append(' ' * (pad + pad_left) + '\\\\' + '_' * len_border + '//' + ' ' * pad)
# add navigation tag
if add_index == true and index_prefix.length == 1:
pass
else:
header.append(' ' * MAX_WIDTH)
return header
def build_index(yaml, prefix): def build_index(yaml, prefix):
l_title = [] l_title = []
for item in yaml: for item in yaml:
l_title += [item['title']] l_title += [item['title']]
l_index = [] l_index = []
for i, item in enumerate(l_title): for i, item in enumerate(l_title):
l_index += [' - ' + item.ljust(MAX_WIDTH - 10) + f'[{prefix}{i+1:03}] '] l_index += [' - ' + item.ljust(MAX_WIDTH - 10) + f'[{prefix}{i+1:03}] ']
l_index += [' ' * MAX_WIDTH] l_index += [' ' * MAX_WIDTH]
return l_index return l_index
def build_game(name, platform): def build_game(name, platform):
s_platform = platform.ljust(MAX_WIDTH_PLATFORM) s_platform = platform.ljust(MAX_WIDTH_PLATFORM)
s_name = name.ljust(MAX_WIDTH_NAME) s_name = name.ljust(MAX_WIDTH_NAME)
return f' {s_platform} | {s_name} ' return f' {s_platform} | {s_name} '
# #
# main # main
# #
def main(): def main():
print('go') print('go')
with open(f'{BASE}/todo.yaml', 'r') as file: with open(f'{BASE}/todo.yaml', 'r') as file:
todo_yaml = yaml.safe_load(file) todo_yaml = yaml.safe_load(file)
with open(f'{BASE}/finished.yaml', 'r') as file: with open(f'{BASE}/finished.yaml', 'r') as file:
finished_yaml = yaml.safe_load(file) finished_yaml = yaml.safe_load(file)
# #
# Section: TO-DO # Section: TO-DO
# #
todo_result = []
todo_result += build_header([TITLE_LEFT]) todo_result = []
todo_result += build_index(todo_yaml, 'S') todo_result += build_header([TITLE_LEFT])
for section in todo_yaml: todo_result += build_index(todo_yaml, 'S')
s_title = section['title']
l_title = []
if len(s_title) > MAX_WIDTH_TITLE: for section in todo_yaml:
l_title = split_to_list(s_title, MAX_WIDTH_TITLE) s_title = section['title']
else: l_title = []
l_title = [s_title]
# Create section header
todo_result += build_header(l_title)
# Create table header
todo_result += [' ______ ________________________________________________________',
'|======|========================================================']
# Add games one by one if len(s_title) > MAX_WIDTH_TITLE:
# Prefix : | l_title = split_to_list(s_title, MAX_WIDTH_TITLE)
for game in section['games']: else:
s_name = game['name'] l_title = [s_title]
s_platform = game['platform']
if len(s_platform) > MAX_WIDTH_PLATFORM: # Create section header
raise Exception(f"Platform shortcode for {s_name} is longer than 4 characters. Cannot parse...") todo_result += build_header(l_title)
if len(s_name) > MAX_WIDTH_NAME:
l_name = split_to_list(s_name, MAX_WIDTH_NAME)
for i,i_name in enumerate(l_name):
todo_result += ['|' + build_game(i_name, s_platform if i == 0 else '')]
else:
todo_result += ['|' + build_game(s_name, s_platform)]
# Close table header # Create table header
todo_result += ['|______|________________________________________________________', todo_result += [' ______ ________________________________________________________',
' ' * MAX_WIDTH] '|======|========================================================']
#
# Section: Finished
#
finished_result = [] # Add games one by one
# Prefix : |
for game in section['games']:
s_name = game['name']
s_platform = game['platform']
finished_result += build_header([TITLE_RIGHT]) if len(s_platform) > MAX_WIDTH_PLATFORM:
raise Exception(f"Platform shortcode for {s_name} is longer than 4 characters. Cannot parse...")
finished_result += build_index(finished_yaml, 'P') if len(s_name) > MAX_WIDTH_NAME:
l_name = split_to_list(s_name, MAX_WIDTH_NAME)
for i,i_name in enumerate(l_name):
todo_result += ['|' + build_game(i_name, s_platform if i == 0 else '')]
else:
todo_result += ['|' + build_game(s_name, s_platform)]
for section in finished_yaml: # Close table header
s_title = section['title'] todo_result += ['|______|________________________________________________________',
l_title = [] ' ' * MAX_WIDTH]
if len(s_title) > MAX_WIDTH_TITLE: #
l_title = split_to_list(s_title, MAX_WIDTH_TITLE) # Section: Finished
else: #
l_title = [s_title]
# Create section header
finished_result += build_header(l_title)
# Create table header
finished_result += ['______ ________________________________________________________ ',
'======|========================================================|']
# Add games one by one finished_result = []
# Suffix : |
for game in section['games']:
s_name = game['name']
s_platform = game['platform']
if len(s_platform) > MAX_WIDTH_PLATFORM: finished_result += build_header([TITLE_RIGHT])
raise Exception(f"Platform shortcode for {s_name} is longer than 4 characters. Cannot parse...")
if len(s_name) > MAX_WIDTH_NAME:
l_name = split_to_list(s_name, MAX_WIDTH_NAME)
for i,i_name in enumerate(l_name):
finished_result += [build_game(i_name, platform if i == 0 else '') + '|']
else:
finished_result += [build_game(s_name, s_platform) + '|']
# Close table header finished_result += build_index(finished_yaml, 'P')
finished_result += ['______|________________________________________________________|',
' ' * MAX_WIDTH]
# adjust length of both sides for section in finished_yaml:
len_difference = len(todo_result) - len(finished_result) s_title = section['title']
# todo > finished l_title = []
if len_difference > 0:
finished_result += [' ' * MAX_WIDTH] * len_difference
# todo < finished
elif len_difference < 0:
len_difference *= -1
todo_result += [' ' * MAX_WIDTH] * len_difference
if len(todo_result) != len(finished_result): if len(s_title) > MAX_WIDTH_TITLE:
raise Exception("Results are not of equal size. Something went wrong...") l_title = split_to_list(s_title, MAX_WIDTH_TITLE)
else:
result = [] l_title = [s_title]
i = 0
while len(todo_result) > i:
result += [todo_result[i] + DELIMITER + finished_result[i]]
i += 1
with open('list-of-game.txt', 'w') as file:
for i in result:
file.write(f"{i}\n")
print('done') # Create section header
finished_result += build_header(l_title)
return
# Create table header
finished_result += ['______ ________________________________________________________ ',
'======|========================================================|']
# Add games one by one
# Suffix : |
for game in section['games']:
s_name = game['name']
s_platform = game['platform']
if len(s_platform) > MAX_WIDTH_PLATFORM:
raise Exception(f"Platform shortcode for {s_name} is longer than 4 characters. Cannot parse...")
if len(s_name) > MAX_WIDTH_NAME:
l_name = split_to_list(s_name, MAX_WIDTH_NAME)
for i,i_name in enumerate(l_name):
finished_result += [build_game(i_name, platform if i == 0 else '') + '|']
else:
finished_result += [build_game(s_name, s_platform) + '|']
# Close table header
finished_result += ['______|________________________________________________________|',
' ' * MAX_WIDTH]
# adjust length of both sides
len_difference = len(todo_result) - len(finished_result)
# todo > finished
if len_difference > 0:
finished_result += [' ' * MAX_WIDTH] * len_difference
# todo < finished
elif len_difference < 0:
len_difference *= -1
todo_result += [' ' * MAX_WIDTH] * len_difference
if len(todo_result) != len(finished_result):
raise Exception("Results are not of equal size. Something went wrong...")
result = []
i = 0
while len(todo_result) > i:
result += [todo_result[i] + DELIMITER + finished_result[i]]
i += 1
with open('list-of-game.txt', 'w') as file:
for i in result:
file.write(f"{i}\n")
print('done')
return
if __name__ == "__main__": if __name__ == "__main__":
main() main()