Skip to content

Commit 64631ee

Browse files
Merge pull request #3 from KristofferC/kc/io
Use sprint instead of redirecting stdout
2 parents fb5761b + b62ec82 commit 64631ee

File tree

2 files changed

+50
-74
lines changed

2 files changed

+50
-74
lines changed

src/DocumentFunction.jl

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,6 @@ module DocumentFunction
1414

1515
export documentfunction, getfunctionmethods, getfunctionarguments, getfunctionkeywords
1616

17-
"""
18-
Redirect STDOUT to a reader
19-
"""
20-
function stdoutcaptureon()
21-
global outputoriginal = stdout;
22-
(outR, outW) = redirect_stdout();
23-
global outputread = outR;
24-
global outputwrite = outW;
25-
global outputreader = @async read(outputread, String);
26-
end
27-
28-
"""
29-
Restore STDOUT
30-
"""
31-
function stdoutcaptureoff()
32-
redirect_stdout(outputoriginal);
33-
close(outputwrite);
34-
fetch(outputreader);
35-
close(outputread);
36-
return outputreader.result
37-
end
38-
3917
"""
4018
Get function methods
4119
@@ -54,60 +32,60 @@ end
5432

5533
function documentfunction(f::Function; location::Bool=true, maintext::String="", argtext::Dict=Dict(), keytext::Dict=Dict())
5634
modulename = first(methods(f)).module
57-
stdoutcaptureon()
58-
if maintext != ""
59-
println("**", parentmodule(f), ".", nameof(f), "**\n")
60-
println("$(maintext)\n")
61-
end
62-
ms = getfunctionmethods(f)
63-
nm = length(ms)
64-
if nm == 0
65-
println("No methods\n")
66-
else
67-
println("Methods:")
68-
for i = 1:nm
69-
s = strip.(split(ms[i], " at "))
70-
m = match(r"(\[.+\]\s)(.*)", s[1]) # take string after [1..]
71-
methodname = m.captures[2]
72-
if location
73-
println(" - `$modulename.$(methodname)` : $(s[2])")
74-
else
75-
println(" - `$modulename.$(methodname)`")
76-
end
35+
sprint() do io
36+
if maintext != ""
37+
println(io, "**", parentmodule(f), ".", nameof(f), "**\n")
38+
println(io, "$(maintext)\n")
7739
end
78-
a = getfunctionarguments(f, ms)
79-
l = length(a)
80-
if l > 0
81-
println("Arguments:")
82-
for i = 1:l
83-
arg = strip(string(a[i]))
84-
print(" - `$(arg)`")
85-
if occursin("::", arg)
86-
arg = split(arg, "::")[1]
87-
end
88-
if haskey(argtext, arg)
89-
println(" : $(argtext[arg])")
40+
ms = getfunctionmethods(f)
41+
nm = length(ms)
42+
if nm == 0
43+
println(io, "No methods\n")
44+
else
45+
println(io, "Methods:")
46+
for i = 1:nm
47+
s = strip.(split(ms[i], " at "))
48+
m = match(r"(\[.+\]\s)(.*)", s[1]) # take string after [1..]
49+
methodname = m.captures[2]
50+
if location
51+
println(io, " - `$modulename.$(methodname)` : $(s[2])")
9052
else
91-
println("")
53+
println(io, " - `$modulename.$(methodname)`")
9254
end
9355
end
94-
end
95-
a = getfunctionkeywords(f, ms)
96-
l = length(a)
97-
if l > 0
98-
println("Keywords:")
99-
for i = 1:l
100-
key = strip(string(a[i]))
101-
print(" - `$(key)`")
102-
if haskey(keytext, key)
103-
println(" : $(keytext[key])")
104-
else
105-
println("")
56+
a = getfunctionarguments(f, ms)
57+
l = length(a)
58+
if l > 0
59+
println(io, "Arguments:")
60+
for i = 1:l
61+
arg = strip(string(a[i]))
62+
print(io, " - `$(arg)`")
63+
if occursin("::", arg)
64+
arg = split(arg, "::")[1]
65+
end
66+
if haskey(argtext, arg)
67+
println(io, " : $(argtext[arg])")
68+
else
69+
println(io, "")
70+
end
71+
end
72+
end
73+
a = getfunctionkeywords(f, ms)
74+
l = length(a)
75+
if l > 0
76+
println(io, "Keywords:")
77+
for i = 1:l
78+
key = strip(string(a[i]))
79+
print(io, " - `$(key)`")
80+
if haskey(keytext, key)
81+
println(io, " : $(keytext[key])")
82+
else
83+
println(io, "")
84+
end
10685
end
10786
end
10887
end
10988
end
110-
stdoutcaptureoff()
11189
end
11290

11391
@doc """

test/runtests.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ output = DocumentFunction.documentfunction(DocumentFunction.documentfunction;
1313
"keytext"=>"Dictionary with text for each keyword",
1414
"location"=>"Boolean to show/hide function location on the disk"))
1515

16-
DocumentFunction.documentfunction(DocumentFunction.stdoutcaptureon; location=true)
17-
DocumentFunction.getfunctionkeywords(DocumentFunction.stdoutcaptureon)
18-
DocumentFunction.getfunctionarguments(DocumentFunction.stdoutcaptureon)
19-
16+
DocumentFunction.documentfunction(DocumentFunction.documentfunction; location=true)
17+
noargfunction() = nothing
2018
@Test.testset "Document" begin
2119
@Test.test output == expected
22-
@Test.test [] == DocumentFunction.getfunctionkeywords(DocumentFunction.stdoutcaptureon)
23-
@Test.test [] == DocumentFunction.getfunctionarguments(DocumentFunction.stdoutcaptureon)
20+
@Test.test [] == DocumentFunction.getfunctionkeywords(DocumentFunction.getfunctionmethods)
21+
@Test.test [] == DocumentFunction.getfunctionarguments(noargfunction)
2422
end
2523

2624
:passed

0 commit comments

Comments
 (0)