Moved `Key::Names` into constants under `Key::ASCII`. Not really any prettier. Dunno...
[nfoiled.git] / lib / nfoiled / key.rb
blobbc9719410a8b0e451158b998da66874603f58232
1 module Nfoiled
2   ##
3   # This is the class of a single character of input received by Nfoiled.
4   class Key
5     ##
6     # A container for some useful constants.
7     module ASCII
8       # TODO: Figure out meaning of, and proper capitalization of, some of
9       # these constants.
10       Null = NUL = 0; SOH = 1; STX = 2; ETX = 3; EOT = 4
11       ENQ = 5; Acknowledge = ACK = 6; Bell = BEL = 7; Backspace = BS = 8
12       Tab = HT = 9; Newline = NL = 10; VT = 11; NP = 12
13       CarriageReturn = CR = 13; SO = 14; SI = 15; DLE = 16
14       DC1 = 17; DC2 = 18; DC3 = 19; DC4 = 20
15       NAK = 21; SYN = 22; ETB = 23; CAN = 24
16       EM = 25; SUB = 26; Escape = ESC = 27; FS = 28
17       GS = 29; RS = 30; US = 31; Delete = DEL = 127
18     end
19     
20     ##
21     # Creates a `Key` out of an ASCII integer.
22     def self.ascii charint
23       if (32..126).member? charint
24         new charint.chr.to_sym
25       elsif  Names.member? charint 
26         new Names[charint]
27       end
28     end
29     
30     # The character corresponding to this keypress
31     attr_reader :char
32     
33     ##
34     # Creates a new `Key`. Input will be turned into a symbol if possible.
35     def initialize stringish
36       @char = stringish.respond_to?(:to_s) ? stringish.to_s.to_sym : stringish.to_sym
37     end
38     
39     ##
40     # Compares two keys' characters.
41     def == o
42       return false unless o.is_a? Key
43       self.char == o.char
44     end
45   end
46 end