Skip to content

Commit

Permalink
Add default print to stdout for mwetable/mwearray (#30)
Browse files Browse the repository at this point in the history
* add default print to stdout

* add for no-table inputs

---------

Co-authored-by: Peter Deffebach <[email protected]>
  • Loading branch information
svilupp and pdeffebach authored Mar 13, 2024
1 parent ee8d9cf commit 795d3bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 22 deletions.
57 changes: 38 additions & 19 deletions src/ClipData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ function cliparray(t::AbstractVecOrMat; returnstring = false, delim='\t',
end

"""
mwetable(; name="df")
mwetable([io::IO=stdout]; name="df")
Create a Minimum Working Example (MWE) using
the clipboard. `tablmwe` prints out a multi-line
comma-separated string and provides the necessary
code to read that string using `CSV.File`.
The object is assigned the name given by
`name` (default `"df"`).
`name` (default `"df"`). Prints to `io`,
which is by default `stdout`.
# Examples
Expand All @@ -187,21 +188,25 @@ a,b
```
"""
function mwetable(; returnstring=false, name="df")
function mwetable(io::IO; returnstring=false, name="df")
t = cliptable()
mwetable(t, returnstring=returnstring, name=name)
mwetable(io, t, returnstring=returnstring, name=name)
end

mwetable(; kwargs...) = mwetable(stdout; kwargs...)


"""
mwetable(t; name="df")
mwetable([io::IO=stdout], t; name="df")
Create a Minimum Working Example (MWE) from
an existing Tables.jl-compatible object.
`tablmwe` prints out a multi-line
comma-separated string and provides the necessary
code to read that string using `CSV.File`.
The object is assigned the name given by
`name` (default `:df`).
`name` (default `:df`). Prints to `io`,
which is by default `stdout`.
# Examples
Expand All @@ -218,7 +223,7 @@ a,b
\"\"\" |> IOBuffer |> CSV.File
```
"""
function mwetable(t; returnstring=false, name="df")
function mwetable(io::IO, t; returnstring=false, name="df")
main_io = IOBuffer()
table_io = IOBuffer()

Expand All @@ -238,10 +243,13 @@ $name = \"\"\"
if returnstring == true
return s
else
print(io, s)
return nothing
end
end

mwetable(t; kwargs...) = mwetable(stdout, t; kwargs...)

function mwetable_helper(t::Symbol)
t_name = QuoteNode(t)
:(mwetable($t, name = $t_name))
Expand All @@ -256,7 +264,8 @@ an existing Tables.jl-compatible object.
comma-separated string and provides the necessary
code to read that string using `CSV.File`. The name
assigned to the object in the MWE is the
same as the name of the input object.
same as the name of the input object. Prints
to `stdout`.
# Examples
Expand All @@ -278,13 +287,14 @@ macro mwetable(t)
end

"""
mwearray(; name=:X)
mwearray([io::IO=stdout]; name=:X)
Create a Minimum Working Example (MWE) from
the clipboard to create an array. `mwearray`
returns the a multi-line string with the
code necessary to read the string stored in
clipboard as a `Vector` or `Matrix`.
clipboard as a `Vector` or `Matrix`. Prints to `io`,
which is by default `stdout`.
# Examples
Expand All @@ -301,19 +311,22 @@ X = \"\"\"
\"\"\" |> IOBuffer |> CSV.File |> Tables.matrix
```
"""
function mwearray(; returnstring=false, name=nothing)
function mwearray(io::IO; returnstring=false, name=nothing)
t = cliparray()
name= t isa AbstractVector ? :x : :X
mwearray(t, returnstring=returnstring, name=name)
mwearray(io, t, returnstring=returnstring, name=name)
end

mwearray(; kwargs...) = mwearray(stdout; kwargs...)

"""
mwearray(t::AbstractMatrix; name=:X)
mwearray([io::IO=stdout], t::AbstractMatrix; name=:X)
Create a Minimum Working Example (MWE) from
a `Matrix`. `mwearray`
returns the a multi-line string with the
code necessary to recreate `t`.
code necessary to recreate `t`. Prints to `io`,
which is by default `stdout`.
# Examples
Expand All @@ -330,7 +343,7 @@ X = \"\"\"
\"\"\" |> IOBuffer |> CSV.File |> Tables.matrix
```
"""
function mwearray(t::AbstractMatrix; returnstring=false, name=:X)
function mwearray(io::IO, t::AbstractMatrix; returnstring=false, name=:X)
main_io = IOBuffer()
array_io = IOBuffer()

Expand All @@ -350,17 +363,19 @@ $name = \"\"\"
if returnstring == true
return s
else
print(io, s)
return nothing
end
end

"""
mwearray(t::AbstractVector; name=:x)
mwearray([io::IO=stdout], t::AbstractVector; name=:x)
Create a Minimum Working Example (MWE) from
a `Vector`. `mwearray`
returns the a multi-line string with the
code necessary to recreate `t`.
code necessary to recreate `t`. Prints to `io`,
which is by default `stdout`.
# Example
Expand All @@ -374,7 +389,7 @@ x = \"\"\"
\"\"\" |> IOBuffer |> CSV.File |> Tables.matrix |> vec
```
"""
function mwearray(t::AbstractVector; returnstring=false, name=:x)
function mwearray(io::IO, t::AbstractVector; returnstring=false, name=:x)
main_io = IOBuffer()
array_io = IOBuffer()

Expand All @@ -396,10 +411,13 @@ $name = \"\"\"
if returnstring == true
return s
else
print(io, s)
return nothing
end
end

mwearray(t::Union{AbstractVector, AbstractMatrix}; kwargs...) = mwearray(stdout, t; kwargs...)

function mwearray_helper(t::Symbol)
t_name = QuoteNode(t)
:(mwearray($t, name=$t_name))
Expand All @@ -410,7 +428,8 @@ end
Create a Minimum Working Example (MWE)
from a `Vector` or `Matrix` with the same
name as the object in the Julia session.
name as the object in the Julia session. Prints
to `stdout`.
# Examples
Expand Down
33 changes: 30 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ a,b

@test s == s_correct


t = (a = [1, 3], b = [2, 4])
s = mwetable(t; returnstring = true)

@test s == s_correct

# Tests the default printing to stdout
io = IOBuffer()
mwetable(io, t; returnstring=false)
@test String(take!(io)) == s_correct

io = IOBuffer()
cliptable(t)
mwetable(io; returnstring=false)
@test String(take!(io)) == s_correct
end

@testset "mwearray" begin
Expand All @@ -107,12 +115,20 @@ X = \"\"\"

@test s == s_correct


t = [1 2; 3 4]
s = mwearray(t; returnstring = true)

@test s == s_correct

io = IOBuffer()
mwearray(io, t; returnstring=false)
@test String(take!(io)) == s_correct

io = IOBuffer()
cliparray(t)
mwearray(io)
@test String(take!(io)) == s_correct

"""
1
2
Expand All @@ -137,6 +153,17 @@ x = \"\"\"

s = mwearray(x; returnstring=true)
@test s == s_correct

# Tests the default printing to stdout
io = IOBuffer()
mwearray(io, x; returnstring=false)
@test String(take!(io)) == s_correct

io = IOBuffer()
cliparray(x)
mwearray(io)
@test String(take!(io)) == s_correct

end

@testset "@mwetable" begin
Expand Down

0 comments on commit 795d3bc

Please sign in to comment.