class TracLang::ImmediateRead

Class to do console input. This is put in a separate class to make it easier to switch between implementations, since this seems to be seomthing that has some incompatibilities between operating systems.

Public Class Methods

new() click to toggle source

Creates class with console input handler depending on operating system.

# File lib/trac_lang/immediate_read.rb, line 15
def initialize
  if (/mingw|win|emx/=~RUBY_PLATFORM)!=nil
    @getchar=lambda{WinAPI._getch} # Windows
  else
    @getchar=lambda{STDIN.getbyte} # Unix
  end
  @method_name = :highline
end

Public Instance Methods

console_io() click to toggle source
# File lib/trac_lang/immediate_read.rb, line 29
def console_io
  c = IO.console.getch
  print c
  c
end
getch() click to toggle source

Get character from console input.

# File lib/trac_lang/immediate_read.rb, line 25
def getch
  self.send(@method_name)
end
highline() click to toggle source

Get character from console input, doing any translation necessary.

# File lib/trac_lang/immediate_read.rb, line 36
def highline
  chr = @getchar[].chr
  case chr
  when "\r"
    chr = "\n"
    puts
  when "\u0003"
    throw :done
  else
    STDOUT.write chr
  end
  chr
end