Skip to content

improve code readability #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions tagstudio/src/cli/ts_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def get_char_limit(self, text: str) -> int:
# print(f'Char Limit: {char_limit}, Len: {len(text)}')
return char_limit

def truncate_text(self, text: str) -> int:
def truncate_text(self, text: str) -> str:
"""Returns a truncated string for displaying, calculated with `get_char_limit()`."""
if len(text) > self.get_char_limit(text):
# print(f'Char Limit: {self.get_char_limit(text)}, Len: {len(text)}')
Expand Down Expand Up @@ -761,7 +761,7 @@ def print_columns(self, content: list[object], add_enum: False) -> None:
offset = 1 if (index >= row_count) and (
row_number != row_count) else 0
elif displayable % table_size != 0:
if col_num > 1 and col_num <= displayable % table_size:
if 1 < col_num <= displayable % table_size:
offset += col_num - 1
elif col_num > 1 and col_num > displayable % table_size:
offset = displayable % table_size
Expand Down Expand Up @@ -1022,30 +1022,31 @@ def global_commands(self, com:list[str]) -> tuple[bool,str]:
"""
was_executed:bool = False
message:str = ''
com_name = com[0].lower()

# Backup Library =======================================================
if (com[0].lower() == 'backup'):
if com_name == 'backup':
self.backup_library(display_message=False)
was_executed = True
message=f'{INFO} Backed up Library to disk.'
# Create Collage =======================================================
elif (com[0].lower() == 'collage'):
elif com_name == 'collage':
filename = self.create_collage()
if filename:
was_executed = True
message = f'{INFO} Saved collage to \"{filename}\".'
# Save Library =========================================================
elif (com[0].lower() == 'save' or com[0].lower() == 'write' or com[0].lower() == 'w'):
elif com_name in ('save', 'write', 'w'):
self.save_library(display_message=False)
was_executed = True
message=f'{INFO} Library saved to disk.'
# Toggle Debug =========================================================
elif (com[0].lower() == 'toggle-debug'):
elif com_name == 'toggle-debug':
self.args.debug = not self.args.debug
was_executed = True
message=f'{INFO} Debug Mode Active.' if self.args.debug else f'{INFO} Debug Mode Deactivated.'
# Toggle External Preview ==============================================
elif (com[0].lower() == 'toggle-external-preview'):
elif com_name == 'toggle-external-preview':
self.args.external_preview = not self.args.external_preview
if self.args.external_preview:
self.init_external_preview()
Expand All @@ -1054,11 +1055,11 @@ def global_commands(self, com:list[str]) -> tuple[bool,str]:
was_executed = True
message=f'{INFO} External Preview Enabled.' if self.args.external_preview else f'{INFO} External Preview Disabled.'
# Quit =================================================================
elif com[0].lower() == 'quit' or com[0].lower() == 'q':
elif com_name in ('quit', 'q'):
self.exit(save=True, backup=False)
was_executed = True
# Quit without Saving ==================================================
elif com[0].lower() == 'quit!' or com[0].lower() == 'q!':
elif com_name in ('quit!', 'q!'):
self.exit(save=False, backup=False)
was_executed = True

Expand Down Expand Up @@ -1345,7 +1346,7 @@ def scr_library_home(self, clear_scr=True):
# self.scr_library_home(clear_scr=False)
# Add New Entries ==================================================
elif ' '.join(com) == 'add new':
if self.is_new_file_count_init == False:
if not self.is_new_file_count_init:
print(
f'{INFO} Scanning for files in \'{self.lib.library_dir}\' (This may take a while)...')
# if not self.lib.files_not_in_library:
Expand Down Expand Up @@ -1390,7 +1391,7 @@ def scr_library_home(self, clear_scr=True):
for unresolved in self.lib.missing_matches:
res = self.scr_choose_missing_match(
self.lib.get_entry_id_from_filepath(unresolved), clear_scr=False)
if res != None and int(res) >= 0:
if res is not None and int(res) >= 0:
clear()
print(
f'{INFO} Updated {self.lib.entries[self.lib.get_entry_id_from_filepath(unresolved)].path} -> {self.lib.missing_matches[unresolved][res]}')
Expand Down Expand Up @@ -1555,7 +1556,7 @@ def scr_browse_entries_gallery(self, index, clear_scr=True, refresh=True):

print(self.format_title(title))

if len(self.filtered_entries) > 0:
if self.filtered_entries:
# entry = self.lib.get_entry_from_index(
# self.filtered_entries[index])
entry = self.lib.get_entry(self.filtered_entries[index][1])
Expand All @@ -1580,7 +1581,7 @@ def scr_browse_entries_gallery(self, index, clear_scr=True, refresh=True):

self.print_fields(self.filtered_entries[index][1])
else:
if len(self.lib.entries) > 0:
if self.lib.entries:
print(self.format_h1('No Entry Results for Query', color=BRIGHT_RED_FG))
self.set_external_preview_default()
else:
Expand Down Expand Up @@ -2049,14 +2050,15 @@ def scr_choose_option(self, subtitle: str, choices: list, prompt: str = '', requ
'<#> Quit', BRIGHT_CYAN_FG))
print('> ', end='')

com: list[str] = input().lstrip().rstrip().split(' ')
com: list[str] = input().strip().split(' ')
gc, message = self.global_commands(com)
if gc:
if message:
clear()
print(message)
clear_scr=False
else:
com_name = com[0].lower()

try:
# # Quit =========================================================
Expand All @@ -2069,13 +2071,13 @@ def scr_choose_option(self, subtitle: str, choices: list, prompt: str = '', requ
# # self.cleanup()
# sys.exit()
# Cancel =======================================================
if (com[0].lower() == 'cancel' or com[0].lower() == 'c' or com[0] == '0') and required==False:
if com_name in ('cancel', 'c', '0') and not required:
clear()
return -1
# Selection ====================================================
elif int(com[0]) > 0 and int(com[0]) <= len(choices):
elif com_name.isdigit() and 0 < int(com_name) <= len(choices):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I added condition .isdigit(), otherwise it could end up with ValueError when calling int() on non-numeric string.

clear()
return int(com[0]) - 1
return int(com_name) - 1
else:
# invalid_input = True
# print(self.format_h1(str='Please Enter a Valid Selection Number', color=BRIGHT_RED_FG))
Expand Down Expand Up @@ -2554,7 +2556,7 @@ def scr_select_field_templates(self, field_ids: list[int], allow_multiple=True,
f'Enter #{plural} Cancel', fg_color))
print('> ', end='')

com: list[int] = input().split(' ')
com: list[str] = input().split(' ')
selected_ids: list[int] = []
try:
for c in com:
Expand Down Expand Up @@ -2625,14 +2627,14 @@ def scr_edit_entry_text(self, entry_index, field_index, allow_newlines=True, cle
self.lib.update_entry_field(
entry_index, field_index, new_content.rstrip('\n').rstrip('\r'), 'replace')

def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True) -> None:
def scr_list_tags(self, query: str = '', tag_ids: list[int] = None, clear_scr=True) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the empty list as a default parameter isnt intentional, then it might be causing some issues, see https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

"""A screen for listing out and performing CRUD operations on Library Tags."""
# NOTE: While a screen that just displays the first 40 or so random tags on your screen
# isn't really that useful, this is just a temporary measure to provide a launchpad
# screen for necessary commands such as adding and editing tags.
# A more useful screen presentation might look like a list of ranked occurrences, but
# that can be figured out and implemented later.

tag_ids = tag_ids or []
title = f'{self.base_title} - Library \'{self.lib.library_dir}\''


Expand Down Expand Up @@ -2673,17 +2675,17 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
'Create Edit <#> Delete <#> Search <Query> Close/Done', BRIGHT_MAGENTA_FG))
print('> ', end='')

com: list[str] = input().lstrip().rstrip().split(' ')
com: list[str] = input().strip().split(' ')
gc, message = self.global_commands(com)
if gc:
if message:
clear()
print(message)
clear_scr=False
else:

com_name = com[0].lower()
# Search Tags ==========================================================
if (com[0].lower() == 'search' or com[0].lower() == 's'):
if com_name in ('search', 's'):
if len(com) > 1:
new_query: str = ' '.join(com[1:])
# self.scr_list_tags(prev_scr, query=new_query,
Expand All @@ -2696,7 +2698,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
tag_ids=self.lib.search_tags('')
# return
# Edit Tag ===========================================================
elif com[0].lower() == 'edit' or com[0].lower() == 'e':
elif com_name in ('edit', 'e'):
if len(com) > 1:
try:
index = int(com[1]) - 1
Expand All @@ -2720,7 +2722,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
# return

# Create Tag ============================================================
elif com[0].lower() == 'create' or com[0].lower() == 'mk':
elif com_name in ('create', 'mk'):
tag = Tag(id=0, name='New Tag', shorthand='',
aliases=[], subtags_ids=[], color='')
self.scr_manage_tag(
Expand All @@ -2731,7 +2733,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
# self.scr_list_tags(prev_scr, query=query, tag_ids=tag_ids)
# return
# Delete Tag ===========================================================
elif com[0].lower() == 'delete' or com[0].lower() == 'del':
elif com_name in ('delete', 'del'):
if len(com) > 1:
if len(com) > 1:
try:
Expand All @@ -2757,7 +2759,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
# tag_ids=tag_ids, clear_scr=False)
# return
# Close View ===========================================================
elif (com[0].lower() == 'close' or com[0].lower() == 'c' or com[0].lower() == 'done'):
elif com_name in ('close', 'c', 'done'):
# prev_scr()
return
# # Quit =================================================================
Expand Down Expand Up @@ -3192,7 +3194,7 @@ def scr_tag_color_dropdown(self, fallback: str, colors: list[str], clear_scr=Tru

selected: str = input()
try:
if int(selected) > 0 and int(selected) <= len(colors):
if selected.isdigit() and 0 < int(selected) <= len(colors):
selected = colors[int(selected)-1]
return selected
# except SystemExit:
Expand Down
Loading