diff --git a/lib/log_bench/app/input_handler.rb b/lib/log_bench/app/input_handler.rb index 4195573..7e6cff3 100644 --- a/lib/log_bench/app/input_handler.rb +++ b/lib/log_bench/app/input_handler.rb @@ -15,6 +15,8 @@ class InputHandler CTRL_L = 12 # Clear requests CTRL_R = 18 # Undo clear requests (restore) ESC = 27 # Escape + BACKSPACE_KEYS = [127, 8, KEY_BACKSPACE].freeze + EXIT_FILTER_MODE_KEYS = [27, 10, 13].freeze # Escape, Enter, Return # UI constants DEFAULT_VISIBLE_HEIGHT = 20 @@ -62,7 +64,7 @@ def filter_mode_active? def handle_filter_input(ch) case ch - when 10, 13, 27 # Enter, Return, Escape + when *EXIT_FILTER_MODE_KEYS state.exit_filter_mode when KEY_UP state.exit_filter_mode @@ -70,7 +72,7 @@ def handle_filter_input(ch) when KEY_DOWN state.exit_filter_mode state.navigate_down - when 127, 8 # Backspace + when *BACKSPACE_KEYS state.backspace_filter else add_character_to_filter(ch) diff --git a/test/test_input_handler_filter.rb b/test/test_input_handler_filter.rb new file mode 100644 index 0000000..989a9fa --- /dev/null +++ b/test/test_input_handler_filter.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "test_helper" + +class TestInputHandlerFilter < Minitest::Test + def setup + @state = test_state + @state.switch_to_left_pane + @state.enter_filter_mode + @state.add_to_filter("ab") + @input_handler = LogBench::App::InputHandler.new(@state, Object.new) + end + + def test_backspace_ascii_127_removes_character + @input_handler.send(:handle_filter_input, 127) + + assert_equal "a", @state.main_filter.display_text + end + + def test_backspace_ctrl_h_removes_character + @input_handler.send(:handle_filter_input, 8) + + assert_equal "a", @state.main_filter.display_text + end + + def test_backspace_key_backspace_removes_character + @input_handler.send(:handle_filter_input, Curses::KEY_BACKSPACE) + + assert_equal "a", @state.main_filter.display_text + end +end