Skip to content

Commit

Permalink
better config design
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Feb 29, 2024
1 parent 75c0817 commit c618111
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
37 changes: 21 additions & 16 deletions cat_win/persistence/cconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, working_dir: str) -> None:
self.color_dic = {}

self.longest_char_count = 30
self.rows = 3
self.columns = 3

def load_config(self) -> dict:
"""
Expand Down Expand Up @@ -112,19 +112,21 @@ def _print_get_all_available_colors(self) -> list:

for index, fore_option in enumerate(fore_options):
key, value = fore_option
colored_option = f"{value}Fore.{key}{ColorOptions.Style['RESET']}"
config_menu += f"{index+1: <{index_offset}}: "
config_menu += f"{colored_option: <{self.longest_char_count+len(value)}}"
if index % self.rows == self.rows-1:
f_key = f"Fore.{key}"
config_menu += f"{index+1: <{index_offset}}: {value}"
config_menu += f"{f_key: <{self.longest_char_count}}"
config_menu += f"{ColorOptions.Style['RESET']} "
if index % self.columns == self.columns-1:
config_menu += '\n'
options.append('Fore.' + key)
config_menu += '\n'
for index, back_option in enumerate(back_options):
key, value = back_option
colored_option = f"{value}Back.{key}{ColorOptions.Style['RESET']}"
config_menu += f"{len(fore_options)+index+1: <{index_offset}}: "
config_menu += f"{colored_option: <{self.longest_char_count+len(value)}}"
if index % self.rows == self.rows-1:
b_key = f"Back.{key}"
config_menu += f"{len(fore_options)+index+1: <{index_offset}}: {value}"
config_menu += f"{b_key: <{self.longest_char_count}}"
config_menu += f"{ColorOptions.Style['RESET']} "
if index % self.columns == self.columns-1:
config_menu += '\n'
options.append('Back.' + key)
config_menu += '\n'
Expand All @@ -138,16 +140,19 @@ def _print_all_available_elements(self) -> None:
"""
print('Here is a list of all available elements you may change:')

self.longest_char_count = max(map(len, self.elements)) + 12
h_width, _ = os.get_terminal_size()
index_offset = len(str(len(self.elements) + 1))

self.longest_char_count = max(map(len, self.elements))
self.longest_char_count+= max(
(h_width - self.columns * (index_offset+3 + self.longest_char_count)) // self.columns,
1
)
config_menu = ''
for index, element in enumerate(self.elements):
colored_element = f"{self.color_dic[element]}{element}{ColorOptions.Style['RESET']}"
config_menu += f"{index+1: <{index_offset}}: "
offset = self.longest_char_count+len(self.color_dic[element])
config_menu += f"{colored_element: <{offset}}"
if index % self.rows == self.rows-1:
config_menu += f"{index+1: <{index_offset}}: {self.color_dic[element]}"
config_menu += f"{element: <{self.longest_char_count}}"
config_menu += f"{ColorOptions.Style['RESET']} "
if index % self.columns == self.columns-1:
config_menu += '\n'

print(config_menu)
Expand Down
11 changes: 8 additions & 3 deletions cat_win/persistence/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, working_dir: str) -> None:
self.const_dic = {}

self.longest_char_count = 30
self.rows = 3
self.columns = 3

@staticmethod
def convert_config_element(element: str, element_type: type):
Expand Down Expand Up @@ -141,14 +141,19 @@ def _print_all_available_elements(self) -> None:
"""
print('Here is a list of all available elements you may change:')

self.longest_char_count = max(map(len, self.elements)) + 12
h_width, _ = os.get_terminal_size()
index_offset = len(str(len(self.elements) + 1))
self.longest_char_count = max(map(len, self.elements))
self.longest_char_count+= max(
(h_width - self.columns * (index_offset+3 + self.longest_char_count)) // self.columns,
1
)

config_menu = ''
for index, element in enumerate(self.elements):
config_menu += f"{index+1: <{index_offset}}: "
config_menu += f"{element: <{self.longest_char_count}}"
if index % self.rows == self.rows-1:
if index % self.columns == self.columns-1:
config_menu += '\n'

print(config_menu)
Expand Down
1 change: 1 addition & 0 deletions cat_win/tests/test_cconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
config.load_config()


@patch('os.get_terminal_size', lambda: (120, 30))
class TestCConfig(TestCase):
maxDiff = None

Expand Down
1 change: 1 addition & 0 deletions cat_win/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
config.load_config()


@patch('os.get_terminal_size', lambda: (120, 30))
class TestConfig(TestCase):
maxDiff = None

Expand Down

0 comments on commit c618111

Please sign in to comment.