Class: InteractiveTerm::Term
- Inherits:
-
Object
- Object
- InteractiveTerm::Term
- Defined in:
- lib/interactive_term.rb
Constant Summary collapse
- FPS =
30
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#screen ⇒ Object
readonly
Returns the value of attribute screen.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #break_loop ⇒ Object
- #debug! ⇒ Object
- #end_session ⇒ Object
-
#initialize(start_session = false) ⇒ Term
constructor
A new instance of Term.
- #loop(&block) ⇒ Object
- #register_listener(&block) ⇒ Object
- #start_session ⇒ Object
Constructor Details
#initialize(start_session = false) ⇒ Term
Returns a new instance of Term.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/interactive_term.rb', line 9 def initialize(start_session = false) @stty_state = @width = @height = @listener_thread = @session_active = @loop_active = nil @listeners = [] @keypress_queue = Queue.new @height, @width = IO.console.winsize @screen = VirtualScreen.new(@width , @height) self.start_session if start_session end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/interactive_term.rb', line 7 def height @height end |
#screen ⇒ Object (readonly)
Returns the value of attribute screen.
7 8 9 |
# File 'lib/interactive_term.rb', line 7 def screen @screen end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
7 8 9 |
# File 'lib/interactive_term.rb', line 7 def width @width end |
Instance Method Details
#break_loop ⇒ Object
80 81 82 |
# File 'lib/interactive_term.rb', line 80 def break_loop @loop_active = false end |
#debug! ⇒ Object
98 99 100 101 102 103 |
# File 'lib/interactive_term.rb', line 98 def debug! unless @debug @listeners << proc {|key| @screen.draw(key, width - 1, height - 1)} @debug = true end end |
#end_session ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/interactive_term.rb', line 84 def end_session #bring back the cursor puts "\e[?25h" #restore stty `stty #{@stty_state}` #return to original terminal context system('tput rmcup') @session_active = false @loop_active = false end |
#loop(&block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/interactive_term.rb', line 57 def loop(&block) @loop_active = true while @loop_active # process up to 5 keypresses (should be fine because happens FPS times per second) begin 5.times do keypress = @keypress_queue.pop(true) #nonblock @listeners.each {|listener| listener.call(keypress)} if keypress @keypress_queue = Queue.new end rescue ThreadError # nothing to pop end # run loop code yield @screen.update! sleep 1.0/FPS end end |
#register_listener(&block) ⇒ Object
20 21 22 |
# File 'lib/interactive_term.rb', line 20 def register_listener(&block) @listeners << block end |
#start_session ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/interactive_term.rb', line 24 def start_session @session_active = true # switch to a new terminal context system('tput smcup') # hide the cursor puts "\e[?25l" # clear the screen puts "\e[H\e[2J" # store the stty state @stty_state = `stty -g` # raw: keypresses get passed along unprocessed # -echo: user doesn't see what they type # -icanon: no buffering/delay on keypress # isig: enable quit special character (necessary because of raw) `stty raw -echo -icanon isig` @lisen_thread = Thread.new do Thread.current.abort_on_exception = true loop do @keypress_queue.push $stdin.getc end end trap("SIGINT") do self.end_session end end |