diff --git a/Project.toml b/Project.toml index 6cac549..40303c7 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/events.jl b/src/events.jl index 5376018..0e0804e 100644 --- a/src/events.jl +++ b/src/events.jl @@ -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...) diff --git a/test/runtests.jl b/test/runtests.jl index da0916f..8cffd4c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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