Skip to content

Commit

Permalink
For JRUBY-6943
Browse files Browse the repository at this point in the history
io/console extension missing in 1.9 mode

Add a trivial io/console using stty, for basic operations to work.
  • Loading branch information
headius committed Oct 16, 2012
1 parent fd45ea3 commit 0eb4171
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions lib/ruby/shared/io/console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
warn "io/console on JRuby shells out to stty for most operations"

# attempt to call stty; if failure, fall back on stubbed version
result = begin
`stty -a`
rescue Exception
nil
end

if !result || $?.exitstatus != 0 || RbConfig::CONFIG['host_os'] =~ /(mswin)|(win32)|(ming)/
# Windows version is always stubbed for now
class IO
def raw(*)
yield self
end

def raw!(*)
end

def cooked(*)
yield self
end

def cooked!(*)
end

def getch(*)
getc
end

def echo=(echo)
end

def echo?
true
end

def noecho
yield self
end

def winsize
[25, 80]
end

def winsize=(size)
end

def iflush
end

def oflush
end

def ioflush
end
end
else
# Non-Windows assumes stty command is available
class IO
def raw(*)
saved = `stty -g`
`stty raw`
yield self
ensure
`stty #{saved}`
end

def raw!(*)
`stty raw`
end

def cooked(*)
saved = `stty -g`
`stty cooked`
yield self
ensure
`stty #{saved}`
end

def cooked!(*)
`stty -raw`
end

def getch(*)
getc
end

def echo=(echo)
`stty #{echo ? 'echo' : '-echo'}`
end

def echo?
(`stty -a` =~ (/ -echo /)) ? false : true
end

def noecho
saved = `stty -g`
`stty -echo`
yield self
ensure
`stty #{saved}`
end

def winsize
match = `stty -a`.match(/(\d+) rows; (\d+) columns/)
[match[1].to_i, match[2].to_i]
end

def winsize=(size)
`stty rows #{size[0]} cols #{size[1]}`
end

def iflush
end

def oflush
end

def ioflush
end
end
end

0 comments on commit 0eb4171

Please sign in to comment.