Pretty printing enhancements

pull/4/head
fundamental 2013-10-27 18:49:24 -04:00
parent 192392ba19
commit 76c77b5fc1
2 changed files with 35 additions and 31 deletions

View File

@ -1,4 +1,5 @@
module OSC module OSC
import Base.show
macro incfp(x) quote begin macro incfp(x) quote begin
local gensym_ = $(esc(x)) local gensym_ = $(esc(x))
$(esc(x)) = $(esc(x))+1 $(esc(x)) = $(esc(x))+1
@ -93,7 +94,7 @@ end
function vsosc_null(address::ASCIIString, function vsosc_null(address::ASCIIString,
arguments::ASCIIString, arguments::ASCIIString,
args...) args...)
pos::Int = length(address) pos::Int = length(address)+1
pos = align(pos) pos = align(pos)
pos += 1+length(arguments) pos += 1+length(arguments)
pos = align(pos) pos = align(pos)
@ -119,7 +120,7 @@ function vsosc_null(address::ASCIIString,
end #other args classes are ignored end #other args classes are ignored
end end
return pos; return pos-1;
end end
function rtosc_amessage(buffer::Array{Uint8}, function rtosc_amessage(buffer::Array{Uint8},
@ -196,6 +197,8 @@ function rtosc_amessage(buffer::Array{Uint8},
return pos-1; return pos-1;
end end
OscMsg(address, arguments, args...) = message(address, arguments, args...)
function message(address::ASCIIString, function message(address::ASCIIString,
arguments::ASCIIString, arguments::ASCIIString,
args...) args...)
@ -273,15 +276,15 @@ end
getindex(msg::OscMsg, idx::Int) = rtosc_argument(msg, idx) getindex(msg::OscMsg, idx::Int) = rtosc_argument(msg, idx)
function show(msg::OscMsg) function show(io::IO, msg::OscMsg)
println("OSC Message to ", stringify(msg.data)) println(io, "OSC Message to ", stringify(msg.data))
println(" Arguments:"); println(io, " Arguments:");
for i=1:narguments(msg) for i=1:narguments(msg)
showField(msg,i) showField(io, msg,i)
end end
end end
function showField(msg::OscMsg, arg_id) function showField(io::IO, msg::OscMsg, arg_id)
map = ['i' Int32; 'f' Float32; 's' String; 'b' :Blob; 'h' Int32; 't' Uint64; map = ['i' Int32; 'f' Float32; 's' String; 'b' :Blob; 'h' Int32; 't' Uint64;
'd' Float64; 'S' Symbol; 'c' Char; 'r' :RBG; 'm' :Midi; 'T' true; 'd' Float64; 'S' Symbol; 'c' Char; 'r' :RBG; 'm' :Midi; 'T' true;
'F' false; 'N' Nothing] 'F' false; 'N' Nothing]
@ -292,11 +295,14 @@ function showField(msg::OscMsg, arg_id)
if(issubtype(typeof(value), Array)) if(issubtype(typeof(value), Array))
value = value' value = value'
end end
@printf(" #%2d %c:", arg_id, typeChar); @printf(io, " #%2d %c:", arg_id, typeChar);
print(dict[typeChar]," - ", value) print(dict[typeChar]," - ", value)
if(!issubtype(typeof(value), Array)) if(!issubtype(typeof(value), Array))
println() println()
end end
end end
export OscMsg
end end

View File

@ -1,5 +1,6 @@
using Base.Test using Base.Test
require("OSC") require("OSC")
using OSC
test_type = length(ARGS) == 1 ? ARGS[1] : "ALL" test_type = length(ARGS) == 1 ? ARGS[1] : "ALL"
@ -36,9 +37,8 @@ function test_it_fat()
#nil #nil
#inf #inf
msg = OSC.message("/dest", msg = OscMsg("/dest", "[ifsbhtdScrmTFNI]", i,f,s,b,h,t,d,S,c,r,m);
"[ifsbhtdScrmTFNI]", i,f,s,b,h,t,d,S,c,r,m); show(msg)
OSC.show(msg)
#println(string(map(x->(hex(x,2)), buffer[1:len])...)) #println(string(map(x->(hex(x,2)), buffer[1:len])...))
#println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...)) #println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...))
@ -62,7 +62,6 @@ function test_it_fat()
end end
function test_it_osc_spec() function test_it_osc_spec()
buffer::Array{Uint8} = Array(Uint8, 256)
println("Starting OSC Spec...") println("Starting OSC Spec...")
message_one::Array{Uint8} = [ message_one::Array{Uint8} = [
0x2f, 0x6f, 0x73, 0x63, 0x2f, 0x6f, 0x73, 0x63,
@ -76,37 +75,36 @@ function test_it_osc_spec()
]; ];
message_two::Array{Uint8} = [ message_two::Array{Uint8} = [
0x2f, 0x66, 0x6f, 0x6f, 0x2f, 0x66, 0x6f, 0x6f, #4
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, #8
0x2c, 0x69, 0x69, 0x73, 0x2c, 0x69, 0x69, 0x73,
0x66, 0x66, 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, #16
0x00, 0x00, 0x03, 0xe8, 0x00, 0x00, 0x03, 0xe8,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x68, 0x65, 0x6c, 0x6c, 0x68, 0x65, 0x6c, 0x6c,
0x6f, 0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, #32
0x3f, 0x9d, 0xf3, 0xb6, 0x3f, 0x9d, 0xf3, 0xb6, #36
0x40, 0xb5, 0xb2, 0x2d, 0x40, 0xb5, 0xb2, 0x2d, #40
]; ];
len=OSC.rtosc_amessage(buffer, 256, "/oscillator/4/frequency", "f", float32(440.0)) osc=OscMsg("/oscillator/4/frequency", "f", float32(440.0))
println(string(map(x->(hex(x,2)), buffer[1:len])...)) println(string(map(x->(hex(x,2)), osc.data)...))
println(string(map(x->(hex(x,2)), message_one)...)) println(string(map(x->(hex(x,2)), message_one)...))
println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...)) println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), osc.data)...))
println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), message_one)...)) println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), message_one)...))
@test len == length(message_one) @test length(osc.data) == length(message_one)
@test buffer[1:length(message_one)] == message_one @test osc.data == message_one
OSC.show(OSC.OscMsg(buffer)) show(osc)
len = OSC.rtosc_amessage(buffer, 256, "/foo", "iisff", osc = OscMsg("/foo", "iisff", int32(1000), int32(-1), "hello", float32(1.234), float32(5.678))
int32(1000), int32(-1), "hello", float32(1.234), float32(5.678)) println(string(map(x->(hex(x,2)), osc.data)...))
println(string(map(x->(hex(x,2)), buffer[1:len])...))
println(string(map(x->(hex(x,2)), message_two)...)) println(string(map(x->(hex(x,2)), message_two)...))
println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), buffer[1:len])...)) println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), osc.data)...))
println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), message_two)...)) println(string(map(x->(isprint(char(x&0x7f)) ? string(char(x&0x7f)," ") : ". "), message_two)...))
@test len == length(message_two) @test length(osc.data) == length(message_two)
@test buffer[1:len] == message_two @test osc.data == message_two
OSC.show(OSC.OscMsg(buffer)) show(osc)
end end
if test_type in ["ALL", "TEST", "INSTALL"] if test_type in ["ALL", "TEST", "INSTALL"]