Skip to content

Commit d5d9cd3

Browse files
committed
improve code readability
1 parent 31f4022 commit d5d9cd3

File tree

4 files changed

+77
-105
lines changed

4 files changed

+77
-105
lines changed

tagstudio/src/cli/ts_cli.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def get_char_limit(self, text: str) -> int:
493493
# print(f'Char Limit: {char_limit}, Len: {len(text)}')
494494
return char_limit
495495

496-
def truncate_text(self, text: str) -> int:
496+
def truncate_text(self, text: str) -> str:
497497
"""Returns a truncated string for displaying, calculated with `get_char_limit()`."""
498498
if len(text) > self.get_char_limit(text):
499499
# 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:
761761
offset = 1 if (index >= row_count) and (
762762
row_number != row_count) else 0
763763
elif displayable % table_size != 0:
764-
if col_num > 1 and col_num <= displayable % table_size:
764+
if 1 < col_num <= displayable % table_size:
765765
offset += col_num - 1
766766
elif col_num > 1 and col_num > displayable % table_size:
767767
offset = displayable % table_size
@@ -1022,30 +1022,31 @@ def global_commands(self, com:list[str]) -> tuple[bool,str]:
10221022
"""
10231023
was_executed:bool = False
10241024
message:str = ''
1025+
com_name = com[0].lower()
10251026

10261027
# Backup Library =======================================================
1027-
if (com[0].lower() == 'backup'):
1028+
if com_name == 'backup':
10281029
self.backup_library(display_message=False)
10291030
was_executed = True
10301031
message=f'{INFO} Backed up Library to disk.'
10311032
# Create Collage =======================================================
1032-
elif (com[0].lower() == 'collage'):
1033+
elif com_name == 'collage':
10331034
filename = self.create_collage()
10341035
if filename:
10351036
was_executed = True
10361037
message = f'{INFO} Saved collage to \"{filename}\".'
10371038
# Save Library =========================================================
1038-
elif (com[0].lower() == 'save' or com[0].lower() == 'write' or com[0].lower() == 'w'):
1039+
elif com_name in ('save', 'write', 'w'):
10391040
self.save_library(display_message=False)
10401041
was_executed = True
10411042
message=f'{INFO} Library saved to disk.'
10421043
# Toggle Debug =========================================================
1043-
elif (com[0].lower() == 'toggle-debug'):
1044+
elif com_name == 'toggle-debug':
10441045
self.args.debug = not self.args.debug
10451046
was_executed = True
10461047
message=f'{INFO} Debug Mode Active.' if self.args.debug else f'{INFO} Debug Mode Deactivated.'
10471048
# Toggle External Preview ==============================================
1048-
elif (com[0].lower() == 'toggle-external-preview'):
1049+
elif com_name == 'toggle-external-preview':
10491050
self.args.external_preview = not self.args.external_preview
10501051
if self.args.external_preview:
10511052
self.init_external_preview()
@@ -1054,11 +1055,11 @@ def global_commands(self, com:list[str]) -> tuple[bool,str]:
10541055
was_executed = True
10551056
message=f'{INFO} External Preview Enabled.' if self.args.external_preview else f'{INFO} External Preview Disabled.'
10561057
# Quit =================================================================
1057-
elif com[0].lower() == 'quit' or com[0].lower() == 'q':
1058+
elif com_name in ('quit', 'q'):
10581059
self.exit(save=True, backup=False)
10591060
was_executed = True
10601061
# Quit without Saving ==================================================
1061-
elif com[0].lower() == 'quit!' or com[0].lower() == 'q!':
1062+
elif com_name in ('quit!', 'q!'):
10621063
self.exit(save=False, backup=False)
10631064
was_executed = True
10641065

@@ -1345,7 +1346,7 @@ def scr_library_home(self, clear_scr=True):
13451346
# self.scr_library_home(clear_scr=False)
13461347
# Add New Entries ==================================================
13471348
elif ' '.join(com) == 'add new':
1348-
if self.is_new_file_count_init == False:
1349+
if not self.is_new_file_count_init:
13491350
print(
13501351
f'{INFO} Scanning for files in \'{self.lib.library_dir}\' (This may take a while)...')
13511352
# if not self.lib.files_not_in_library:
@@ -1390,7 +1391,7 @@ def scr_library_home(self, clear_scr=True):
13901391
for unresolved in self.lib.missing_matches:
13911392
res = self.scr_choose_missing_match(
13921393
self.lib.get_entry_id_from_filepath(unresolved), clear_scr=False)
1393-
if res != None and int(res) >= 0:
1394+
if res is not None and int(res) >= 0:
13941395
clear()
13951396
print(
13961397
f'{INFO} Updated {self.lib.entries[self.lib.get_entry_id_from_filepath(unresolved)].path} -> {self.lib.missing_matches[unresolved][res]}')
@@ -2049,14 +2050,15 @@ def scr_choose_option(self, subtitle: str, choices: list, prompt: str = '', requ
20492050
'<#> Quit', BRIGHT_CYAN_FG))
20502051
print('> ', end='')
20512052

2052-
com: list[str] = input().lstrip().rstrip().split(' ')
2053+
com: list[str] = input().strip().split(' ')
20532054
gc, message = self.global_commands(com)
20542055
if gc:
20552056
if message:
20562057
clear()
20572058
print(message)
20582059
clear_scr=False
20592060
else:
2061+
com_name = com[0].lower()
20602062

20612063
try:
20622064
# # Quit =========================================================
@@ -2069,13 +2071,13 @@ def scr_choose_option(self, subtitle: str, choices: list, prompt: str = '', requ
20692071
# # self.cleanup()
20702072
# sys.exit()
20712073
# Cancel =======================================================
2072-
if (com[0].lower() == 'cancel' or com[0].lower() == 'c' or com[0] == '0') and required==False:
2074+
if com_name in ('cancel', 'c', '0') and not required:
20732075
clear()
20742076
return -1
20752077
# Selection ====================================================
2076-
elif int(com[0]) > 0 and int(com[0]) <= len(choices):
2078+
elif com_name.isdigit() and 0 < int(com_name) <= len(choices):
20772079
clear()
2078-
return int(com[0]) - 1
2080+
return int(com_name) - 1
20792081
else:
20802082
# invalid_input = True
20812083
# 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,
25542556
f'Enter #{plural} Cancel', fg_color))
25552557
print('> ', end='')
25562558

2557-
com: list[int] = input().split(' ')
2559+
com: list[str] = input().split(' ')
25582560
selected_ids: list[int] = []
25592561
try:
25602562
for c in com:
@@ -2625,14 +2627,14 @@ def scr_edit_entry_text(self, entry_index, field_index, allow_newlines=True, cle
26252627
self.lib.update_entry_field(
26262628
entry_index, field_index, new_content.rstrip('\n').rstrip('\r'), 'replace')
26272629

2628-
def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True) -> None:
2630+
def scr_list_tags(self, query: str = '', tag_ids: list[int] = None, clear_scr=True) -> None:
26292631
"""A screen for listing out and performing CRUD operations on Library Tags."""
26302632
# NOTE: While a screen that just displays the first 40 or so random tags on your screen
26312633
# isn't really that useful, this is just a temporary measure to provide a launchpad
26322634
# screen for necessary commands such as adding and editing tags.
26332635
# A more useful screen presentation might look like a list of ranked occurrences, but
26342636
# that can be figured out and implemented later.
2635-
2637+
tag_ids = tag_ids or []
26362638
title = f'{self.base_title} - Library \'{self.lib.library_dir}\''
26372639

26382640

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

2676-
com: list[str] = input().lstrip().rstrip().split(' ')
2678+
com: list[str] = input().strip().split(' ')
26772679
gc, message = self.global_commands(com)
26782680
if gc:
26792681
if message:
26802682
clear()
26812683
print(message)
26822684
clear_scr=False
26832685
else:
2684-
2686+
com_name = com[0].lower()
26852687
# Search Tags ==========================================================
2686-
if (com[0].lower() == 'search' or com[0].lower() == 's'):
2688+
if com_name in ('search', 's'):
26872689
if len(com) > 1:
26882690
new_query: str = ' '.join(com[1:])
26892691
# 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
26962698
tag_ids=self.lib.search_tags('')
26972699
# return
26982700
# Edit Tag ===========================================================
2699-
elif com[0].lower() == 'edit' or com[0].lower() == 'e':
2701+
elif com_name in ('edit', 'e'):
27002702
if len(com) > 1:
27012703
try:
27022704
index = int(com[1]) - 1
@@ -2720,7 +2722,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
27202722
# return
27212723

27222724
# Create Tag ============================================================
2723-
elif com[0].lower() == 'create' or com[0].lower() == 'mk':
2725+
elif com_name in ('create', 'mk'):
27242726
tag = Tag(id=0, name='New Tag', shorthand='',
27252727
aliases=[], subtags_ids=[], color='')
27262728
self.scr_manage_tag(
@@ -2731,7 +2733,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
27312733
# self.scr_list_tags(prev_scr, query=query, tag_ids=tag_ids)
27322734
# return
27332735
# Delete Tag ===========================================================
2734-
elif com[0].lower() == 'delete' or com[0].lower() == 'del':
2736+
elif com_name in ('delete', 'del'):
27352737
if len(com) > 1:
27362738
if len(com) > 1:
27372739
try:
@@ -2757,7 +2759,7 @@ def scr_list_tags(self, query: str = '', tag_ids: list[int] = [], clear_scr=True
27572759
# tag_ids=tag_ids, clear_scr=False)
27582760
# return
27592761
# Close View ===========================================================
2760-
elif (com[0].lower() == 'close' or com[0].lower() == 'c' or com[0].lower() == 'done'):
2762+
elif com_name in ('close', 'c', 'done'):
27612763
# prev_scr()
27622764
return
27632765
# # Quit =================================================================
@@ -3192,7 +3194,7 @@ def scr_tag_color_dropdown(self, fallback: str, colors: list[str], clear_scr=Tru
31923194

31933195
selected: str = input()
31943196
try:
3195-
if int(selected) > 0 and int(selected) <= len(colors):
3197+
if selected.isdigit() and 0 < int(selected) <= len(colors):
31963198
selected = colors[int(selected)-1]
31973199
return selected
31983200
# except SystemExit:

0 commit comments

Comments
 (0)