Class: Termcontroller::Controller
- Inherits:
-
Object
- Object
- Termcontroller::Controller
- Defined in:
- lib/termcontroller/controller.rb
Constant Summary collapse
- @@controllers =
[]
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#lastcmd ⇒ Object
readonly
Returns the value of attribute lastcmd.
Instance Method Summary collapse
- #handle_input ⇒ Object
-
#initialize(target = nil, keybindings = {}) ⇒ Controller
constructor
A new instance of Controller.
-
#pause ⇒ Object
Pause processing so you can read directly from stdin yourself.
- #paused? ⇒ Boolean
- #pop_target ⇒ Object
- #push_target(t) ⇒ Object
-
#raw ⇒ Object
FIXME: The first event after the yield appears to fail to pass the mapping.
- #resume ⇒ Object
- #suspend ⇒ Object
Constructor Details
#initialize(target = nil, keybindings = {}) ⇒ Controller
Returns a new instance of Controller.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/termcontroller/controller.rb', line 43 def initialize(target=nil, keybindings={}) @m = Mutex.new @target = target @target_stack = [] @keybindings = keybindings @buf = "" @commands = Queue.new @mode = :cooked @kb = KeyboardMap.new @con = IO.console raise if !@con at_exit { quit } trap("CONT") { resume } trap("WINCH") { @commands << :resize } setup @t = Thread.new { readloop } @@controllers << @t end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
41 42 43 |
# File 'lib/termcontroller/controller.rb', line 41 def commands @commands end |
#lastcmd ⇒ Object (readonly)
Returns the value of attribute lastcmd.
41 42 43 |
# File 'lib/termcontroller/controller.rb', line 41 def lastcmd @lastcmd end |
Instance Method Details
#handle_input ⇒ Object
119 120 121 122 123 124 |
# File 'lib/termcontroller/controller.rb', line 119 def handle_input if c = @commands.pop do_command(c) end return Array(c) end |
#pause ⇒ Object
Pause processing so you can read directly from stdin yourself. E.g. to use Readline, or to suspend
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/termcontroller/controller.rb', line 88 def pause @m.synchronize do old = @mode begin @mode = :pause IO.console.cooked! cleanup r = yield setup r rescue Interrupt ensure @mode = old end end end |
#paused? ⇒ Boolean
70 |
# File 'lib/termcontroller/controller.rb', line 70 def paused?; @mode == :pause; end |
#pop_target ⇒ Object
77 78 79 80 81 |
# File 'lib/termcontroller/controller.rb', line 77 def pop_target t = @target @target = @target_stack.pop t end |
#push_target(t) ⇒ Object
72 73 74 75 |
# File 'lib/termcontroller/controller.rb', line 72 def push_target(t) @target_stack << @target @target = t end |
#raw ⇒ Object
FIXME: The first event after the yield appears to fail to pass the mapping. Maybe move the mapping to the client thread?
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/termcontroller/controller.rb', line 108 def raw keybindings = @keybindings @keybindings = {} push_target(nil) yield rescue Interrupt ensure @keybindings = keybindings pop_target end |
#resume ⇒ Object
133 134 135 136 137 |
# File 'lib/termcontroller/controller.rb', line 133 def resume @mode = :cooked setup @commands << [:resume] end |
#suspend ⇒ Object
126 127 128 129 130 131 |
# File 'lib/termcontroller/controller.rb', line 126 def suspend pause do yield if block_given? Process.kill("STOP", 0) end end |