Skip to content

Commit

Permalink
add IO#raw! and IO#cooked! to io/console.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
timfel committed Mar 2, 2017
1 parent caf29d0 commit f369308
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 deletions.
82 changes: 82 additions & 0 deletions lib-ruby/io/console.rb
@@ -0,0 +1,82 @@
class IO
# Indexes for termios list.
IFLAG = 0
OFLAG = 1
CFLAG = 2
LFLAG = 3
ISPEED = 4
OSPEED = 5
CC = 6
include Topaz::TermIOConstants

# def raw
# end

def raw!(&block)
mode = Topaz.tcgetattr(fileno)
mode[IFLAG] = mode[IFLAG] & ~(IGNBRK | BRKINT | PARMRK | ISTRIP | ICRNL |
IXON)
mode[OFLAG] = mode[OFLAG] & ~(OPOST)
mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB)
mode[CFLAG] = mode[CFLAG] | CS8
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON | ISIG |
IEXTEN)
Topaz.tcsetattr(fileno, TCSANOW, mode)
if block
begin
block[]
ensure
cooked!
end
end
end

# def cooked
# end

def cooked!
mode = Topaz.tcgetattr(fileno)
mode[IFLAG] = mode[IFLAG] | (BRKINT | ISTRIP | ICRNL | IXON)
mode[OFLAG] = mode[OFLAG] | OPOST
mode[LFLAG] = mode[LFLAG] | (ECHO | ECHOE | ECHOK | ECHONL | ICANON | ISIG |
IEXTEN)
Topaz.tcsetattr(fileno, TCSANOW, mode)
end

# def getch
# end

# def echo=
# end

# def echo
# end

# def echo?
# end

# def noecho
# end

# def winsize
# end

# def winsize=
# end

# def iflush
# end

# def oflush
# end

# def ioflush
# end

# def self.console
# end

# module Readable
# alias getch getc
# end
end
Empty file added lib-ruby/io/wait.rb
Empty file.
12 changes: 7 additions & 5 deletions topaz/modules/topaz.py
Expand Up @@ -4,6 +4,7 @@
from rpython.rlib.rarithmetic import intmask
from rpython.rlib.rtermios import tcsetattr, tcgetattr, all_constants

from topaz.error import error_for_oserror
from topaz.module import ModuleDef
from topaz.objects.classobject import W_ClassObject

Expand All @@ -14,8 +15,9 @@ class Topaz(object):
@moduledef.setup_module
def setup_module(space, w_mod):
space.set_const(w_mod, "FIXNUM_MAX", space.newint(sys.maxint))
w_termioconsts = space.newclass("TermIOConstants", None)
for name, value in all_constants:
w_termioconsts = space.newmodule("TermIOConstants", None)
space.set_const(w_mod, "TermIOConstants", w_termioconsts)
for name, value in all_constants.iteritems():
space.set_const(w_termioconsts, name, space.newint(value))

@moduledef.function("intmask")
Expand Down Expand Up @@ -62,22 +64,22 @@ def method_tcsetattr(self, space, fd, when, mode_w):
try:
tcsetattr(fd, when, mode)
except OSError as e:
raise space.error_for_oserror(e)
raise error_for_oserror(space, e)
return self

@moduledef.function("tcgetattr", fd="int")
def method_tcsetattr(self, space, fd):
try:
mode = tcgetattr(fd)
except OSError as e:
raise space.error_for_oserror(e)
raise error_for_oserror(space, e)
mode_w = [
space.newint(mode[0]), # iflag
space.newint(mode[1]), # oflag
space.newint(mode[2]), # cflag
space.newint(mode[3]), # lflag
space.newint(mode[4]), # ispeed
space.newint(mode[5]), # ospeed
space.newarray([space.newstr_fromstr(cc) in mode[6]])
space.newarray([space.newstr_fromstr(cc) for cc in mode[6]])
]
return space.newarray(mode_w)
4 changes: 0 additions & 4 deletions topaz/objects/ioobject.py
@@ -1,7 +1,6 @@
import os

from rpython.rlib.rpoll import POLLIN
from rpython.rlib.rtermios import tcsetattr

from topaz.coerce import Coerce
from topaz.error import error_for_oserror
Expand Down Expand Up @@ -293,6 +292,3 @@ def method_ready(self, space):
except PollError:
return space.w_nil
return space.newbool(len(retval) > 0)

@classdef.method("raw!")
def method_raw_bang(self, space):

0 comments on commit f369308

Please sign in to comment.