Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2835728d0892
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c7b7e2cfdf31
Choose a head ref
  • 4 commits
  • 2 files changed
  • 2 contributors

Commits on Jul 23, 2016

  1. Copy the full SHA
    bb2cfbc View commit details
  2. Remove IO#read_nonblock

    jhass committed Jul 23, 2016
    Copy the full SHA
    d7865a3 View commit details
  3. Copy the full SHA
    161452a View commit details

Commits on Jul 24, 2016

  1. Merge pull request #3036 from jhass/remove_nonblock

    Remove IO#read_nonblock
    Ary Borenszweig authored Jul 24, 2016
    Copy the full SHA
    c7b7e2c View commit details
Showing with 9 additions and 31 deletions.
  1. +8 −10 samples/2048.cr
  2. +1 −21 src/io/console.cr
18 changes: 8 additions & 10 deletions samples/2048.cr
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ require "colorize"

module Screen
TILES = {
0 => {:white, :black},
0 => {:white, nil},
2 => {:black, :white},
4 => {:blue, :white},
8 => {:black, :yellow},
@@ -26,7 +26,9 @@ module Screen

def self.colorize_for(tile)
fg_color, bg_color = TILES[tile]
with_color(fg_color).on(bg_color).surround do
color = with_color(fg_color)
color = color.on(bg_color) if bg_color
color.surround do
yield
end
end
@@ -37,14 +39,10 @@ module Screen

def self.read_keypress
STDIN.raw do |io|
input = io.gets 1
return :unknown unless input
if input == "\e"
next_two_bytes = io.read_nonblock(2) rescue nil
# third_byte = io.read_nonblock(1) rescue nil
input += next_two_bytes if next_two_bytes
# input += third_byte if third_byte
end
buffer = Bytes.new(3)
bytes_read = io.read(buffer)
return :unknown if bytes_read == 0
input = String.new(buffer[0, bytes_read])

case input
when "\e[A", "w"
22 changes: 1 addition & 21 deletions src/io/console.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "termios"

module IO
class IO::FileDescriptor
# Turn off character echoing for the duration of the given block.
# This will prevent displaying back to the user what they enter on the terminal.
# Only call this when this IO is a TTY, such as a not redirected stdin.
@@ -111,24 +111,4 @@ module IO
LibC.tcsetattr(fd, Termios::LineControl::TCSANOW, pointerof(before))
end
end

def read_nonblock(size)
before = LibC.fcntl(fd, LibC::F_GETFL)
LibC.fcntl(fd, LibC::F_SETFL, before | LibC::O_NONBLOCK)

begin
String.new(size) do |buffer|
read_size = read Slice.new(buffer, size)
if read_size == 0
raise EOFError.new "read_nonblock: read nothing"
elsif Errno.value == LibC::EWOULDBLOCK
raise Errno.new "exception in read_nonblock"
else
{read_size.to_i, 0}
end
end
ensure
LibC.fcntl(fd, LibC::F_SETFL, before)
end
end
end