Skip to content

Commit

Permalink
Update and test event_loop for julia 1.x (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvertSchippers authored Dec 5, 2020
1 parent 3f6d3bd commit 4c8bb11
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Displaz"
uuid = "67fdca37-c731-5d95-9a19-4e7673210fab"
version = "1.2.0"
version = "1.2.1"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
34 changes: 25 additions & 9 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,51 @@ end
```
"""
function event_loop(callback::Function, plotobj::DisplazWindow, event_list...)

command = hook_command(plotobj.name, event_list...)

open(command, "r") do event_stream
handle_events(callback, event_stream)
end

end

function hook_command(servername, event_list...)

hookopts = [["-hook", _eventspec_str(e), _argspec_str(p)]
for (e,p) in event_list]
stdout,stdin,proc = readandwrite(`$_displaz_cmd -server $(plotobj.name) $(vcat(hookopts...))`)
while true
rawline = readline(stdout)
for (e,p) in event_list]

return `$_displaz_cmd -server $(servername) $(vcat(hookopts...))`
end

function handle_events(callback, event_stream)

while !eof(event_stream)
rawline = readline(event_stream)
line = split(rawline)
if length(line) < 2
warn("Unrecognized displaz hook string: \"$line\"")
@warn("Unrecognized displaz hook string: \"$line\"")
break
end
eventspec = line[1]
if startswith(eventspec, "key:")
event = KeyEvent(eventspec[5:end])
else
warn("Unrecognized displaz event type: \"$eventspec\"")
@warn("Unrecognized displaz event type: \"$eventspec\"")
event = eventspec
end
argspec = line[2]
if argspec == "null"
arg = :nothing
arg = nothing
elseif argspec == "cursor"
arg = CursorPosition(map(s->parse(Float64,s), line[3:end]))
else
warn("Unrecognized displaz hook payload: \"$argspec\"")
@warn("Unrecognized displaz hook payload: \"$argspec\"")
arg = line[3:end]
end
callback(event, arg) != false || break
end
kill(proc)

end

event_loop(callback::Function, event_list...) = event_loop(callback, current(), event_list...)
Expand Down
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,33 @@ end
@test @displaz_args(annotation([0, 1, 2], "Hello world")) == `-script -server default -annotation "Hello world" 0 1 2 -label "Hello world"`
@test @displaz_args(annotation([0, 1, 2], "Hello world", "A label")) == `-script -server default -annotation "Hello world" 0 1 2 -label "A label"`
end

@testset "hook" begin

# first make sure the command is OK:
command = Displaz.hook_command("someserver",
KeyEvent("c")=>Nothing,
KeyEvent("p")=>CursorPosition)

@test command == `$(Displaz._displaz_cmd) -server someserver -hook key:c null -hook key:p cursor`

# prepare some fake Displaz outputs:
lines = ["key:c null",
"key:p cursor 0 0 0"]
event_stream = IOBuffer()
foreach(l -> write(event_stream, l * "\n"), lines)
seek(event_stream, 0)

# prepare our received events buffer and a callback function:
received = Vector{Any}()
callback(x...) = push!(received, x)

# this should plow through our fake output lines and return:
Displaz.handle_events(callback, event_stream)

# see if we got what we expected:
@test length(received) == 2
@test received[1] == (KeyEvent("c"), nothing)
@test received[2] == (KeyEvent("p"), CursorPosition([0.0, 0.0, 0.0]))

end

0 comments on commit 4c8bb11

Please sign in to comment.