Merge pull request 'Adds a fix to the readline issues we've been seeing' (#96) from readline-escape-fix into master

Reviewed-on: #96
This commit is contained in:
cmccabe 2020-07-11 17:01:19 -04:00
commit e408c3e1aa
1 changed files with 34 additions and 32 deletions

View File

@ -58,7 +58,7 @@ def print_category_details(view_cat):
# unread mark width # unread mark width
header = "\n{}\n\n {:>3s} {:>10s} {:<{namelen}s} {:<5} {:<s}".format( header = "\n{}\n\n {:>3s} {:>10s} {:<{namelen}s} {:<5} {:<s}".format(
style_text(view_cat["name"].upper(), "bold"), style_text(view_cat["name"].upper(), False, "bold"),
"ID#", "ID#",
"DATE", "DATE",
"AUTHOR", "AUTHOR",
@ -125,15 +125,15 @@ def print_thread_details(post_id) -> tuple:
) )
# post detail view # post detail view
print("\n\n{:<17}: {}".format(style_text("Title", "bold"), post_title)) print("\n\n{:<17}: {}".format(style_text("Title", False, "bold"), post_title))
print("{:<17}: {}".format(style_text("Link", "bold"), post_url)) print("{:<17}: {}".format(style_text("Link", False, "bold"), post_url))
print("{:<17}: {}".format(style_text("Category", "bold"), post_category)) print("{:<17}: {}".format(style_text("Category", flase, "bold"), post_category))
print("{:<17}: {}".format(style_text("User", "bold"), post_username)) print("{:<17}: {}".format(style_text("User", False, "bold"), post_username))
print("{:<17}: {}".format(style_text("Date", "bold"), post_datetime)) print("{:<17}: {}".format(style_text("Date", False, "bold"), post_datetime))
# post reply view # post reply view
if replies: if replies:
print("\n{}:\n".format(style_text("Replies", "underline"))) print("\n{}:\n".format(style_text("Replies", False, "underline")))
for line in replies: for line in replies:
comment_author = line[1] comment_author = line[1]
comment_date = datetime.fromtimestamp(float(line[2])).isoformat( comment_date = datetime.fromtimestamp(float(line[2])).isoformat(
@ -189,9 +189,9 @@ def search():
print_search_results(keyword, search_results) print_search_results(keyword, search_results)
option = input( option = input(
"Enter a post ID to see its thread, {} to start a new search, {} to go back, or {} to quit: ".format( "Enter a post ID to see its thread, {} to start a new search, {} to go back, or {} to quit: ".format(
style_text("s", "underline"), style_text("s", False, "underline"),
style_text("m", "underline"), style_text("m", False, "underline"),
style_text("q", "underline"), style_text("q", False, "underline"),
) )
).lower() ).lower()
if option == "q": if option == "q":
@ -207,7 +207,7 @@ def search():
raise IndexError("Invalid post ID") raise IndexError("Invalid post ID")
except (KeyError, ValueError, IndexError): except (KeyError, ValueError, IndexError):
# Catch a Post ID that is not in the thread list or is not a number # Catch a Post ID that is not in the thread list or is not a number
print("{}".format(style_text("Invalid entry", "bold"))) print("{}".format(style_text("Invalid entry", False, "bold")))
def view_link_in_browser(url): def view_link_in_browser(url):
@ -278,7 +278,7 @@ def get_input(item: str) -> str:
"""Get user input with the specified prompt, validate and return it, or """Get user input with the specified prompt, validate and return it, or
break if invalid""" break if invalid"""
while True: while True:
i: str = input("{}: ".format(style_text(item, "underline"))) i: str = input("{}: ".format(style_text(item, True, "underline")))
if i == "": if i == "":
raise ValueError("Empty field") raise ValueError("Empty field")
if i == "?": if i == "?":
@ -305,7 +305,7 @@ def post_link() -> int:
print("\nPost a link by entering the details below.") print("\nPost a link by entering the details below.")
print( print(
"Enter {} for a list of categories. Enter an empty field to cancel.\n".format( "Enter {} for a list of categories. Enter an empty field to cancel.\n".format(
style_text("?", "underline") style_text("?", False, "underline")
) )
) )
@ -336,9 +336,9 @@ def menu_view_categories():
option = input( option = input(
"Enter a category ID, {} to post a link, {} to search, or {} to quit: ".format( "Enter a category ID, {} to post a link, {} to search, or {} to quit: ".format(
style_text("p", "underline"), style_text("p", True, "underline"),
style_text("s", "underline"), style_text("s", True, "underline"),
style_text("q", "underline"), style_text("q", True, "underline"),
) )
).lower() ).lower()
@ -369,10 +369,10 @@ def menu_view_category_details(cat_index):
option = input( option = input(
"Enter a post ID to see its thread, {} to go back, {} to " "Enter a post ID to see its thread, {} to go back, {} to "
"search, {} to post a link, or {} to quit: ".format( "search, {} to post a link, or {} to quit: ".format(
style_text("m", "underline"), style_text("m", True, "underline"),
style_text("s", "underline"), style_text("s", True, "underline"),
style_text("p", "underline"), style_text("p", True, "underline"),
style_text("q", "underline"), style_text("q", True, "underline"),
) )
).lower() ).lower()
@ -393,19 +393,19 @@ def menu_view_category_details(cat_index):
menu_view_thread_details(post_id) menu_view_thread_details(post_id)
except (KeyError, ValueError): except (KeyError, ValueError):
# Catch a Post ID that is not in the thread list or is not a number # Catch a Post ID that is not in the thread list or is not a number
print("{}\n\n".format(style_text("Invalid category ID/entry", "bold"))) print("{}\n\n".format(style_text("Invalid category ID/entry", False, "bold")))
def menu_view_thread_details(post_id): def menu_view_thread_details(post_id):
"""Displays thread details, handles related navigation menu""" """Displays thread details, handles related navigation menu"""
option_text = "Type {} to reply, {} to view in {}, {} to search, {} to post a new link, {} to go back, or {} to quit: ".format( option_text = "Type {} to reply, {} to view in {}, {} to search, {} to post a new link, {} to go back, or {} to quit: ".format(
style_text("r", "underline"), style_text("r", True, "underline"),
style_text("b", "underline"), style_text("b", True, "underline"),
config.USER.browser, config.USER.browser,
style_text("s", "underline"), style_text("s", True, "underline"),
style_text("p", "underline"), style_text("p", True, "underline"),
style_text("m", "underline"), style_text("m", True, "underline"),
style_text("q", "underline"), style_text("q", True, "underline"),
) )
while True: while True:
@ -429,13 +429,13 @@ def menu_view_thread_details(post_id):
break break
if option == "q": if option == "q":
graceful_exit() graceful_exit()
print("{}\n\n".format(style_text("Invalid entry", "bold"))) print("{}\n\n".format(style_text("Invalid entry", False, "bold")))
## GENERAL ## GENERAL
def style_text(text: str, *args) -> str: def style_text(text: str, is_input: bool, *args) -> str:
"""Style input strings as specified using terminal escape sequences. Returns a styled string""" """Style input strings as specified using terminal escape sequences. Returns a styled string"""
styles = { styles = {
"bold": "\033[1m", "bold": "\033[1m",
@ -444,15 +444,17 @@ def style_text(text: str, *args) -> str:
"blink": "\033[5m", # This is here if you REALLY need it...dont use it. "blink": "\033[5m", # This is here if you REALLY need it...dont use it.
# (ctrl+shift+esc+E to enable evil mode.) # (ctrl+shift+esc+E to enable evil mode.)
"inverse": "\033[7m", # Make fg and bg color swap "inverse": "\033[7m", # Make fg and bg color swap
"rl_left_fix": "\001" if is_input else "",
"rl_right_fix": "\002" if is_input else ""
} }
out = "" out = ""
for arg in args: for arg in args:
if arg in styles: if arg in styles:
out += "\001" out += styles["rl_left_fix"]
out += styles[arg] out += styles[arg]
out += "\002" out += styles["rl_right_fix"]
out += text out += text
out += "\001\033[0m\002" out += "{}\033[0m{}".format(styles["rl_left_fix"], styles["rl_right_fix"])
return out return out