Module: Potty::Keys

Defined in:
lib/potty/keys.rb

Overview

Named key codes, so widget input handling reads in intent rather than magic integers. Special keys resolve through Curses with fallbacks to the conventional ncurses values, for portability across curses builds.

(END_ carries a trailing underscore because END is a Ruby keyword.)

Constant Summary collapse

CTRL_C =

Control / ASCII

3
CTRL_A =
1
CTRL_E =
5
CTRL_D =
4
TAB =
9
ENTER =
10
RETURN =
13
ESC =
27
SPACE =
32
CTRL_H =
8
DEL_ASCII =
127
UP =

Arrows / navigation (curses-resolved with ncurses fallbacks)

const_or(:UP, 259)
DOWN =
const_or(:DOWN, 258)
LEFT =
const_or(:LEFT, 260)
RIGHT =
const_or(:RIGHT, 261)
HOME =
const_or(:HOME, 262)
END_ =
const_or(:END, 360)
DELETE =
const_or(:DC, 330)
BACKSPACE =
const_or(:BACKSPACE, 263)
SHIFT_TAB =
const_or(:BTAB, 353)
RESIZE =
const_or(:RESIZE, 410)
ENTERS =

Common groupings

[ENTER, RETURN].freeze
BACKSPACES =
[DEL_ASCII, CTRL_H, BACKSPACE].freeze

Class Method Summary collapse

Class Method Details

.backspace?(ch) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/potty/keys.rb', line 63

def backspace?(ch)
  BACKSPACES.include?(ch)
end

.code(ch) ⇒ Object

Normalize a curses #getch result to an integer key code. Ruby’s curses returns a String for ordinary printable input and an Integer for control/function keys — normalizing here lets all widget handle_key logic rely on numeric codes. Passes nil (tick timeout) and Integers through unchanged.



52
53
54
55
56
57
# File 'lib/potty/keys.rb', line 52

def code(ch)
  return ch unless ch.is_a?(String)
  return nil if ch.empty?

  ch.ord
end

.const_or(path, fallback) ⇒ Object



25
26
27
# File 'lib/potty/keys.rb', line 25

def self.const_or(path, fallback)
  Curses::Key.const_defined?(path) ? Curses::Key.const_get(path) : fallback
end

.enter?(ch) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/potty/keys.rb', line 59

def enter?(ch)
  ENTERS.include?(ch)
end

.printable?(ch) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/potty/keys.rb', line 67

def printable?(ch)
  ch.is_a?(Integer) && ch >= SPACE && ch <= DEL_ASCII - 1
end