Merge pull request 'Add readline functionality, modified input calls and updated styles' (#90) from readline_again into master

This commit is contained in:
cmccabe 2020-05-20 06:25:19 -04:00
commit 1433aabac2
2 changed files with 17 additions and 8 deletions

View File

@ -4,6 +4,7 @@
## If this script contains bugs, blame cmccabe.
import getpass
import readline # pylint: disable=unused-import
import signal
import subprocess
import sys
@ -130,6 +131,8 @@ def print_thread_details(post_id) -> tuple:
else:
print("\nNo replies yet. Be the first!")
print("")
# return data used by menu control
return parent_id, post_url
@ -147,6 +150,7 @@ def print_search_results(keyword: str, search_results: list):
author = record[1]
desc = record[6]
print("{:4d} {:<15s}{:<12s}{:<13s}".format(display_index, date, author, desc))
print("")
## CONTROLS
@ -155,7 +159,8 @@ def print_search_results(keyword: str, search_results: list):
def search():
"""Control for the search function"""
while True:
keyword = input("\nEnter your search (or leave empty to cancel):\n")
print("")
keyword = input("Enter your search (or leave empty to cancel): ")
if keyword == "":
print("Search cancelled\n")
return
@ -166,7 +171,7 @@ def search():
while True:
print_search_results(keyword, search_results)
option = input(
"\nEnter a post ID to see its thread, {} to start a new search, {} to go back, or {} to quit: \n".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("m", "underline"),
style_text("q", "underline"),
@ -185,7 +190,7 @@ def search():
raise IndexError("Invalid post ID")
except (KeyError, ValueError, IndexError):
# Catch a Post ID that is not in the thread list or is not a number
print("\n{}\n".format(style_text("Invalid entry", "bold")))
print("{}".format(style_text("Invalid entry", "bold")))
def view_link_in_browser(url):
@ -313,7 +318,7 @@ def menu_view_categories():
print_categories()
option = input(
"\nEnter 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("s", "underline"),
style_text("q", "underline"),
@ -376,7 +381,7 @@ def menu_view_category_details(cat_index):
def menu_view_thread_details(post_id):
"""Displays thread details, handles related navigation menu"""
option_text = "\nType {} 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("b", "underline"),
config.USER.browser,
@ -426,9 +431,11 @@ def style_text(text: str, *args) -> str:
out = ""
for arg in args:
if arg in styles:
out += "\001"
out += styles[arg]
out += "\002"
out += text
out += "\033[0m"
out += "\001\033[0m\002"
return out

View File

@ -11,6 +11,7 @@ class TestPrintSearchResults(unittest.TestCase):
@patch("builtins.print")
def test_print_search_results(self, mock_print):
"""tests that the search results are printed correctly"""
test_keyword = "keyword"
test_search_results = [
(66, "keyword", "1576461366.5580268", "", "c", "c", "c"),
@ -26,12 +27,13 @@ class TestPrintSearchResults(unittest.TestCase):
call(" 2 2019-12-16 poster6 c "),
call(" 3 2019-12-16 poster7 c "),
call(" 4 2019-12-16 poster8 keyword "),
call(""),
]
linkulator.print_search_results(test_keyword, test_search_results)
self.assertEqual(
mock_print.call_count, 5
) # one count for title, 4 for the items)
mock_print.call_count, 6
) # one count for title, 4 for the items and a blank line for formatting
self.assertListEqual(test_print_calls, mock_print.call_args_list)