Skip to content

Commit

Permalink
Add filtering behavior to whos()
Browse files Browse the repository at this point in the history
Defaults to not displaying modules
  • Loading branch information
jdlangs committed Feb 27, 2015
1 parent 3b110e8 commit 7b6d9e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
26 changes: 18 additions & 8 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1113,17 +1113,27 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
cartesianmap(print_slice, tail)
end

function whos(m::Module, pattern::Regex)
for v in sort(names(m))
s = string(v)
if isdefined(m,v) && ismatch(pattern, s)
println(rpad(s, 30), summary(eval(m,v)))
function whos(m::Module, pattern::Regex; filter=[])
filtertypes = applicable(start, filter) ?
filter : [filter]
istype(t) = any(T -> isa(t,T), [DataType,UnionType,TypeConstructor])
if any(T -> ! istype(T), filtertypes)
throw(ArgumentError(
"Keyword argument \"filter\" must be a type or iterable of types"
))
end
filtertest(var) = ! any(T -> typeof(var) <: T, filtertypes)
for n in sort(names(m))
s = string(n)
v = eval(m,n)
if isdefined(m,n) && ismatch(pattern, s) && filtertest(v)
println(rpad(s, 30), summary(v))
end
end
end
whos() = whos(r"")
whos(m::Module) = whos(m, r"")
whos(pat::Regex) = whos(current_module(), pat)
whos(;filter=Module) = whos(r""; filter=filter)
whos(m::Module; filter=[]) = whos(m, r""; filter=filter)
whos(pat::Regex; filter=[]) = whos(current_module(), pat; filter=filter)

# global flag for limiting output
# TODO: this should be replaced with a better mechanism. currently it is only
Expand Down
10 changes: 7 additions & 3 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ Getting Around

Determine whether Julia is running an interactive session.

.. function:: whos([Module,] [pattern::Regex])
.. function:: whos([Module,] [pattern::Regex]; [filter=nothing])

Print information about exported global variables in a module, optionally restricted
to those matching ``pattern``.
Print information about exported global variables in a module (defaults to
the current module), optionally restricted to those matching ``pattern``.

The ``filter`` keyword is either a single type or iterable object of types.
Variables whose type or any supertype is given will not be displayed. If
invoked without arguments, ``filter`` defaults to ``Module``.

.. function:: edit(file::AbstractString, [line])

Expand Down

0 comments on commit 7b6d9e6

Please sign in to comment.