Add ability to use up and down arrow keys...

to skip to the beginning and end of the line
This commit is contained in:
VetheonGames 2023-08-02 15:35:16 -06:00
parent 9afcf1b3c6
commit 5cc9f761ec
5 changed files with 42 additions and 15 deletions

View File

@ -5,8 +5,8 @@ require_relative 'lib/dynamic_curses_input/version'
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = 'dynamic_curses_input' spec.name = 'dynamic_curses_input'
spec.version = DynamicCursesInput::VERSION spec.version = DynamicCursesInput::VERSION
spec.authors = ['VetheonGames'] spec.authors = ['PixelRidge-Softworks']
spec.email = ['vetheon@pixelatedstudios.net'] spec.email = ['connorc@pixelridgesoftworks.com']
spec.summary = 'A simple library for making Curses TUI input more dynamic and user-friendly' spec.summary = 'A simple library for making Curses TUI input more dynamic and user-friendly'
spec.description = "Dynamic Curses Input is a highly simple, yet powerful gem that allows simple implementation of spec.description = "Dynamic Curses Input is a highly simple, yet powerful gem that allows simple implementation of
@ -16,17 +16,22 @@ Gem::Specification.new do |spec|
that code, by providing simple to use methods that allow developers to capture user input, while that code, by providing simple to use methods that allow developers to capture user input, while
allowing the special keys to work as the average user would expect. allowing the special keys to work as the average user would expect.
IE: When you press the left arrow key, the cursor moves to the left and allows you to delete a IE: When you press the left arrow key, the cursor moves to the left and allows you to delete a
character you entered that isn't the last character you entered." character you entered that isn't the last character you entered.
spec.homepage = 'https://github.com/Pixelated-Studios/dynamic_curses_input'
KNOWN BUG: X & Y coords for placing windows appear to be broken right now. Will fix with
next update"
spec.homepage = 'https://git.pixelridgesoftworks.com/PixelRidge-Softworks/dynamic_curses_input'
spec.license = 'MIT' spec.license = 'MIT'
spec.required_ruby_version = '3.2.2' spec.required_ruby_version = '3.2.2'
spec.metadata['homepage_uri'] = spec.homepage spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/Pixelated-Studios/dynamic_curses_input' spec.metadata['source_code_uri'] = 'https://git.pixelridgesoftworks.com/PixelRidge-Softworks/dynamic_curses_input'
spec.metadata['changelog_uri'] = 'https://github.com/Pixelated-Studios/dynamic_curses_input/blob/main/CHANGELOG.md' spec.metadata['changelog_uri'] = 'https://git.pixelridgesoftworks.com/PixelRidge-Softworks/dynamic_curses_input/src/branch/main/CHANGELOG.md'
# Specify which files should be added to the gem when it is released. # Specify which files should be added to the gem when it is released.
spec.files = Dir.glob('{bin,lib,sig}/**/*') + Dir.glob('*').reject { |f| f.start_with?('spec', '.rspec', 'dynamic_curses_input.gemspec') } spec.files = Dir.glob('{bin,lib,sig}/**/*') + Dir.glob('*').reject do |f|
f.start_with?('spec', '.rspec', 'dynamic_curses_input.gemspec')
end
spec.files << 'LICENSE.txt' spec.files << 'LICENSE.txt'
spec.files << 'README.md' spec.files << 'README.md'
spec.files << 'dynamic_curses_input.gemspec' spec.files << 'dynamic_curses_input.gemspec'

View File

@ -15,12 +15,12 @@ module DynamicCursesInput
InputHandler.catch_input(echo) InputHandler.catch_input(echo)
end end
def self.ask_question(color = 'white', question, x: 'center', input: true, echo: nil) def self.ask_question(color = 'white', question, x: 'center', input: true, echo: nil) # rubocop:disable Naming/MethodParameterName
Curses.clear Curses.clear
ColorWindow.add_color_window(color, question, y:, x:, input:, echo:) ColorWindow.add_color_window(color, question, y:, x:, input:, echo:)
end end
def self.print_color_window(color, text, y_value: nil, x: 'center', input: nil, echo: true) def self.print_color_window(color, text, y_value: nil, x: 'center', input: nil, echo: true) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/ParameterLists, Naming/MethodParameterName
case x case x
when 'center' when 'center'
terminal_size = `stty size`.split.map(&:to_i) terminal_size = `stty size`.split.map(&:to_i)
@ -59,7 +59,7 @@ module DynamicCursesInput
class << self class << self
private private
def process_print_color_window_args(args) def process_print_color_window_args(args) # rubocop:disable Metrics/MethodLength
case args.size case args.size
when 2 when 2
['white', args[0], args[1], 'center', nil, true] ['white', args[0], args[1], 'center', nil, true]

View File

@ -9,7 +9,7 @@ module DynamicCursesInput
# Class for creating a colored window # Class for creating a colored window
class ColorWindow class ColorWindow
# Initialize instance variables and setup curses # Initialize instance variables and setup curses
def initialize(echo, x, y) def initialize(echo, x, y) # rubocop:disable Metrics/MethodLength, Naming/MethodParameterName
@echo = echo # Determines whether input should be echoed to the screen @echo = echo # Determines whether input should be echoed to the screen
setup_curses_color # Setup curses setup_curses_color # Setup curses
@x = x @x = x
@ -26,7 +26,7 @@ module DynamicCursesInput
end end
# Method that adds colored text to the window # Method that adds colored text to the window
def add_color_window(color, text, x, y, input: nil, echo: true) def add_color_window(color, text, x, y, input: nil, echo: true) # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists, Naming/MethodParameterName
# Map color names to color pair numbers # Map color names to color pair numbers
color_map = { color_map = {
'black' => 1, 'black' => 1,
@ -70,7 +70,7 @@ module DynamicCursesInput
end end
# Set cursor position manually on the X and Y axis # Set cursor position manually on the X and Y axis
def set_position(y, x) def set_position(y, x) # rubocop:disable Naming/MethodParameterName
Curses.setpos(y, x) Curses.setpos(y, x)
end end
end end

View File

@ -47,6 +47,8 @@ module DynamicCursesInput
case chk case chk
when Curses::KEY_LEFT then handle_left_key # Move cursor left when Curses::KEY_LEFT then handle_left_key # Move cursor left
when Curses::KEY_RIGHT then handle_right_key # Move cursor right when Curses::KEY_RIGHT then handle_right_key # Move cursor right
when Curses::KEY_UP then handle_up_key
when Curses::KEY_DOWN then handle_down_key
when Curses::KEY_BACKSPACE, 127 then handle_backspace_key # Delete character when Curses::KEY_BACKSPACE, 127 then handle_backspace_key # Delete character
when 10, 13 then handle_enter_key # Break loop if enter key is pressed when 10, 13 then handle_enter_key # Break loop if enter key is pressed
else handle_default_key(chk) # Add character to input string else handle_default_key(chk) # Add character to input string
@ -63,6 +65,16 @@ module DynamicCursesInput
@cursor_pos = CursorMover.right(@cursor_pos, @input.length) @cursor_pos = CursorMover.right(@cursor_pos, @input.length)
end end
# Move cursor down
def handle_down_key
@cursor_pos = CursorMover.down(@cursor_pos, @input.length)
end
# Move cursor up
def handle_up_key
@cursor_pos = CursorMover.up(@cursor_pos, @input.length)
end
# Delete character # Delete character
def handle_backspace_key def handle_backspace_key
@input, @cursor_pos = CharacterDeleter.delete(@input, @cursor_pos) @input, @cursor_pos = CharacterDeleter.delete(@input, @cursor_pos)
@ -103,8 +115,18 @@ module DynamicCursesInput
cursor_pos == length ? cursor_pos : cursor_pos + 1 cursor_pos == length ? cursor_pos : cursor_pos + 1
end end
# Handle down arrow key
def self.down(_, length)
length # Move to the end of the line
end
# Handle up arrow key
def self.up(_, _length)
0 # Move to the beginning of the line (position 0)
end
# Set cursor position # Set cursor position
def self.set_position(y, x) def self.set_position(y, x) # rubocop:disable Naming/MethodParameterName
Curses.setpos(y, x) Curses.setpos(y, x)
end end
end end

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
module DynamicCursesInput module DynamicCursesInput
VERSION = '1.1.0' VERSION = '1.2.0'
end end