Class: Device::IO
Constant Summary collapse
- F1 =
"\001"- F2 =
"\002"- F3 =
"\003"- F4 =
"\004"- FUNC =
"\006"- UP =
"\007"- DOWN =
"\008"- MENU =
"\009"- ENTER =
0x0D.chr
- CLEAR =
0x0F.chr
- ALPHA =
0x10.chr
- SHARP =
0x11.chr
- KEY_TIMEOUT =
0x12.chr
- BACK =
"\017"- CANCEL =
0x1B.chr
- IO_INPUT_NUMBERS =
:numbers- IO_INPUT_LETTERS =
:letters- IO_INPUT_ALPHA =
:alpha- IO_INPUT_SECRET =
:secret- IO_INPUT_DECIMAL =
:decimal- IO_INPUT_MONEY =
:money- IO_INPUT_MASK =
:mask- MASK_ALPHA =
:alpha- MASK_LETTERS =
:letters- MASK_NUMBERS =
:number- DEFAULT_TIMEOUT =
30000- NUMBERS =
%w(1 2 3 4 5 6 7 8 9 0)
- ONE_NUMBER =
"1"- TWO_NUMBER =
"2"- THREE_NUMBER =
"3"- FOUR_NUMBER =
"4"- FIVE_NUMBER =
"5"- SIX_NUMBER =
"6"- SEVEN_NUMBER =
"7"- EIGHT_NUMBER =
"8"- NINE_NUMBER =
"9"- ZERO_NUMBER =
"0"
Class Attribute Summary collapse
-
.keys_range ⇒ Object
Returns the value of attribute keys_range.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
- .change_next(text, mask_type = Device::IO::MASK_ALPHA) ⇒ Object
- .check_mask_type(text, options) ⇒ Object
- .format(string, options) ⇒ Object
-
.get_format(min, max, options = {}) ⇒ String
Restricted to terminals, get strings and numbers.
-
.getc(timeout = self.timeout) ⇒ String
Read 1 byte on keyboard, wait until be pressed.
- .insert_key?(key, options) ⇒ Boolean
- .set_default_format_option(options) ⇒ Object
-
.setup_keyboard(map) ⇒ NilClass
Setup Keyboard Map.
Class Attribute Details
.keys_range ⇒ Object
Returns the value of attribute keys_range.
48 49 50 |
# File 'lib/device/io.rb', line 48 def keys_range @keys_range end |
.timeout ⇒ Object
Returns the value of attribute timeout.
48 49 50 |
# File 'lib/device/io.rb', line 48 def timeout @timeout end |
Class Method Details
.change_next(text, mask_type = Device::IO::MASK_ALPHA) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/device/io.rb', line 174 def self.change_next(text, mask_type = Device::IO::MASK_ALPHA) char = text[-1] if char && (range = self.keys_range[mask_type].detect { |range| range.include?(char) }) index = range.index(char) new_value = range[index+1] if new_value text[-1] = new_value else text[-1] = range[0] end end text end |
.check_mask_type(text, options) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/device/io.rb', line 162 def self.check_mask_type(text, ) if [:mode] == Device::IO::IO_INPUT_ALPHA Device::IO::MASK_ALPHA elsif [:mode] == Device::IO::IO_INPUT_LETTERS Device::IO::MASK_LETTERS elsif [:mode] == Device::IO::IO_INPUT_MASK [:mask_clean][text.length - 1].match(/[0-9]/) ? Device::IO::MASK_NUMBERS : Device::IO::MASK_LETTERS else Device::IO::MASK_ALPHA end end |
.format(string, options) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/device/io.rb', line 197 def self.format(string, ) [:label].to_s + if [:mode] == IO_INPUT_MONEY || [:mode] == IO_INPUT_DECIMAL number_to_currency(string, ) elsif [:mode] == IO_INPUT_SECRET "*" * string.size elsif [:mode] == IO_INPUT_MASK string.to_mask([:mask]) else string end end |
.get_format(min, max, options = {}) ⇒ String
Restricted to terminals, get strings and numbers. The switch method between uppercase, lowercase and number characters is to keep pressing a same button quickly. The timeout of this operation is 1 second.
:value - Represent the current value, to be initially used.
:precision - Sets the level of precision (defaults to 2).
:separator - Sets the separator between the units (defaults to “.”).
:delimiter - Sets the thousands delimiter (defaults to “,”).
:label - Sets the label display before currency, eg.: “U$:”, “R$:”
:mask - If mode IO_INPUT_MASK a mask should send
(only numbers and letters are allowed), eg.: "9999-AAAA"
:mode - Define input modes:
:numbers (IO_INPUT_NUMBERS) - Only number.
:letters (IO_INPUT_LETTERS) - Only Letters.
:alpha (IO_INPUT_ALPHA) - Letters and numbers.
:secret (IO_INPUT_SECRET) - Secret *.
:decimal (IO_INPUT_DECIMAL) - Decimal input, only number.
:money (IO_INPUT_MONEY) - Money input, only number.
:mask (IO_INPUT_MASK) - Custom mask.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/device/io.rb', line 129 def self.get_format(min, max, = {}) set_default_format_option() key = text = [:value] || "" while key != CANCEL Device::Display.clear [:line] Device::Display.print_line format(text, ), [:line], [:column] key = getc if key == BACK text = text[0..-2] elsif key == ENTER || key == KEY_TIMEOUT return text elsif key == F1 || key == DOWN || key == UP || key == ALPHA change_next(text, check_mask_type(text, )) next elsif text.size >= max next elsif insert_key?(key, ) text << key end end end |
.getc(timeout = self.timeout) ⇒ String
Read 1 byte on keyboard, wait until be pressed
If not sent the default timeout is 30_000. If nil should be blocking.
195 |
# File 'lib/device/io.rb', line 195 def self.getc(timeout = self.timeout); super(timeout); end |
.insert_key?(key, options) ⇒ Boolean
210 211 212 213 214 215 216 217 218 |
# File 'lib/device/io.rb', line 210 def self.insert_key?(key, ) if [:mode] == IO_INPUT_MONEY || [:mode] == IO_INPUT_DECIMAL || [:mode] == IO_INPUT_NUMBERS NUMBERS.include?(key) elsif [:mode] != IO_INPUT_NUMBERS && [:mode] != IO_INPUT_MONEY && [:mode] != IO_INPUT_DECIMAL true else false end end |
.set_default_format_option(options) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/device/io.rb', line 152 def self.set_default_format_option() [:mode] ||= IO_INPUT_LETTERS [:line] ||= 2 [:column] ||= 0 if [:mask] [:mask_clean] = [:mask].chars.reject{|ch| ch.match(/[^0-9A-Za-z]/) }.join end end |
.setup_keyboard(map) ⇒ NilClass
Setup Keyboard Map
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/device/io.rb', line 71 def self.setup_keyboard(map) one_letters, two_letters, three_letters, four_letters, five_letters, six_letters, seven_letters, eight_letters, nine_letters, zero_letters = map range_number = [ ONE_NUMBER , TWO_NUMBER , THREE_NUMBER , FOUR_NUMBER , FIVE_NUMBER , SIX_NUMBER , SEVEN_NUMBER , EIGHT_NUMBER , NINE_NUMBER , ZERO_NUMBER ] range_letters = [ one_letters, two_letters, three_letters, four_letters, five_letters, six_letters, seven_letters, eight_letters, nine_letters, zero_letters ] range_alpha = [ ONE_NUMBER + one_letters, TWO_NUMBER + two_letters, THREE_NUMBER + three_letters, FOUR_NUMBER + four_letters, FIVE_NUMBER + five_letters, SIX_NUMBER + six_letters, SEVEN_NUMBER + seven_letters, EIGHT_NUMBER + eight_letters, NINE_NUMBER + nine_letters, ZERO_NUMBER + zero_letters ] @keys_range = {MASK_ALPHA => range_alpha, MASK_LETTERS => range_letters, MASK_NUMBERS => range_number} end |