From 192a3a70ca585963ff90f3a03c67abaacc919c50 Mon Sep 17 00:00:00 2001 From: Ricardo Ferraz Leal Date: Tue, 9 Mar 2021 20:09:43 -0500 Subject: [PATCH] Tests with prompt-toolkit --- gamestonk_terminal/discovery/disc_menu.py | 47 +++++++++++++---------- gamestonk_terminal/menu.py | 7 ++++ requirements-full.txt | 1 + requirements-old.txt | 1 + requirements-win.txt | 1 + requirements.txt | 1 + terminal.py | 47 +++++++++++++---------- 7 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 gamestonk_terminal/menu.py diff --git a/gamestonk_terminal/discovery/disc_menu.py b/gamestonk_terminal/discovery/disc_menu.py index bfabf65877b..e6d05739121 100644 --- a/gamestonk_terminal/discovery/disc_menu.py +++ b/gamestonk_terminal/discovery/disc_menu.py @@ -11,6 +11,9 @@ from gamestonk_terminal.discovery import unusual_whales_api from gamestonk_terminal.helper_funcs import get_flair +from gamestonk_terminal.menu import session +from prompt_toolkit.completion import WordCompleter + def print_discovery(): """ Print help """ @@ -42,33 +45,35 @@ def disc_menu(): # Add list of arguments that the discovery parser accepts disc_parser = argparse.ArgumentParser(prog="discovery", add_help=False) - disc_parser.add_argument( - "cmd", - choices=[ - "help", - "q", - "quit", - "map", - "sectors", - "gainers", - "spacs", - "orders", - "spachero", - "high_short", - "low_float", - "up_earnings", - "simply_wallst", - "uwhales", - "mill", - ], - ) + choices = [ + "help", + "q", + "quit", + "map", + "sectors", + "gainers", + "spacs", + "orders", + "spachero", + "high_short", + "low_float", + "up_earnings", + "simply_wallst", + "uwhales", + "mill", + ] + disc_parser.add_argument("cmd", choices=choices) + word_completer = WordCompleter(choices) print_discovery() # Loop forever and ever while True: # Get input command from user - as_input = input(f"{get_flair()} (disc)> ") + as_input = session.prompt( + f"{get_flair()} (disc)> ", + completer=word_completer, + ) # Parse fundamental analysis command of the list of possible commands try: diff --git a/gamestonk_terminal/menu.py b/gamestonk_terminal/menu.py new file mode 100644 index 00000000000..4c898945042 --- /dev/null +++ b/gamestonk_terminal/menu.py @@ -0,0 +1,7 @@ +import os + +from prompt_toolkit import PromptSession +from prompt_toolkit.history import FileHistory + +history_file = os.path.join(os.path.expanduser("~"), ".gamestonk_terminal.his") +session = PromptSession(history=FileHistory(history_file)) diff --git a/requirements-full.txt b/requirements-full.txt index 4ee3b88a656..2618656e3f1 100644 --- a/requirements-full.txt +++ b/requirements-full.txt @@ -156,3 +156,4 @@ wrapt==1.12.1; python_version >= "3.6" and python_full_version < "3.0.0" or pyth yarl==1.6.3; python_version >= "3.6" yfinance==0.1.55 zipp==3.4.1; python_version < "3.8" and python_version >= "3.6" +prompt-toolkit==3.0.16; python_version >= "3.6" diff --git a/requirements-old.txt b/requirements-old.txt index 01857adc3ce..a6b15ffebcc 100644 --- a/requirements-old.txt +++ b/requirements-old.txt @@ -133,3 +133,4 @@ wrapt==1.12.1; python_version >= "3.6" and python_full_version < "3.0.0" or pyth yarl==1.6.3; python_version >= "3.6" yfinance==0.1.55 zipp==3.4.0; python_version >= "3.6" and python_full_version < "3.0.0" and python_version < "3.8" or python_full_version >= "3.2.0" and python_version >= "3.6" and python_version < "3.8" +prompt-toolkit==3.0.16; python_version >= "3.6" diff --git a/requirements-win.txt b/requirements-win.txt index b77624f1fc1..6049dad70ca 100644 --- a/requirements-win.txt +++ b/requirements-win.txt @@ -133,3 +133,4 @@ wrapt==1.12.1; python_version >= "3.6" and python_full_version < "3.0.0" or pyth yarl==1.6.3; python_version >= "3.6" yfinance==0.1.55 zipp==3.4.0; python_version >= "3.6" and python_full_version < "3.0.0" and python_version < "3.8" or python_full_version >= "3.2.0" and python_version >= "3.6" and python_version < "3.8" +prompt-toolkit==3.0.16; python_version >= "3.6" diff --git a/requirements.txt b/requirements.txt index 81e052a160e..c08fd5316b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -90,3 +90,4 @@ websocket-client==0.58.0; python_version >= "3.6" and python_full_version < "3.0 yarl==1.6.3; python_version >= "3.6" yfinance==0.1.55 zipp==3.4.1; python_version < "3.8" and python_version >= "3.6" +prompt-toolkit==3.0.16; python_version >= "3.6" diff --git a/terminal.py b/terminal.py index 4c982d6106f..c954e3329fa 100644 --- a/terminal.py +++ b/terminal.py @@ -15,6 +15,9 @@ from gamestonk_terminal.sentiment import sen_menu as sm from gamestonk_terminal.papermill import papermill_menu as mill from gamestonk_terminal import res_menu as rm from gamestonk_terminal import config_terminal as cfg +from gamestonk_terminal.menu import session +from prompt_toolkit.completion import WordCompleter + # import warnings # warnings.simplefilter("always") @@ -42,26 +45,25 @@ def main(): # Add list of arguments that the main parser accepts menu_parser = argparse.ArgumentParser(prog="gamestonk_terminal", add_help=False) - menu_parser.add_argument( - "opt", - choices=[ - "help", - "quit", - "q", - "clear", - "load", - "view", - "export", - "disc", - "mill", - "sen", - "res", - "fa", - "ta", - "dd", - "pred", - ], - ) + choices = [ + "help", + "quit", + "q", + "clear", + "load", + "view", + "export", + "disc", + "mill", + "sen", + "res", + "fa", + "ta", + "dd", + "pred", + ] + menu_parser.add_argument("opt", choices=choices) + word_completer = WordCompleter(choices) # Print first welcome message and help print("") @@ -77,7 +79,10 @@ def main(): should_print_help = False # Get input command from user - as_input = input(f"{get_flair()}> ") + as_input = session.prompt( + f"{get_flair()}> ", + completer=word_completer, + ) # Is command empty if not as_input: