From f2685aa20931588baace1d509417705cc9015fc4 Mon Sep 17 00:00:00 2001 From: VetheonGames Date: Wed, 7 Jun 2023 16:19:23 -0600 Subject: [PATCH] Add in the code --- lib/dynamic_curses_input/input_handler.rb | 46 +++++++++++++++++++++++ lib/dynamic_curses_input/version.rb | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 lib/dynamic_curses_input/input_handler.rb diff --git a/lib/dynamic_curses_input/input_handler.rb b/lib/dynamic_curses_input/input_handler.rb new file mode 100644 index 0000000..a853c57 --- /dev/null +++ b/lib/dynamic_curses_input/input_handler.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +# lib/dynamic_curses_input/input_handler.rb + +require "curses" + +module DynamicCursesInput + # our main class for actually handling our user input + class InputHandler + def self.catch_input(echo) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity + Curses.stdscr.keypad(true) + input = "" + cursor_pos = 0 + initial_y = Curses.stdscr.cury + initial_x = Curses.stdscr.curx + Curses.noecho unless echo + while (ch = Curses.getch) + case ch + when Curses::KEY_LEFT + cursor_pos -= 1 unless cursor_pos.zero? + when Curses::KEY_RIGHT + cursor_pos += 1 unless cursor_pos == input.length + when Curses::KEY_BACKSPACE, 127 + if cursor_pos.positive? + input = input[0...cursor_pos - 1] + input[cursor_pos..] + cursor_pos -= 1 + end + when 10, 13 + break + else + if ch.is_a?(String) && !ch.nil? + input = input[0...cursor_pos] + ch + input[cursor_pos..] + cursor_pos += 1 + end + end + Curses.setpos(initial_y, initial_x) + Curses.addstr(" " * (Curses.cols - initial_x)) + Curses.setpos(initial_y, initial_x) + Curses.addstr(input) if echo + Curses.setpos(initial_y, initial_x + cursor_pos) + end + Curses.echo if echo + input + end + end +end diff --git a/lib/dynamic_curses_input/version.rb b/lib/dynamic_curses_input/version.rb index 5c63542..bd266b5 100644 --- a/lib/dynamic_curses_input/version.rb +++ b/lib/dynamic_curses_input/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module DynamicCursesInput - VERSION = "0.1.0" + VERSION = "1.0.0" end