From 57d12c44647eb9565667c98fd66e10be05a3ae78 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 25 Oct 2021 20:47:52 -0500 Subject: [PATCH] Option to disable keys by using -D. --- dstatic/dstatic.py | 82 ++++++++++++++++++--------------- tests/test_dstatic.py | 28 +++++++++++ tests/test_dstatic_arguments.py | 8 ++++ 3 files changed, 80 insertions(+), 38 deletions(-) diff --git a/dstatic/dstatic.py b/dstatic/dstatic.py index 583ccd5..7cae260 100644 --- a/dstatic/dstatic.py +++ b/dstatic/dstatic.py @@ -191,49 +191,52 @@ def static(screen, color_mode: str, args: argparse.Namespace): ch = screen.getch() if args.screen_saver and ch != -1: break - elif ch in [81, 113]: # q, Q + if ch in [81, 113]: # q, Q break - elif ch == 98: # b - set_curses_colors("bw", []) - cycle_colors = False - additive_mode = False - elif ch == 67: # C - set_curses_colors("color", []) - cycle_colors = False - additive_mode = False - elif ch == 99: # c - cycle_colors = True - additive_mode = False - elif ch == 100 or ch == 68: # d, D - cycle_colors = False - set_curses_colors("color", []) - delay_time = convert_delay_number_to_delay_time(4) - additive_mode = False - elif ch == 97: # a - if additive_mode: + if args.disable_keys: + pass + else: + if ch == 98: # b + set_curses_colors("bw", []) + cycle_colors = False additive_mode = False + elif ch == 67: # C set_curses_colors("color", []) - else: cycle_colors = False - additive_mode = True - additive_colors = ["black"] + additive_mode = False + elif ch == 99: # c + cycle_colors = True + additive_mode = False + elif ch == 100 or ch == 68: # d, D + cycle_colors = False + set_curses_colors("color", []) + delay_time = convert_delay_number_to_delay_time(4) + additive_mode = False + elif ch == 97: # a + if additive_mode: + additive_mode = False + set_curses_colors("color", []) + else: + cycle_colors = False + additive_mode = True + additive_colors = ["black"] + set_curses_colors("additive", additive_colors) + elif additive_mode and ch in ch_color_codes: + color = curses_ch_codes_color[ch] + if color in additive_colors: + additive_colors.pop(additive_colors.index(color)) + else: + additive_colors.append(color) set_curses_colors("additive", additive_colors) - elif additive_mode and ch in ch_color_codes: - color = curses_ch_codes_color[ch] - if color in additive_colors: - additive_colors.pop(additive_colors.index(color)) - else: - additive_colors.append(color) - set_curses_colors("additive", additive_colors) - elif not additive_mode and ch in ch_color_codes: - color = curses_ch_codes_color[ch] - set_curses_colors(color, []) - cycle_colors = False - elif ch in ch_number_codes: - number = curses_number_ch_codes[ch] - delay_time = convert_delay_number_to_delay_time(number) - elif cycle_delay and ch in ch_shift_num_codes: - cycle_delay = curses_shift_num_codes[ch] + elif not additive_mode and ch in ch_color_codes: + color = curses_ch_codes_color[ch] + set_curses_colors(color, []) + cycle_colors = False + elif ch in ch_number_codes: + number = curses_number_ch_codes[ch] + delay_time = convert_delay_number_to_delay_time(number) + elif cycle_delay and ch in ch_shift_num_codes: + cycle_delay = curses_shift_num_codes[ch] sleep(delay_time) # clear screen before returning @@ -330,6 +333,9 @@ def argument_parser(argv: Optional[Sequence[str]] = None) -> argparse.Namespace: parser.add_argument("-a", dest="additive", action="store_true", help="Additive color mode. Use color keys " "(r,t,y,u,i,o,p,[) to add and remove colors") + parser.add_argument("-D", dest="disable_keys", action="store_true", + help="Disable keys while running except for 'Q' or 'q' and " + "for ctrl-c. Does not affect screensaver mode.") parser.add_argument("--list_colors", action="store_true", help="List available colors and exit.") parser.add_argument("--list_commands", action="store_true", diff --git a/tests/test_dstatic.py b/tests/test_dstatic.py index 308eb63..bb33f04 100644 --- a/tests/test_dstatic.py +++ b/tests/test_dstatic.py @@ -248,3 +248,31 @@ def test_dstatic_list_colors(): def test_dstatic_display_version(): with Runner(*dstatic_cmd("--version")) as h: h.await_text(f"{dstatic.version}") + + +@pytest.mark.parametrize("test_keys", ["Q", "q", "1", "u", "+", "d", "a", " "]) +def test_dstatic_disable_keys_screensaver_works(test_keys): + with Runner(*dstatic_cmd("-D", "-S")) as h: + h.default_timeout = 3 + h.await_text(chr(9617)) + h.write(test_keys) + h.press("Enter") + h.await_exit() + + +def test_dstatic_disable_keys_run_timer_exit(): + """ Test auto exit when using run timer. """ + with Runner(*dstatic_cmd("-r2", "-D")) as h: + h.default_timeout = 3 + h.await_text(chr(9617)) + h.await_exit() + + +@pytest.mark.parametrize("test_keys", ["Q", "q"]) +def test_dstatic_disable_keys_quit_works(test_keys): + with Runner(*dstatic_cmd("-D")) as h: + h.default_timeout = 3 + h.await_text(chr(9617)) + h.write(test_keys) + h.press("Enter") + h.await_exit() diff --git a/tests/test_dstatic_arguments.py b/tests/test_dstatic_arguments.py index aaf353e..7b7eb0f 100644 --- a/tests/test_dstatic_arguments.py +++ b/tests/test_dstatic_arguments.py @@ -149,3 +149,11 @@ def test_argument_parsing_cycle_colors(test_values, expected_results): def test_argument_parsing_additive_color_mode(test_values, expected_result): result = dstatic.argument_parser(test_values) assert result.additive is expected_result + + +@pytest.mark.parametrize("test_values, expected_result", [ + ([], False), (["-D"], True), +]) +def test_argument_parsing_disable_keys(test_values, expected_result): + result = dstatic.argument_parser(test_values) + assert result.disable_keys is expected_result