class Renote::Models::Shell

Attributes

output_file_path[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/renote/models/shell.rb, line 52
def initialize
  @output_file = nil
  super() # NOTE: This *must* be called, otherwise states won't get initialized
end

Public Instance Methods

alive?() click to toggle source
# File lib/renote/models/shell.rb, line 27
def alive?
  true
end
open(handle) click to toggle source
# File lib/renote/models/shell.rb, line 47
def open(handle)
  set_output_file handle
  start!
end
run() click to toggle source
# File lib/renote/models/shell.rb, line 33
def run
  c = read_char
  handle_input c if c
end

Private Instance Methods

handle_input(c) click to toggle source
# File lib/renote/models/shell.rb, line 63
def handle_input(c)
  if c=="\e"
    finish!
  else
    @output_file.putc c
    putc '.'
  end
end
read_char() click to toggle source

read a character without pressing enter and without printing to the screen

# File lib/renote/models/shell.rb, line 73
def read_char
  begin
    # save previous state of stty
    old_state = `stty -g`
    # disable echoing and enable raw (not having to press enter)
    system "stty raw -echo"
    c = STDIN.getc.chr
    # gather next two characters of special keys
    if c=="\e"
      extra_thread = Thread.new {
        c = c + STDIN.getc.chr
        c = c + STDIN.getc.chr
      }
      # wait just long enough for special keys to get swallowed
      extra_thread.join(0.00001)
      # kill thread so not-so-long special keys don't wait on getc
      extra_thread.kill
    end
  rescue => ex
    puts "#{ex.class}: #{ex.message}"
    puts ex.backtrace
  ensure
    # restore previous state of stty
    system "stty #{old_state}"
  end
  c
end
set_output_file(handle) click to toggle source
# File lib/renote/models/shell.rb, line 59
def set_output_file(handle)
  @output_file = handle
end