Skip to content

Commit

Permalink
allow 0.6 compatibility (need to check if 0.7 still works)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianKurz committed May 28, 2018
1 parent 5728557 commit d30298d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
8 changes: 4 additions & 4 deletions Terminal.jl/Terminal.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Terminal

using REPL
using Compat, Compat.REPL
export rawmode, clear_screen, readKey, cursor_move_abs, put, cursor_deleteline

function __init__()
@compat function __init__()
global terminal
terminal = REPL.Terminals.TTYTerminal(get(ENV, "TERM", is_windows() ? "" : "dumb"), STDIN, STDOUT, STDERR)
terminal = REPL.Terminals.TTYTerminal(get(ENV, "TERM", is_windows() ? "" : "dumb"),stdin, stdout, stderr)
end

include("rawmode.jl")
include("cursor.jl")
include("readkey.jl")

end #module
end #module
18 changes: 9 additions & 9 deletions Terminal.jl/cursor.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
cursor_deleteline(buf::IO=STDOUT) = print(buf, "\x1b[2K")
cursor_deleteline(buf::IO=terminal.out_stream) = print(buf, "\x1b[2K")

cursor_hide(buf::IO=STDOUT) = print(buf, "\x1b[?25l")
cursor_show(buf::IO=STDOUT) = print(buf, "\x1b[?25h")
cursor_hide(buf::IO=terminal.out_stream) = print(buf, "\x1b[?25l")
cursor_show(buf::IO=terminal.out_stream) = print(buf, "\x1b[?25h")

cursor_save_position(buf::IO=STDOUT) = print(buf, "\x1b[s")
cursor_restore_position(buf::IO=STDOUT) = print(buf, "\x1b[u")
cursor_save_position(buf::IO=terminal.out_stream) = print(buf, "\x1b[s")
cursor_restore_position(buf::IO=terminal.out_stream) = print(buf, "\x1b[u")

cursor_move_abs(buf::IO, c::Vector{Int}=[0,0]) = print(buf, "\x1b[$(c[2]);$(c[1])H")
cursor_move_abs(c::Vector{Int}) = cursor_move_abs(STDOUT, c)
cursor_move_abs(c::Vector{Int}) = cursor_move_abs(terminal.out_stream, c)

# ToDo: Remove x offset after newline
function cursor_move_rel(buf::IO, c=[0,0])
x = c[1] >= 0 ? "\x1b[$(abs(c[1]))A" : "\x1b[$(abs(c[1]))B"
y = c[2] >= 0 ? "\x1b[$(abs(c[2]))C" : "\x1b[$(abs(c[2]))D"
print(buf, x,y)
end
cursor_move_rel(c::Vector{Int}) = cursor_move_rel(STDOUT, c)
cursor_move_rel(c::Vector{Int}) = cursor_move_rel(terminal.out_stream, c)


function clear_screen()
Expand All @@ -30,7 +30,7 @@ end

"""
put(pos::Vector, s::String)
Put text `s` on screen at coordinates `pos`.
Put text `s` on screen at coordinates `pos`.
Does not change cursor position.
"""
function put(buf::IO, pos::Vector, s::String)
Expand All @@ -39,4 +39,4 @@ function put(buf::IO, pos::Vector, s::String)
print(buf, s)
cursor_restore_position(buf)
end
put(pos::Vector, s::String) = put(STDOUT, pos::Vector, s::String)
put(pos::Vector, s::String) = put(terminal.out_stream, pos::Vector, s::String)
10 changes: 5 additions & 5 deletions Terminal.jl/rawmode.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function rawmode(f, terminal=terminal, hide_cursor=true)
rawenabled = enableRawMode(terminal)
function rawmode(f, hide_cursor=true)
rawenabled = enableRawMode()
rawenabled && hide_cursor && cursor_hide(terminal.out_stream)
try
f()
finally
rawenabled && disableRawMode(terminal); cursor_show(terminal.out_stream)
rawenabled && disableRawMode(); cursor_show(terminal.out_stream)
end
end

function enableRawMode(terminal)
function enableRawMode()
try
REPL.Terminals.raw!(terminal, true)
return true
Expand All @@ -18,7 +18,7 @@ function enableRawMode(terminal)
return false
end

function disableRawMode(terminal)
function disableRawMode()
try
REPL.Terminals.raw!(terminal, false)
return true
Expand Down
8 changes: 5 additions & 3 deletions Terminal.jl/readkey.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function readKey(stream::IO=STDIN)
function readKey(stream::IO=terminal.in_stream)
readNextChar() = Char(read(stream,1)[1])
c = readNextChar()
if c == '\x1b' # Escape
esc_s = ""
while isempty(esc_s) || esc_s[end] ['A','B','C','D','F','H','P','Q','R','S','~']
esc_s *= readNextChar()
esc_s *= readNextChar()
end
return esc_codes[esc_s]
elseif Int(c) in 0:31
Expand All @@ -16,10 +16,12 @@ end

function testread()
clear_screen()
result = ""
while result != "Ctrl-C"
result = readKey()
cursor_move_abs([0,0])
cursor_deleteline()
print("read: ", readKey())
print("read: ", result)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/REPLTetris.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__precompile__()
module REPLTetris

using Crayons
using Crayons, Compat
export tetris
import Base.copy

Expand All @@ -23,7 +23,7 @@ function tetris(board = Board(), tile = rand(Tiles)())
print_tile_preview(nexttile)

while !abort[1] && drop!(board, tile)
sleep((0.8 - (board.level-1) * 0.007)^(board.level-1))
sleep((0.8 - (board.level-1) * 0.007)^(board.level-1))
end

delete_lines!(board)
Expand Down
14 changes: 7 additions & 7 deletions src/board.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function delete_lines!(board::Board)
end
board.lines_to_goal -= nr_lines
board.score += [0 1 3 5 8][nr_lines+1] * board.level * 100
if board.lines_to_goal 0
if board.lines_to_goal 0
board.level += 1
board.lines_to_goal += board.level*5
end
Expand All @@ -41,18 +41,18 @@ end
function blocks(i)
buf = IOBuffer()
block = ""
if i==0
if i==0
block =""
i += 8
end
end
print(buf, Crayon(foreground = i), block )
return String(take!(buf))
end

function update_board!(b1::Board, b2::Board)
@compat function update_board!(b1::Board, b2::Board)
buf = IOBuffer()
for I in findall(b1.data .⊻ b2.data .!= 0)
y,x = Tuple(I)
for i in findall(b1.data .⊻ b2.data .!= 0)
y,x = ind2sub((20,10), i)
put(buf, [(3*x)-2,y], blocks(b2.data[y,x]))
end
if (b1.level != b2.level) || (b1.score != b2.score)
Expand All @@ -72,7 +72,7 @@ function print_tile_preview(tile::Tile)
dt = data(tile)'
_, dy = size(dt)
for i in 1:dy
put(buf, [35, 10+i], string(blocks.(dt[:, i])...))
put(buf, [35, 10+i], string(blocks.(dt[:, i])...))
end
print(String(take!(buf)))
end

0 comments on commit d30298d

Please sign in to comment.