From 281ec983d04b002fa6b66b5fddc7bb6b4b86e235 Mon Sep 17 00:00:00 2001 From: "didierlopes.eth" Date: Mon, 14 Mar 2022 18:07:42 +0000 Subject: [PATCH] Add command location to plots (#1474) * poc of source on figures * update feature flag * fix reset on stocks/ba menu * improve aligment * remove source * make command location a global variable * add cmdloc to settings controller --- gamestonk_terminal/feature_flags.py | 3 ++ gamestonk_terminal/helper_classes.py | 31 ++++++++++++++++++- gamestonk_terminal/helper_funcs.py | 16 ++++++++++ gamestonk_terminal/parent_classes.py | 2 ++ gamestonk_terminal/settings_controller.py | 14 +++++++++ .../behavioural_analysis/ba_controller.py | 6 ++++ .../test_ba_controller.py | 6 ++-- 7 files changed, 74 insertions(+), 4 deletions(-) diff --git a/gamestonk_terminal/feature_flags.py b/gamestonk_terminal/feature_flags.py index 2b240956dd2..6efc5fe9e76 100644 --- a/gamestonk_terminal/feature_flags.py +++ b/gamestonk_terminal/feature_flags.py @@ -25,6 +25,9 @@ USE_ION = strtobool(os.getenv("GTFF_USE_ION", "True")) # Enable watermark in the figures USE_WATERMARK = strtobool(os.getenv("GTFF_USE_WATERMARK", "True")) +# Enable command and source in the figures +USE_CMD_LOCATION_FIGURE = strtobool(os.getenv("GTFF_USE_CMD_LOCATION_FIGURE", "True")) + # Enable Prompt Toolkit USE_PROMPT_TOOLKIT = strtobool(os.getenv("GTFF_USE_PROMPT_TOOLKIT", "True")) diff --git a/gamestonk_terminal/helper_classes.py b/gamestonk_terminal/helper_classes.py index a1a3d91b27f..e2cf70ef905 100644 --- a/gamestonk_terminal/helper_classes.py +++ b/gamestonk_terminal/helper_classes.py @@ -316,20 +316,49 @@ class TerminalStyle: """ label = "Gamestonk Terminal" fig.text( - 0.69, + 0.99, 0.0420, label, fontsize=12, color="gray", alpha=0.5, + horizontalalignment="right", ) + # pylint: disable=import-outside-toplevel + def add_cmd_source( + self, + fig: plt.figure, + ): + """Add a text label to a figure in a funny position. + + Parameters + ---------- + fig : plt.figure + A matplotlib figure + """ + from gamestonk_terminal.helper_funcs import command_location + + if command_location: + fig.text( + 0.01, + 0.5, + command_location, + rotation=90, + fontsize=12, + color="gray", + alpha=0.5, + verticalalignment="center", + ) + # pylint: disable=import-outside-toplevel def visualize_output(self, force_tight_layout: bool = True): """Show chart in an interactive widget.""" import gamestonk_terminal.feature_flags as gtff from gamestonk_terminal.rich_config import console + if gtff.USE_CMD_LOCATION_FIGURE: + self.add_cmd_source(plt.gcf()) if gtff.USE_WATERMARK: self.add_label(plt.gcf()) if force_tight_layout: diff --git a/gamestonk_terminal/helper_funcs.py b/gamestonk_terminal/helper_funcs.py index c6fa195f2c1..ebceb52ff82 100644 --- a/gamestonk_terminal/helper_funcs.py +++ b/gamestonk_terminal/helper_funcs.py @@ -43,6 +43,22 @@ MENU_GO_BACK = 0 MENU_QUIT = 1 MENU_RESET = 2 +# Command location path to be shown in the figures depending on watermark flag +command_location = "" + + +# pylint: disable=global-statement +def set_command_location(cmd_loc: str): + """Set command location + + Parameters + ---------- + cmd_loc: str + Command location called by user + """ + global command_location + command_location = cmd_loc + def log_and_raise(error: Union[argparse.ArgumentTypeError, ValueError]) -> None: logger.error(str(error)) diff --git a/gamestonk_terminal/parent_classes.py b/gamestonk_terminal/parent_classes.py index da4262b8aab..8f9a1a865aa 100644 --- a/gamestonk_terminal/parent_classes.py +++ b/gamestonk_terminal/parent_classes.py @@ -26,6 +26,7 @@ from gamestonk_terminal.helper_funcs import ( valid_date, parse_known_args_and_warn, valid_date_in_past, + set_command_location, ) from gamestonk_terminal.config_terminal import theme from gamestonk_terminal.rich_config import console @@ -176,6 +177,7 @@ class BaseController(metaclass=ABCMeta): elif known_args.cmd == "r": known_args.cmd = "reset" + set_command_location(f"{self.PATH}{known_args.cmd}") logger.info("CMD: %s", an_input) self.log_queue("QUEUE") diff --git a/gamestonk_terminal/settings_controller.py b/gamestonk_terminal/settings_controller.py index 4f8aaf66f58..835852572bc 100644 --- a/gamestonk_terminal/settings_controller.py +++ b/gamestonk_terminal/settings_controller.py @@ -35,6 +35,7 @@ class SettingsController(BaseController): "dt", "ion", "watermark", + "cmdloc", "promptkit", "predict", "autoscaling", @@ -103,6 +104,8 @@ class SettingsController(BaseController): ) color = "green" if gtff.USE_WATERMARK else "red" help_text += f" [{color}]watermark watermark in figures[/{color}]\n" + color = "green" if gtff.USE_CMD_LOCATION_FIGURE else "red" + help_text += f" [{color}]cmdloc command location displayed in figures[/{color}]\n" color = "green" if gtff.USE_PLOT_AUTOSCALING else "red" help_text += f" [{color}]autoscaling plot autoscaling[/{color}]\n\n" color = "green" if gtff.USE_DATETIME else "red" @@ -238,6 +241,17 @@ class SettingsController(BaseController): dotenv.set_key(self.env_file, "GTFF_USE_WATERMARK", str(gtff.USE_WATERMARK)) console.print("") + @log_start_end(log=logger) + def call_cmdloc(self, _): + """Process cmdloc command""" + gtff.USE_CMD_LOCATION_FIGURE = not gtff.USE_CMD_LOCATION_FIGURE + dotenv.set_key( + self.env_file, + "GTFF_USE_CMD_LOCATION_FIGURE", + str(gtff.USE_CMD_LOCATION_FIGURE), + ) + console.print("") + @log_start_end(log=logger) def call_autoscaling(self, _): """Process autoscaling command""" diff --git a/gamestonk_terminal/stocks/behavioural_analysis/ba_controller.py b/gamestonk_terminal/stocks/behavioural_analysis/ba_controller.py index 343a3b39c59..0cefb615cce 100644 --- a/gamestonk_terminal/stocks/behavioural_analysis/ba_controller.py +++ b/gamestonk_terminal/stocks/behavioural_analysis/ba_controller.py @@ -128,6 +128,12 @@ class BehaviouralAnalysisController(StockBaseController): """ console.print(text=help_text, menu="Stocks - Behavioural Analysis") + def custom_reset(self): + """Class specific component of reset command""" + if self.ticker: + return ["stocks", "ba", f"load {self.ticker}"] + return [] + @log_start_end(log=logger) def call_watchlist(self, other_args: List[str]): """Process watchlist command""" diff --git a/tests/gamestonk_terminal/stocks/behavioural_analysis/test_ba_controller.py b/tests/gamestonk_terminal/stocks/behavioural_analysis/test_ba_controller.py index cd60e9e710a..2d386096a9a 100644 --- a/tests/gamestonk_terminal/stocks/behavioural_analysis/test_ba_controller.py +++ b/tests/gamestonk_terminal/stocks/behavioural_analysis/test_ba_controller.py @@ -159,7 +159,7 @@ def test_print_help(): ("help/help", ["help", "help"]), ("q", ["quit"]), ("h", []), - ("r", ["quit", "quit", "reset", "stocks", "ba"]), + ("r", ["quit", "quit", "reset", "stocks", "ba", "load TSLA"]), ], ) def test_switch(an_input, expected_queue): @@ -207,12 +207,12 @@ def test_call_cls(mocker): ( "call_reset", [], - ["quit", "quit", "reset", "stocks", "ba"], + ["quit", "quit", "reset", "stocks", "ba", "load TSLA"], ), ( "call_reset", ["help"], - ["quit", "quit", "reset", "stocks", "ba", "help"], + ["quit", "quit", "reset", "stocks", "ba", "load TSLA", "help"], ), ], )