-
-
Notifications
You must be signed in to change notification settings - Fork 394
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)}') | ||
|
@@ -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 | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
||
|
@@ -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: | ||
|
@@ -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]}') | ||
|
@@ -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]) | ||
|
@@ -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: | ||
|
@@ -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 ========================================================= | ||
|
@@ -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): | ||
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)) | ||
|
@@ -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: | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}\'' | ||
|
||
|
||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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( | ||
|
@@ -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: | ||
|
@@ -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 ================================================================= | ||
|
@@ -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: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 withValueError
when callingint()
on non-numeric string.