Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add filtering behavior to whos() #10108

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1119,17 +1119,25 @@ 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]
for ft in filtertypes
isa(ft, DataType) ||
throw(ArgumentError("Non-type object given to filter keyword"))
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=Module) = 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
12 changes: 9 additions & 3 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ Getting Around

Determine whether Julia is running an interactive session.

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

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 who are of type or subtype of an element in ``filter`` will not be
displayed. E.g., ``filter=Any`` will prevent any output. If the function is
called with an explicit Module argument, nothing will be filtered.
Otherwise, submodules in the current module will not be displayed.

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

Expand Down