Class: Spiral::Textbox
- Inherits:
-
Object
- Object
- Spiral::Textbox
- Defined in:
- lib/spiral/textbox.rb
Instance Method Summary collapse
-
#initialize(input = $stdin) ⇒ Textbox
constructor
A new instance of Textbox.
- #main ⇒ Object
- #process(ch) ⇒ Object
Constructor Details
#initialize(input = $stdin) ⇒ Textbox
Returns a new instance of Textbox.
2 3 4 |
# File 'lib/spiral/textbox.rb', line 2 def initialize(input=$stdin) @input = input end |
Instance Method Details
#main ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/spiral/textbox.rb', line 30 def main @input.raw do |io| yield @string = "" return if catch(:exit) do io.chars do |ch| prev = @string.dup result = process(ch) next if !(Symbol === result) && result == prev yield result end end end end |
#process(ch) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/spiral/textbox.rb', line 6 def process(ch) case ch when "\027" # <C-W> @string = @string.rstrip.split(/\b/)[0...-1].join when "\b" @string = @string.split(//)[0...-1].join when "\r" when "\003", "\004" throw :exit when "\x10" # <C-p> return :prev when "\x0e" # <C-n> return :next else case ch when /[^[:cntrl:]]/ @string += ch else end end @string end |