InputHandler Class
The InputHandler
class is the main class for handling user input in the DynamicCursesInput gem. It is responsible for capturing user keystrokes and managing the input string and cursor position accordingly.
Methods
.catch_input(echo)
This is a class method that creates a new instance of InputHandler
and calls the catch_input
instance method on it. It takes one parameter:
echo
: a boolean value that determines whether the input should be echoed back to the user.
initialize(echo)
This is the constructor for the InputHandler
class. It initializes the instance variables and sets up Curses. It takes one parameter:
echo
: a boolean value that determines whether the input should be echoed back to the user.
catch_input
This instance method captures user keystrokes until the Enter key is pressed. It handles each keystroke by calling the handle_key
method and then redraws the input by calling the redraw_input
method. It returns the input string when the Enter key is pressed.
handle_key(chk)
This private instance method handles a single keystroke. It takes one parameter:
chk
: the keystroke to handle.
Depending on the value of chk
, it calls one of the following methods: handle_left_key
, handle_right_key
, handle_backspace_key
, handle_enter_key
, or handle_default_key
.
handle_left_key
This private instance method handles the Left arrow key. It moves the cursor one position to the left by calling the CursorMover.left
method.
handle_right_key
This private instance method handles the Right arrow key. It moves the cursor one position to the right by calling the CursorMover.right
method.
handle_backspace_key
This private instance method handles the Backspace key. It deletes the character at the cursor position and moves the cursor one position to the left by calling the CharacterDeleter.delete
method.
handle_enter_key
This private instance method handles the Enter key. It breaks the loop in the catch_input
method.
handle_default_key(chk)
This private instance method handles any other keystroke. It adds the character at the cursor position and moves the cursor one position to the right by calling the CharacterAdder.add
method. It takes one parameter:
chk
: the keystroke to handle.
redraw_input
This private instance method redraws the input. It clears the line, prints the input string, and moves the cursor to the correct position.